我怎么能在gridlayout中看到我的桌子?

时间:2014-05-22 06:19:35

标签: java eclipse swing jtable

这是我项目中与我大学的人工智能课程相关的一部分。为此,我想创建一个以两个值作为输入的应用程序。输入必须由user进入我的两个表。为了获得两个输入我在gui中插入我的表但是当我运行这个代码时,我无法看到我注意到的gui(我的意思是我的两个表)。在那里&# 39;我的代码:

public class UserInterface extends JFrame {

private JTable table1;
private JTable table2;
private String[] ColumnName = { "", "", "", "" };
private Object[][] content = {{"1","2","3","4"},{"5","6","7","8"},{"9","10","11","12"},{"13","14","15","16"}};
private DefaultTableModel tableModel;
private JPanel columnPanel;
private JPanel buttonpanel;
private JButton BFS = new JButton("BfS Search");
private JButton IDS = new JButton("IDS Search");
private JButton ASTAR = new JButton("A* Search");
private JButton BIdirect = new JButton("Bidirectional Search");

public UserInterface() {

    getContentPane().setLayout(
            new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    setSize(new Dimension(400, 500));
    GridLayout gridLayout = new GridLayout(1, 2);
    tableModel = new DefaultTableModel(content, ColumnName);
    table1 = new JTable(tableModel) {
        public Class getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }
    };
    table2 = new JTable(tableModel) {
        public Class getColumnClass(int column) {
            return getValueAt(0, column).getClass();
        }
    };
    gridLayout.addLayoutComponent("Table1", table1);
    gridLayout.addLayoutComponent("Table2", table2);
    columnPanel = new JPanel();
    columnPanel.setLayout(gridLayout);
    add(columnPanel);
    buttonpanel = new JPanel();
    buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.X_AXIS));
    buttonpanel.add(BFS);
    buttonpanel.add(IDS);
    buttonpanel.add(ASTAR);
    buttonpanel.add(BIdirect);
    add(buttonpanel);
    Tools.setCenterLocation(this);
    Tools.setTheme(this);
}

public static void main(String[] args) {
    UserInterface interface1 = new UserInterface();
    interface1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    interface1.pack();
    interface1.setVisible(true);
}

}

我该如何解决?

1 个答案:

答案 0 :(得分:3)

请勿致电Layout#addLayoutComponent,您无权使用此方法,请改用Container#add

columnPanel.add(table1);
columnPanel.add(table2);