如何在 JPanel 上使用用户输入创建锁?
How to create a lock with user input on JPanel?
我是 Java 的初学者,所以如果我的代码中有一些简单的问题,请告诉我。提前致谢!
在 Java 中,我有一个 "login" JPanel,我希望用户输入他们喜欢的密码,然后让他们在另一个 JPanel 上输入密码(与他们创建的密码相同) .如果它是正确的,并且他们再次单击登录按钮,那么它将把他们带到一个显示 "Welcome" 的屏幕,如果他们不正确,然后是一个显示 "False. Error" 的屏幕。然而,Eclipse 上的一些东西说出了点问题,我不能 运行 它。请告诉我我哪里出错了,如果你能告诉我如何解决它。我将不胜感激!
我的代码有两个问题。它们都是"Syntax error, insert "}“完成ClassBody”,但每次我添加一个时,它都会说"Syntax error on token "}”,删除这个标记
这是我的代码:
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);
}
});
}
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//...
}
);
缺少闭合拥抱 (}
) - 您将学习做的一件事是计算括号和大括号
应该更像
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//...
}
});
registerButton
和 loginButton1
未定义,您似乎也缺少文件末尾的右大括号 (}
)
我还强烈建议您使用布局管理器,它们会让您的生活更轻松。我建议从 How to use CardLayout 开始,因为它可以让您简单地在多个视图之间切换
感谢您帮我解决问题。最后我能够修复它,它现在可以工作了。这是:
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);
}
});
}
});
}
}
我是 Java 的初学者,所以如果我的代码中有一些简单的问题,请告诉我。提前致谢!
在 Java 中,我有一个 "login" JPanel,我希望用户输入他们喜欢的密码,然后让他们在另一个 JPanel 上输入密码(与他们创建的密码相同) .如果它是正确的,并且他们再次单击登录按钮,那么它将把他们带到一个显示 "Welcome" 的屏幕,如果他们不正确,然后是一个显示 "False. Error" 的屏幕。然而,Eclipse 上的一些东西说出了点问题,我不能 运行 它。请告诉我我哪里出错了,如果你能告诉我如何解决它。我将不胜感激!
我的代码有两个问题。它们都是"Syntax error, insert "}“完成ClassBody”,但每次我添加一个时,它都会说"Syntax error on token "}”,删除这个标记
这是我的代码:
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);
}
});
}
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//...
}
);
缺少闭合拥抱 (}
) - 您将学习做的一件事是计算括号和大括号
应该更像
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//...
}
});
registerButton
和 loginButton1
未定义,您似乎也缺少文件末尾的右大括号 (}
)
我还强烈建议您使用布局管理器,它们会让您的生活更轻松。我建议从 How to use CardLayout 开始,因为它可以让您简单地在多个视图之间切换
感谢您帮我解决问题。最后我能够修复它,它现在可以工作了。这是:
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);
}
});
}
});
}
}