无法在Swing

时间:2016-12-13 07:34:33

标签: java swing

我刚开始用一个简单的代码学习Swing来创建登录表单。

package swingbeginner;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginForm {
    private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel inputLabel;
    private JPanel inputPanel;
    private JPanel controlPanel;
    private JLabel statusLabel;

    public LoginForm() {
        prepareGUI();
    }

    public static void main(String[] args) {
        LoginForm loginForm = new LoginForm();
        loginForm.loginProcess();
    }

    private void prepareGUI() {
        mainFrame = new JFrame("Login");
        mainFrame.setSize(600, 600);
        mainFrame.setLayout(new FlowLayout());

        headerLabel = new JLabel("",JLabel.CENTER );
        statusLabel = new JLabel("",JLabel.CENTER);   

        statusLabel.setSize(350,100);

        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {

            }
        });

        inputLabel = new JLabel();
        inputLabel.setLayout(null);

        inputPanel = new JPanel();
        inputPanel.setLayout(null);

        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout());

        mainFrame.add(headerLabel);
        mainFrame.add(inputLabel);
        mainFrame.add(inputPanel);
        mainFrame.add(controlPanel);
        mainFrame.add(statusLabel);
        mainFrame.setVisible(true);
    }

    private void loginProcess() {
        headerLabel.setText("Please Login to Continue!");

        JLabel usernameLabel = new JLabel("Username");
        usernameLabel.setBounds(10,20,80,25);
        JLabel passwordLabel = new JLabel("Password");
        passwordLabel.setBounds(10, 20, 80, 25);

        JTextField usernameTextbox = new JTextField();
        usernameTextbox.setBounds(100,20,165,25);
        JPasswordField passwordTextbox = new JPasswordField();
        passwordTextbox.setBounds(100,20,165,25);

        JButton loginButton = new JButton("Login");
        JButton cancelButton = new JButton("Cancel");

        loginButton.setActionCommand("Login");
        cancelButton.setActionCommand("Cancel");

        loginButton.addActionListener(new ButtonClickListener());
        cancelButton.addActionListener(new ButtonClickListener());

        inputLabel.add(usernameLabel);
        inputPanel.add(usernameTextbox);
        inputLabel.add(passwordLabel);
        inputPanel.add(passwordTextbox);

        controlPanel.add(loginButton);
        controlPanel.add(cancelButton);

        mainFrame.setVisible(true);     
    }


    private class ButtonClickListener implements ActionListener {
        public void actionPerformed(ActionEvent actionEvent) {
            String command = actionEvent.getActionCommand();
            if(command.equals("Login")) {
                statusLabel.setText("Logging In");
            }
            else if(command.equals("Cancel")) {
                statusLabel.setText("Login Cancelled");
            }
        }
    }

}

我的代码显示标题以及“登录和取消”按钮。但标签/文本字段(用户名和密码)未显示在面板中。

我哪里错了?

4 个答案:

答案 0 :(得分:3)

因为每个人都错过了null布局的事实,所以我自己创建一个答案。

inputPanel.setLayout(null);

如果您向此添加组件,则必须指定位置,或者只使用BorderLayoutFlowLayout等布局管理器。

inputPanel.setLayout(new FlowLayout());

如果您使用此功能,您只需将组件添加到JPanel即可。另外,如其他答案中所述,请勿将JLabels添加到其他JLables,因为最顶层的人会覆盖其他人public class LoginForm { private JFrame mainFrame; private JLabel headerLabel; private JPanel inputPanel; private JPanel controlPanel; private JLabel statusLabel; public LoginForm() { prepareGUI(); } public static void main(String[] args) { LoginForm loginForm = new LoginForm(); loginForm.loginProcess(); } private void prepareGUI() { mainFrame = new JFrame("Login"); mainFrame.setSize(600, 600); mainFrame.setLayout(new FlowLayout()); headerLabel = new JLabel("",JLabel.CENTER ); statusLabel = new JLabel("",JLabel.CENTER); statusLabel.setSize(350,100); mainFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent) { } }); //changes here inputPanel = new JPanel(); inputPanel.setLayout(new FlowLayout()); controlPanel = new JPanel(); controlPanel.setLayout(new FlowLayout()); mainFrame.add(headerLabel); mainFrame.add(inputLabel); mainFrame.add(inputPanel); mainFrame.add(controlPanel); mainFrame.add(statusLabel); mainFrame.setVisible(true); } private void loginProcess() { headerLabel.setText("Please Login to Continue!"); JLabel usernameLabel = new JLabel("Username"); usernameLabel.setBounds(10,20,80,25); JLabel passwordLabel = new JLabel("Password"); passwordLabel.setBounds(10, 20, 80, 25); JTextField usernameTextbox = new JTextField(); usernameTextbox.setBounds(100,20,165,25); JPasswordField passwordTextbox = new JPasswordField(); passwordTextbox.setBounds(100,20,165,25); JButton loginButton = new JButton("Login"); JButton cancelButton = new JButton("Cancel"); loginButton.setActionCommand("Login"); cancelButton.setActionCommand("Cancel"); loginButton.addActionListener(new ButtonClickListener()); cancelButton.addActionListener(new ButtonClickListener()); inputPanel.add(usernameLabel); //changes here inputPanel.add(usernameTextbox); inputPanel.add(passwordLabel); //changes here inputPanel.add(passwordTextbox); controlPanel.add(loginButton); controlPanel.add(cancelButton); mainFrame.setVisible(true); } private class ButtonClickListener implements ActionListener { public void actionPerformed(ActionEvent actionEvent) { String command = actionEvent.getActionCommand(); if(command.equals("Login")) { statusLabel.setText("Logging In"); } else if(command.equals("Cancel")) { statusLabel.setText("Login Cancelled"); } } } } 。有了这个说,一个应该工作的解决方案代码将是这样的:

