0

我目前正在编写产品系统,并且正在设计界面。我的问题是,接口不在列中。在此处输入图像描述我希望它与我的代码代码放在一个列中,最好在“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)));

我的代码有什么问题?我很感激你的回答!

PS.:我不想使用布局编辑器,因为这是我的第一次,我想了解 SWING!

PPS.:

|Geben sie bitte die Kriterien für die Suche an:  
|Name:            (textfield)  
|Maximaler Preis: (textfield)  
|Alter des Kunden:(textfield)  
|Kategorie:       (Combo)  
|                 (Button)  
|Table....

除了上面的标签和桌子,一切都在正确的地方

4

1 回答 1

1

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

        }
    }
}
于 2012-10-08T09:41:39.107 回答