我正在尝试在框架中显示一个简单的 jtable,但它没有显示表格的列,我按照 oracle 教程中的示例进行操作,但有些事情我做得不对。程序正在编译并运行。这是代码:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class SimpleTableDemo extends JPanel
{
SimpleTableDemo()
{
String[] columnNames = {"#",
"Repayment Dates",
"Principle Amount",
"Interest",
"Comission fee",
"Total installement amount",
"Principle balance"};
// BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);
//each line of the two dimentional array is a line in the table
Object[][] data = {
{new Integer(1), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(2), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(3), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(4), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(5), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(6), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(7), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(8), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(9), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(10), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(11), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(12), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(13), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(14), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(15), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(16), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(17), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
{new Integer(18), new java.util.Date(), new BigDecimal(12509.23).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(1571.77).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(0.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(14081.00).setScale(2, RoundingMode.HALF_EVEN), new BigDecimal(147490.77).setScale(2, RoundingMode.HALF_EVEN)},
};
JTable table = new JTable(data, columnNames);
// JScrollPane scrollPane = new JScrollPane(table);
// scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
// scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
table.setFillsViewportHeight(true);
this.add(table);
}
public static void main(String[] args)
{
JFrame application = new JFrame();
SimpleTableDemo panel = new SimpleTableDemo();
application.add(panel);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// application.setSize(730,650);
application.pack();
application.setLocationRelativeTo(panel);
// application.setResizable(false);
application.setVisible(true);
}
}