java - 我怎样才能在网格布局中看到我的表格?

标签 java eclipse swing jtable

这是我的项目的一部分,与我大学的人工智能类(class)相关。为此,我想创建一个以两个值作为输入的应用程序。输入必须由用户输入到我的两个表中。因此,为了获得两个输入,我在 gui 中插入了我的表,但是当我运行此代码时,我看不到我注意到的 gui(我的意思是我的两个表)。这是我的代码:

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);
}

}

我该如何解决这个问题?

最佳答案

不要调用Layout#addLayoutComponent,调用此方法不是您的责任,而是使用Container#add

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

关于java - 我怎样才能在网格布局中看到我的表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23799158/

相关文章:

java - 如何在 sourceforge.net 中搜索在 Eclipse 中构建的 Java 项目

java - 获取所有 jcomponent 及其在 jframe 上的位置

java - Play Action 组合委托(delegate)以单独调用

java - 在 js/php 范围内,什么是更好的选择/目标概念

java - 简单的 Java Map/Reduce 框架

java-动态 JLabel 未显示

java - 如何监听 JCombobox 选择事件

java - 在没有服务器的情况下建立平等客户端的双向 TCP 通信的最佳方法是什么?

java - 两者都实现了 JavaLaunchHelper 类。将使用两者之一。哪个是未定义的

eclipse - 与Buildship在build.gradle中相互排斥的仓库