【问题标题】:How to create a lock with user input on JPanel?如何在 JPanel 上使用用户输入创建锁?
【发布时间】:2017-05-11 21:51:02
【问题描述】:

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

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

我的代码有两个问题。它们都是“语法错误,插入“}”以完成ClassBody”,但每次我添加一个时,它都会显示“令牌“}”上的语法错误,删除这个令牌”

这是我的代码:

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);





        }
       });
}

【问题讨论】:

  • " However something on Eclipse is saying that something is wrong and that I can't run it" -- 最适合您发布任何和所有错误消息。
  • 感谢您的建议!我试图发布我所做的事情和发生的事情。
  • 投票结束问题,因为它只不过是一个印刷错误。我不得不怀疑你是否本末倒置——在处理 GUI 之前学习 Java 的基础知识——你不会后悔这样做。
  • 也感谢您的快速回复! :D

标签: java eclipse jpanel jbutton


【解决方案1】:
loginButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        //...
    }

    );

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

它应该看起来更像

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

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

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

【讨论】:

  • 我试过这样做,但出现了一个问题。 “语法错误,插入“;”以完成语句”在此行:loginButton1.addActionListener(new ActionListener()
  • 那你做错了。我所做的正是我上面写的,它解决了第一个问题
  • 其实你是对的! nvm,问题已解决。谢谢!
【解决方案2】:

感谢您帮助我解决问题。最后我能够修复它,它现在可以工作了。这里是:

 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);

                }

        });
        }
    });
}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2017-06-16
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多