0

私は現在、製品システムをコーディングしており、インターフェイスを設計しています。私の問題は、インターフェイスが列にないことです。ここに画像の説明を入力私はそれを自分のコードコードと一緒に 1 つの列に入れたいと思っています。また、「suche nach waren」の行にボタンをもう 1 つ追加するとよいでしょう。コードは次のとおりです。

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 の例...

ここに画像の説明を入力

あなたは本当にJTableJScrollPane の中に置くべきです、それはあなたのためにヘッダーを処理しますが、干し草

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 に答える