-3

コンポーネントの位置を設定する方が簡単だと思うので、アプリに GridBagLayout を使用しています。しかし、私は幅に問題があります。OKボタンを見る

OKボタンを見る

そしてコード:

public LoginForm1 (){
    getContentPane().setLayout(new BorderLayout(0,0));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setExtendedState(MAXIMIZED_BOTH);
    this.setBounds(100, 100, 800, 600);

    JPanel MainPanel = new JPanel();    
    getContentPane().add(MainPanel, BorderLayout.CENTER);
    GridBagLayout GridPanel = new GridBagLayout();
    GridPanel.columnWidths = new int[] {0,0,0,0};
    GridPanel.rowHeights = new int[] {0,0,0,0,0};
    GridPanel.columnWeights = new double[] {0.0, 1.0, 1.0, 0.0};
    GridPanel.rowWeights = new double[] {1.0, 0.1, 0.1, 1.0, 0.1};
    MainPanel.setLayout(GridPanel);

    JLabel userLabel = new JLabel("Username : ");
    userLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
    GridBagConstraints userLabelGrid = new GridBagConstraints();
    userLabelGrid.anchor = GridBagConstraints.EAST;
    userLabelGrid.insets = new Insets(5, 5, 5, 5);
    userLabelGrid.gridx = 1;
    userLabelGrid.gridy = 1;
    MainPanel.add(userLabel, userLabelGrid);

    JLabel passLabel = new JLabel("Password : ");
    passLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
    GridBagConstraints passLabelGrid = new GridBagConstraints();
    passLabelGrid.anchor = GridBagConstraints.EAST;
    passLabelGrid.insets = new Insets(5, 5, 5, 5);
    passLabelGrid.gridx = 1;
    passLabelGrid.gridy = 2;
    MainPanel.add(passLabel, passLabelGrid);

    JTextField userField = new JTextField();
    userField.setFont(UIManager.getFont("TextField.font"));
    GridBagConstraints userFieldGrid = new GridBagConstraints();
    userFieldGrid.fill = GridBagConstraints.VERTICAL;
    userFieldGrid.insets = new Insets(5, 5, 0, 0);
    userFieldGrid.anchor = GridBagConstraints.WEST;
    userFieldGrid.gridx = 2;
    userFieldGrid.gridy = 1;
    userField.setColumns(30);
    MainPanel.add(userField, userFieldGrid);

    JTextField passField = new JTextField();
    passField.setFont(UIManager.getFont("PasswordField.font"));
    GridBagConstraints passFieldGrid = new GridBagConstraints();
    passFieldGrid.fill = GridBagConstraints.VERTICAL;
    passFieldGrid.insets = new Insets(5, 5, 0, 0);
    passFieldGrid.anchor = GridBagConstraints.WEST;
    passFieldGrid.gridx = 2;
    passFieldGrid.gridy = 2;
    passField.setColumns(30);
    MainPanel.add(passField, passFieldGrid);

    JButton okButton = new JButton();
    GridBagConstraints okButtonGrid = new GridBagConstraints();
    okButtonGrid.fill = GridBagConstraints.BOTH;
    okButtonGrid.insets = new Insets(5, 5, 0, 0);
    okButtonGrid.gridx = 1;
    okButtonGrid.gridy = 4;
    okButton.setText("OK");
    MainPanel.add(okButton, okButtonGrid);

    JButton cancelButton = new JButton();
    GridBagConstraints cancelButtonGrid = new GridBagConstraints();
    cancelButtonGrid.fill = GridBagConstraints.BOTH;
    cancelButtonGrid.insets = new Insets(5, 5, 0, 5);
    cancelButtonGrid.gridx = 2;
    cancelButtonGrid.gridy = 4;
    cancelButton.setText("CANCEL");
    MainPanel.add(cancelButton, cancelButtonGrid);
}

私は何が欠けていますか?? 助けてください、事前に感謝します:)

4

2 に答える 2

2

両方のボタンの を削除する.fill = GridBagConstraints.BOTHと、ボタンがセルの幅 (および高さ) いっぱいに拡大するのを防ぐことができます...

ここに画像の説明を入力

