Java Swing - 如何堆叠用户名文本字段和密码文本字段

时间:2017-06-09 02:54:23

标签: java swing layout-manager

我使用gridbaglayout进行布局,我想知道如何重新排列它,以便用户名文本字段直接位于密码文本字段上方,登录按钮位于密码文本字段下方。

目前我的代码输出: enter image description here

但我想要这样的事情:

enter image description here

我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TestingJavaCode {

    public TestingJavaCode() {

        JFrame appFrame = new JFrame();
        JPanel loginPanel = new JPanel();
        appFrame.setSize(1200, 800);
        loginPanel = new JPanel(new GridBagLayout());


        // Button for logging in, with respective GridBagConstraint
        // setFocusable is set to false to take out the border around the text
        JButton loginButton = new JButton("Login");
        GridBagConstraints lButtonC = new GridBagConstraints();
        loginButton.setFocusable(false);


        // Username text-field and JLabel with respective GridBagConstraints
        JTextField tfUsername = new JTextField(15);
        GridBagConstraints tfUserC = new GridBagConstraints();
        JLabel txtUser = new JLabel("Username: ");
        GridBagConstraints txtUserC = new GridBagConstraints();


        // Password text-field and JLabel with respective GridBagConstraints
        JPasswordField tfPassword = new JPasswordField(15);
        GridBagConstraints tfPassC = new GridBagConstraints();
        JLabel txtPassword = new JLabel("Password: ");
        GridBagConstraints txtPassC = new GridBagConstraints();

        // Add all components to the JFrame
        // Making sure to add the text before the text-fields
        loginPanel.add(txtUser, txtUserC);
        loginPanel.add(tfUsername, tfUserC);
        loginPanel.add(txtPassword, txtPassC);
        loginPanel.add(tfPassword, tfPassC);
        loginPanel.add(loginButton, lButtonC);

        // Show the frame
        appFrame.add(loginPanel);
        appFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        appFrame.setVisible(true);
    }


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

}

编辑:我愿意使用其他布局管理器。

1 个答案:

答案 0 :(得分:3)

由于您不想使用gridx / y约束,为什么不使用BoxLayoutFlowLayout的组合?我添加了新的2个新面板。一个用于用户名,一个用于密码,因为FlowLayout是面板的默认布局,我们可以保持原样。

  

FlowLayout是每个JPanel的默认布局管理器。它很简单   将组件布置在一行中,如果是,则开始一个新行   容器不够宽。

之后,让BoxLayout设置为您的loginPanel,只需添加用户名和密码面板即可。

  

BoxLayout类将组件放在一行或一列中。它   尊重组件要求的最大尺寸,也可以让您   对齐组件。

以下是完整的更新来源:

import javax.swing.*;

public class TestingJavaCode {

    public TestingJavaCode() {

        JFrame appFrame = new JFrame();
        JPanel loginPanel = new JPanel();
        appFrame.setSize(300, 130);
        loginPanel.setLayout(new BoxLayout(loginPanel, BoxLayout.PAGE_AXIS));

        // Button for logging in, with respective GridBagConstraint
        // setFocusable is set to false to take out the border around the text
        JButton loginButton = new JButton("Login");
        loginButton.setFocusable(false);

        JPanel usernamePanel = new JPanel();
        // Username text-field and JLabel with respective GridBagConstraints
        JTextField tfUsername = new JTextField(15);
        JLabel txtUser = new JLabel("Username: ");

        usernamePanel.add(txtUser);
        usernamePanel.add(tfUsername);

        JPanel passwordPanel = new JPanel();
        // Password text-field and JLabel with respective GridBagConstraints
        JPasswordField tfPassword = new JPasswordField(15);
        JLabel txtPassword = new JLabel("Password: ");

        passwordPanel.add(txtPassword);
        passwordPanel.add(tfPassword);

        // Add all components to the JFrame
        // Making sure to add the text before the text-fields
        loginPanel.add(usernamePanel);

        loginPanel.add(usernamePanel);
        loginPanel.add(passwordPanel);
        loginPanel.add(loginButton);
        // Show the frame
        appFrame.add(loginPanel);
        appFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        appFrame.setVisible(true);
    }

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

}