1

私はJavaのGUIを初めて使用し、添付した画像のようなGUIを作成しようとしていますが、複数のJLabelとJTextFieldを使用しています。画面下部の中央にボタンを配置したいのですが。

これを行うにはどうすればよいですか?より良い画像が見つからなかったので、黒い線は無視してください。

ここに画像の説明を入力してください

4

4 に答える 4

6

There are a number of ways you could achieve this. For me, the easiest way is to use GridBagLayout, it's the most flexible of all the built in layout managers, it is also one of the most complicated :P

One thing that a lot of people get caught up on, is trying to layout there components on a single container. This can be done, but takes a lot of time and tweaking to get just right.

A better approach (IMHO) is to use multiple, child containers.

This allows you to break down the layout requirements to there individual needs and worry about the relationship between these containers as a separate issue.

For example, in you question, you have a requirement that the word and text field be aligned together, but that they be anchored to the north position of the container and the button should be to the south, but centered between the label and the field.

Worry about how to layout the label and field first, then worry about the relationship between these button and the field/label second...

enter image description here

public class TestLayout08 {

    public static void main(String[] args) {
        new TestLayout08();
    }

    public TestLayout08() {
        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 LayoutPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    protected  class LayoutPane extends JPanel {

        public LayoutPane() {
            JButton button = new JButton("Find Word");
            JLabel label = new JLabel("Word:");
            JTextField field = new JTextField(12);

            setLayout(new GridBagLayout());

            JPanel fieldPane = new JPanel(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.insets = new Insets(2, 2, 2, 2);

            fieldPane.add(label, gbc);
            gbc.gridx++;
            fieldPane.add(field, gbc);

            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.NORTH;
            gbc.weighty = 1;
            add(fieldPane, gbc);

            gbc.gridx = 0;
            gbc.gridy++;
            gbc.weighty = 0;
            gbc.anchor = GridBagConstraints.CENTER;

            add(button, gbc);    
        }        
    }
}

Take a look at;

于 2012-10-12T22:17:14.550 に答える
2

少なくとも2つのオプションがあります。

  • NetBeansやEclipseなどのお気に入りのIDEのGUIビルダーを使用する
  • SwingAPIの使用方法を学びます。特に、LayoutManagerの使用方法を学ぶ必要があります。この特定の例では、BorderLayout、GridLayoutまたはGridBagLayout、およびFlowLayoutの組み合わせを使用することができます。
于 2012-10-12T22:20:37.710 に答える
1

If you'd rather use Eclipse, there is a plugin called WindowsBuilder Pro, you can actually just install the plugin right in Eclipse. https://developers.google.com/java-dev-tools/wbpro/

于 2012-10-12T22:16:55.513 に答える
0

Try using Netbeans. You can drag and drop all these features and then you just need to program the code for the functions of the buttons. The classes get created for you too.

Alternatively, I'd start from the beginning, learn how to build it from scratch, considering it's a very basic GUI.

http://thenewboston.org/tutorials.php

于 2012-10-12T22:10:58.910 に答える