また、お客様のニーズを満たしていることを確認するために、お客様の情報GridPanel.columnWeightsも確認します...GridPanel.rowWeights

の性質上、GridBagLayout実際には一連のパネルを使用して個々の要素 (フィールドとボタン) を個別にレイアウトしたほうがよいでしょう。これにより、レイアウトをより適切に制御できます...

ここに画像の説明を入力

public class LoginPane extends JPanel {

    public LoginPane() {
        setLayout(new BorderLayout(0, 0));
        JPanel mainPanel = new JPanel();

        add(mainPanel, BorderLayout.CENTER);

        mainPanel.setLayout(new GridBagLayout());
        JLabel userLabel = new JLabel("Username : ");

        userLabel.setFont(new Font("Tahoma", Font.BOLD, 20));
        GridBagConstraints userLabelGrid = new GridBagConstraints();
        userLabelGrid.anchor = GridBagConstraints.EAST;
        userLabelGrid.insets = new Insets(5, 5, 5, 5);
        userLabelGrid.gridx = 1;
        userLabelGrid.gridy = 1;

        mainPanel.add(userLabel, userLabelGrid);
        JLabel passLabel = new JLabel("Password : ");

        passLabel.setFont(
                        new Font("Tahoma", Font.BOLD, 20));
        GridBagConstraints passLabelGrid = new GridBagConstraints();
        passLabelGrid.anchor = GridBagConstraints.EAST;
        passLabelGrid.insets = new Insets(5, 5, 5, 5);
        passLabelGrid.gridx = 1;
        passLabelGrid.gridy = 2;

        mainPanel.add(passLabel, passLabelGrid);
        JTextField userField = new JTextField();

        userField.setFont(UIManager.getFont("TextField.font"));
        GridBagConstraints userFieldGrid = new GridBagConstraints();
        userFieldGrid.fill = GridBagConstraints.VERTICAL;
        userFieldGrid.insets = new Insets(5, 5, 0, 0);
        userFieldGrid.anchor = GridBagConstraints.WEST;
        userFieldGrid.gridx = 2;
        userFieldGrid.gridy = 1;

        userField.setColumns(
                        30);
        mainPanel.add(userField, userFieldGrid);
        JTextField passField = new JTextField();

        passField.setFont(UIManager.getFont("PasswordField.font"));
        GridBagConstraints passFieldGrid = new GridBagConstraints();
        passFieldGrid.fill = GridBagConstraints.VERTICAL;
        passFieldGrid.insets = new Insets(5, 5, 0, 0);
        passFieldGrid.anchor = GridBagConstraints.WEST;
        passFieldGrid.gridx = 2;
        passFieldGrid.gridy = 2;

        passField.setColumns(30);
        mainPanel.add(passField, passFieldGrid);
        JButton okButton = new JButton();

        JPanel buttonPane = new JPanel(new GridBagLayout());

        GridBagConstraints okButtonGrid = new GridBagConstraints();
        okButtonGrid.insets = new Insets(5, 5, 0, 0);
        okButtonGrid.gridx = 0;
        okButtonGrid.gridy = 0;

        okButton.setText("OK");
        buttonPane.add(okButton, okButtonGrid);

        JButton cancelButton = new JButton();
        GridBagConstraints cancelButtonGrid = new GridBagConstraints();
        cancelButtonGrid.insets = new Insets(5, 5, 0, 5);
        cancelButtonGrid.gridx = 1;
        cancelButtonGrid.gridy = 0;

        cancelButton.setText("CANCEL");
        buttonPane.add(cancelButton, cancelButtonGrid);

        add(buttonPane, BorderLayout.SOUTH);

    }
}

ps

「OKボタンを見てください」は何の意味もありません...ボタンは、あなたがそうあるべきだと述べたとおりに配置されています...

于 2013-04-27T21:25:51.037 に答える
0
 cancelButtonGrid.fill = GridBagConstraints.BOTH; 

GridBagConstraints.BOTHは、コンポーネントを水平方向および垂直方向にストレッチして、セルの幅全体と高さ全体を埋めます

于 2013-04-27T21:11:39.050 に答える