{{1}}

答案 1 :(得分:0)

您正在将usernameLabel和passwordLabel添加到另一个标签inputLabel上。 [堆叠标签时,最顶层的标签将覆盖,这些标签将消失。](JLabel on top of another JLabel)!为什么不直接将所有标签和字段添加到mainFrame,即JFrame对象?

答案 2 :(得分:0)

以下是您的解决方案:

package swingbeginner;

public class LoginForm {

    private JFrame mainFrame;

    private JLabel headerLabel;

    private JLabel inputLabel;

    private JPanel inputPanel;

    private JPanel controlPanel;

    private JLabel statusLabel;

    public LoginForm() {
        prepareGUI();
    }

    public static void main(String[] args) {

        LoginForm loginForm = new LoginForm();
        loginForm.loginProcess();
    }

    private void prepareGUI() {

        mainFrame = new JFrame("Login");
        mainFrame.setSize(600, 600);
        mainFrame.setLayout(new BorderLayout());

        headerLabel = new JLabel("header", JLabel.CENTER);
        statusLabel = new JLabel("status", JLabel.CENTER);

        statusLabel.setSize(350, 100);

        mainFrame.addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent windowEvent) {

            }
        });

        controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 100));

        mainFrame.add(headerLabel, BorderLayout.NORTH);
        mainFrame.add(controlPanel, BorderLayout.CENTER);
        mainFrame.add(statusLabel, BorderLayout.SOUTH);
        mainFrame.setVisible(true);
    }

    private void loginProcess() {

        headerLabel.setText("Please Login to Continue!");

        JLabel usernameLabel = new JLabel("Username");
        usernameLabel.setBounds(10, 20, 80, 25);
        JLabel passwordLabel = new JLabel("Password");
        passwordLabel.setBounds(10, 20, 80, 25);

        JTextField usernameTextbox = new JTextField(20);
        usernameTextbox.setBounds(100, 20, 165, 25);
        JPasswordField passwordTextbox = new JPasswordField(20);
        passwordTextbox.setBounds(100, 20, 165, 25);

        JButton loginButton = new JButton("Login");
        JButton cancelButton = new JButton("Cancel");

        loginButton.setActionCommand("Login");
        cancelButton.setActionCommand("Cancel");

        loginButton.addActionListener(new ButtonClickListener());
        cancelButton.addActionListener(new ButtonClickListener());

        controlPanel.add(usernameLabel);
        controlPanel.add(usernameTextbox);
        controlPanel.add(passwordLabel);
        controlPanel.add(passwordTextbox);
        controlPanel.add(loginButton);
        controlPanel.add(cancelButton);

        mainFrame.setVisible(true);
    }

    private class ButtonClickListener implements ActionListener {

        public void actionPerformed(ActionEvent actionEvent) {

            String command = actionEvent.getActionCommand();
            if (command.equals("Login")) {
                statusLabel.setText("Logging In");
            }
            else if (command.equals("Cancel")) {
                statusLabel.setText("Login Cancelled");
            }
        }
    }
}

答案 3 :(得分:-1)

您正在向inputLabel添加标签/文本字段(用户名和密码),这是一个不属于容器类型的JLabel实例。

更改

inputLabel.add(usernameLabel);
inputPanel.add(usernameTextbox);
inputLabel.add(passwordLabel);
inputPanel.add(passwordTextbox);

controlPanel.add(usernameLabel);
controlPanel.add(usernameTextbox);
controlPanel.add(passwordLabel);
controlPanel.add(passwordTextbox);

同时为JTextField提供默认大小,因此请更改

JTextField usernameTextbox = new JTextField();
JPasswordField passwordTextbox = new JPasswordField();

JTextField usernameTextbox = new JTextField(20);
JPasswordField passwordTextbox = new JPasswordField(20);

或者您可以将inputLabel变量的类型更改为JPanel,并将其设置为类似FlowLayout的布局。 在这种情况下,不需要在组件上使用setBounds方法。

注意:您对多个组件使用相同的边界,这将使它们成为一个在另一个之上。