我正在尝试在框架中显示一个简单的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);
}
}
答案 0 :(得分:5)
如果JTable
位于JScrollPane
内,则会自动显示表格列。
而不是this.add(table);
,请尝试this.add(new JScrollPane(table));
实际上,您似乎已经存在一些与滚动窗格相关的代码 - 只需取消注释JScrollPane scrollPane = new JScrollPane(table);
并将this.add(table);
更改为this.add(scrollpane);
答案 1 :(得分:3)
拥有JScrollPane
总是有帮助的。您已经拥有以下代码:
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
您需要做的就是将JTable
添加到此scrollpane
。
但是,我建议在事件派发线程上对用户界面进行任何更新。基本上,而不是这个:
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);
}
考虑:
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
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);
}
});
}
This article可能会帮助您理解“使用Swing进行线程化”的概念