如何在JPanel上使用用户输入创建锁?

时间:2017-05-11 21:51:02

标签: java eclipse jpanel jbutton

我是Java的初学者,所以如果我的代码中有一些简单的问题请告诉我。提前谢谢!

在Java中我有一个"登录" JPanel,我希望用户输入他们喜欢的密码,然后让他们在另一个JPanel(他们创建的密码相同)上输入密码。如果它是正确的,他们再次点击登录按钮,那么它将把他们带到一个屏幕上,上面写着"欢迎"如果他们不这样做,那么屏幕会显示" False。错误&#34 ;.但是Eclipse上有些东西说错了,我无法运行它。请告诉我哪里出错了,如果你能告诉我如何解决它。我很感激!

我的代码有两个问题。它们都是"语法错误,插入"}"完成ClassBody"但每次我添加一个,它会说'#34;令牌上的语法错误"}",删除此令牌"

这是我的代码:

package Button;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

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

import java.util.Scanner;

@SuppressWarnings("unused")
public class Login {

public static void main(String[] args) {
    JFrame frame = new JFrame("Login Page");
    frame.setSize(300, 150);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    frame.add(panel);
    placeComponents(panel);

    frame.setVisible(true);

}


private static void placeComponents(JPanel panel) {

    panel.setLayout(null);

    JLabel userLabel = new JLabel("Pasword");
    userLabel.setBounds(10, 10, 80, 25);
    panel.add(userLabel);

    JTextField userText = new JTextField(20);
    userText.setBounds(100, 10, 160, 25);
    panel.add(userText);





    JButton loginButton = new JButton("Create");
    loginButton.setBounds(10, 80, 80, 25);
    panel.add(loginButton);


@SuppressWarnings("resource")
    Scanner user = new Scanner (System.in);

    loginButton.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e) {

            loginButton.setVisible(false);

            registerButton.setVisible(false);

            userText.setVisible(false);

            userLabel.setVisible(false);

            JTextField userText1 = new JTextField(20);
            userText.setBounds(100, 10, 160, 25);
            panel.add(userText1);

            JButton loginButton1 = new JButton("Password");
            loginButton1.setBounds(10, 80, 80, 25);
            panel.add(loginButton1);



        });



         loginButton1.addActionListener(new ActionListener() 
        {
            public void actionPerformed(ActionEvent e) {            

                String name = userText.getText();
                String accept = name;
                String good;        

        if (accept.equals(name)) {
            good = "Welcome";

        } else {
            good = "False. Error";
        }               


        JLabel label1 = new JLabel(good);
        label1.setBounds(100, 40, 100, 100);
        label1.setVisible(true);


        panel.add(label1);





        }
       });
}

2 个答案:

答案 0 :(得分:1)

loginButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        //...
    }

    );

缺少一个结束的拥抱(}) - 你要学会做的一件事是数括号和大括号

看起来应该更像

loginButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        //...
    }
});

registerButtonloginButton1未定义,您似乎也缺少文件末尾的大括号(}

我还强烈建议您使用布局管理器,它们将使您的生活更轻松。我建议从How to use CardLayout开始,因为它允许您在多个视图之间切换

答案 1 :(得分:0)

感谢您帮助我解决问题。最后,我能够解决它现在有效。这是:

 package Button;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;

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

import java.util.Scanner;

@SuppressWarnings("unused")
public class AddOnButton {

public static void main(String[] args) {
    JFrame frame = new JFrame("Login Page");
    frame.setSize(300, 150);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    frame.add(panel);
    placeComponents(panel);

    frame.setVisible(true);

}


private static void placeComponents(JPanel panel) {

    panel.setLayout(null);

    JLabel userLabel = new JLabel("Password");
    userLabel.setBounds(10, 10, 80, 25);
    panel.add(userLabel);

    JTextField userText = new JTextField(20);
    userText.setBounds(100, 10, 160, 25);
    panel.add(userText);

    JButton loginButton = new JButton("Create");
    loginButton.setBounds(10, 80, 80, 25);
    panel.add(loginButton);

    String name = userText.getText();

    loginButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            loginButton.setVisible(false);

            userText.setVisible(false);

            userLabel.setVisible(false);

            JTextField userText1 = new JTextField(20);
            userText1.setBounds(100, 10, 160, 25);
            panel.add(userText1);

            JLabel userLabel1 = new JLabel("Password");
            userLabel1.setBounds(10, 10, 80, 25);
            panel.add(userLabel1);

            JButton loginButton1 = new JButton("Enter");
            loginButton1.setBounds(10, 80, 80, 25);
            panel.add(loginButton1);

            String check = userText1.getText();     

        loginButton1.addActionListener(new ActionListener() {           
            public void actionPerformed(ActionEvent e) {            

            userText1.setVisible(false);

            userLabel1.setVisible(false);

            loginButton1.setVisible(false);

        String good;        

        if (name.equals(check)) {
            good = "Welcome";

        } else {
            good = "False. Error";
        }               


        JLabel label1 = new JLabel(good);
        label1.setBounds(100, 40, 100, 100);
        label1.setVisible(true);


        panel.add(label1);

                }

        });
        }
    });
}
}