1

我正在尝试在框架中显示一个简单的 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);
    }
}
4

2 回答 2

5

JTable仅当位于 内时,表列才会自动显示JScrollPane。而不是this.add(table);,尝试this.add(new JScrollPane(table));

实际上,您似乎已经有一些与滚动窗格相关的代码 - 只需取消注释JScrollPane scrollPane = new JScrollPane(table);并更改this.add(table);this.add(scrollpane);

于 2012-08-22T08:03:33.583 回答
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);
            }
        });
}

这篇文章可能会帮助你理解“Threading with Swing”的概念

于 2012-08-22T08:11:45.220 回答