Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 接口不在列中_Java_Eclipse_Swing_User Interface_Layout - Fatal编程技术网

Java 接口不在列中

Java 接口不在列中,java,eclipse,swing,user-interface,layout,Java,Eclipse,Swing,User Interface,Layout,我目前正在编写一个produkt系统,我正在设计接口。我的问题是,接口不在列中。我想把它和我的代码放在一个栏中,在“suche nach waren”行中再添加一个按钮也不错 代码如下: GroupLayout tLayout = new GroupLayout(mainFrame.getContentPane()); mainFrame.getContentPane().setLayout(tLayout); tLayout.setAutoCreateGaps(true); tLayout.s

我目前正在编写一个produkt系统,我正在设计接口。我的问题是,接口不在列中。我想把它和我的代码放在一个栏中,在“suche nach waren”行中再添加一个按钮也不错 代码如下:

GroupLayout tLayout = new GroupLayout(mainFrame.getContentPane());
mainFrame.getContentPane().setLayout(tLayout);
tLayout.setAutoCreateGaps(true);
tLayout.setAutoCreateContainerGaps(true);

tLayout.setHorizontalGroup(tLayout.createSequentialGroup()
        .addGroup(tLayout.createParallelGroup()
                .addComponent(tLabel0))
        .addGroup(tLayout.createParallelGroup()
                .addComponent(tLabel1)
                .addComponent(tLabel2)
                .addComponent(tLabel3)
                .addComponent(tLabel4))
        .addGroup(tLayout.createParallelGroup()
                .addComponent(tTextField1)
                .addComponent(tTextField2)
                .addComponent(tTextField3)
                .addComponent(tCombo)
                .addComponent(tButton1))
        .addGroup(tLayout.createParallelGroup()
                .addComponent(tTable))
        );

tLayout.setVerticalGroup(tLayout.createSequentialGroup()
        .addComponent(tLabel0)
        .addGroup(tLayout.createParallelGroup()
                .addComponent(tLabel1)
                .addComponent(tTextField1))
            .addGroup(tLayout.createParallelGroup()
                .addComponent(tLabel2)
                .addComponent(tTextField2))
        .addGroup(tLayout.createParallelGroup()
                .addComponent(tLabel3)
                .addComponent(tTextField3))
        .addGroup(tLayout.createParallelGroup()
                .addComponent(tLabel4)
                .addComponent(tCombo))
        .addGroup(tLayout.createParallelGroup()
                .addComponent(tButton1))
        .addGroup(tLayout.createParallelGroup()
                .addComponent(tTable)));
我的代码怎么了? 谢谢你的回答

注:我不想使用布局编辑器,因为这是我第一次使用,我想学习SWING

PPS.:


除了上面的标签和表格外,所有内容都在正确的位置上

GridBagLayout示例

您真的应该将
JTable
放在JScrollPane中,它会为您处理标题,但是

public class TestLayout {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FormPane());
                frame.setSize(600, 300);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected static class FormPane extends JPanel {

        public FormPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.WEST;

            add(new JLabel("Geben sie bitte die Kriterien für die Suche an:"), gbc);

            gbc.gridwidth = 1;
            gbc.gridy++;
            add(new JLabel("Name:"), gbc);
            gbc.gridy++;
            add(new JLabel("Maximaler Preis:"), gbc);
            gbc.gridy++;
            add(new JLabel("Alter des Kunden:"), gbc);
            gbc.gridy++;
            add(new JLabel("Kategorie:"), gbc);

            gbc.gridx++;
            gbc.gridy = 1;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JComboBox(), gbc);
            gbc.gridy++;
            add(new JButton("Click me"), gbc);

            gbc.gridy++;
            gbc.gridx = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            DefaultTableModel model = new DefaultTableModel(
                            new Object[][] {
                                {"id", "Name", "Price", "Something", "Something", "Something"},
                                {"NA", "NA", "NA", "NA", "NA", "NA"}
                            },
                            new Object[]{"id", "Name", "Price", "Something", "Something", "Something"});


            add(new JTable(model), gbc);

        }
    }
}

“我感谢您的回答”然后请添加一个真实的问题。用ASCII艺术画出控件应如何以最小尺寸排列,以及何时为容器提供额外的空间/高度+1用于添加问题。建议:如果要手动编写版面代码,请远离
GroupLayout
。正如在教程中所说,该布局是为GUI builder工具设计的,适合手工编码(这是可行的,但它是一个PITA),对于这种类似表单的布局,我总是推荐JGoodies的
FormLayout
,但这是第三方libFormLayout是Swing布局管理器,但是你也可以使用GridBagLayout做同样的事情,每次我看到这样的代码片段,结果我对自己说,我应该花一些时间学习
GridBagLayout
,但是只要看一眼这些代码,我就会回到
FormLayout
。你能解释一下为什么你的
FormPane
扩展自
JLabel
。甚至不需要扩展,但如果您使用它,为什么不从
JPanel
?哈哈,我想知道为什么我在显示它时遇到问题,是从旧版本复制的example@robin我对FormLayout也有同样的感觉。我真的很想学习它,我认为它可以解决很多问题,而这些问题是好的GridBagLayout所不能解决的,但是我看了代码,它让我头晕目眩。花了3年时间用GridBagLayout手工编写布局,你会对它有点用
public class TestLayout {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new FormPane());
                frame.setSize(600, 300);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected static class FormPane extends JPanel {

        public FormPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.anchor = GridBagConstraints.WEST;

            add(new JLabel("Geben sie bitte die Kriterien für die Suche an:"), gbc);

            gbc.gridwidth = 1;
            gbc.gridy++;
            add(new JLabel("Name:"), gbc);
            gbc.gridy++;
            add(new JLabel("Maximaler Preis:"), gbc);
            gbc.gridy++;
            add(new JLabel("Alter des Kunden:"), gbc);
            gbc.gridy++;
            add(new JLabel("Kategorie:"), gbc);

            gbc.gridx++;
            gbc.gridy = 1;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JComboBox(), gbc);
            gbc.gridy++;
            add(new JButton("Click me"), gbc);

            gbc.gridy++;
            gbc.gridx = 0;
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            DefaultTableModel model = new DefaultTableModel(
                            new Object[][] {
                                {"id", "Name", "Price", "Something", "Something", "Something"},
                                {"NA", "NA", "NA", "NA", "NA", "NA"}
                            },
                            new Object[]{"id", "Name", "Price", "Something", "Something", "Something"});


            add(new JTable(model), gbc);

        }
    }
}