这是具有主要方法的类:
import javax.swing.JFrame;
class apples{
public static void main(String args[]){
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(180,200);
go.setVisible(true);
}
}
这是包含我的程序实际代码的类:
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Icon;
import javax.swing.ImageIcon;
public class Gui extends JFrame{
JButton n1 = new JButton("1");
JButton n2 = new JButton("2");
JButton n3 = new JButton("3");
JButton n4 = new JButton("4");
JButton n5 = new JButton("5");
JButton n6 = new JButton("6");
JButton n7 = new JButton("7");
JButton n8 = new JButton("8");
JButton n9 = new JButton("9");
JTextField username = new JTextField("username");
JPasswordField password = new JPasswordField("password");
JButton play;
public Gui(){
super("Security Passage");
setLayout(new FlowLayout());
add(username);
add(password);
add(n1);
add(n2);
add(n3);
add(n4);
add(n5);
add(n6);
add(n7);
add(n8);
add(n9);
Icon dec = new ImageIcon(getClass().getResource("faceb.png"));
play = new JButton("Login", dec);
add(play);
HandlerClass handler = new HandlerClass();
username.addActionListener(handler);
password.addActionListener(handler);
n1.addActionListener(handler);
n2.addActionListener(handler);
n3.addActionListener(handler);
n4.addActionListener(handler);
n5.addActionListener(handler);
n6.addActionListener(handler);
n7.addActionListener(handler);
n8.addActionListener(handler);
n9.addActionListener(handler);
play.addActionListener(handler);
}
private class HandlerClass implements ActionListener{
public void actionPerformed(ActionEvent e){
String cUser = "";
String cPass = "";
int cCode[] = {10,10,10};
String CodeCheck = String.format("%d%d%d", cCode[0], cCode[1], cCode[2]);
if(e.getSource() == username){
cUser = String.format("%s",e.getActionCommand());
}else if(e.getSource() == password){
cPass = String.format("%s",e.getActionCommand());
}else if(e.getSource() == n1){
if(cCode[0] == 10){
cCode[0] = Integer.parseInt(e.getActionCommand());
}else if(cCode[1] == 10){
cCode[1] = Integer.parseInt(e.getActionCommand());
}else if(cCode[2] == 10){
cCode[2] = Integer.parseInt(e.getActionCommand());
}
}else if(e.getSource() == n2){
if(cCode[0] == 10){
cCode[0] = Integer.parseInt(e.getActionCommand());
}else if(cCode[1] == 10){
cCode[1] = Integer.parseInt(e.getActionCommand());
}else if(cCode[2] == 10){
cCode[2] = Integer.parseInt(e.getActionCommand());
}
}else if(e.getSource() == n3){
if(cCode[0] == 10){
cCode[0] = Integer.parseInt(e.getActionCommand());
}else if(cCode[1] == 10){
cCode[1] = Integer.parseInt(e.getActionCommand());
}else if(cCode[2] == 10){
cCode[2] = Integer.parseInt(e.getActionCommand());
}
}else if(e.getSource() == n4){
if(cCode[0] == 10){
cCode[0] = Integer.parseInt(e.getActionCommand());
}else if(cCode[1] == 10){
cCode[1] = Integer.parseInt(e.getActionCommand());
}else if(cCode[2] == 10){
cCode[2] = Integer.parseInt(e.getActionCommand());
}
}else if(e.getSource() == n5){
if(cCode[0] == 10){
cCode[0] = Integer.parseInt(e.getActionCommand());
}else if(cCode[1] == 10){
cCode[1] = Integer.parseInt(e.getActionCommand());
}else if(cCode[2] == 10){
cCode[2] = Integer.parseInt(e.getActionCommand());
}
}else if(e.getSource() == n6){
if(cCode[0] == 10){
cCode[0] = Integer.parseInt(e.getActionCommand());
}else if(cCode[1] == 10){
cCode[1] = Integer.parseInt(e.getActionCommand());
}else if(cCode[2] == 10){
cCode[2] = Integer.parseInt(e.getActionCommand());
}
}else if(e.getSource() == n7){
if(cCode[0] == 10){
cCode[0] = Integer.parseInt(e.getActionCommand());
}else if(cCode[1] == 10){
cCode[1] = Integer.parseInt(e.getActionCommand());
}else if(cCode[2] == 10){
cCode[2] = Integer.parseInt(e.getActionCommand());
}
}else if(e.getSource() == n8){
if(cCode[0] == 10){
cCode[0] = Integer.parseInt(e.getActionCommand());
}else if(cCode[1] == 10){
cCode[1] = Integer.parseInt(e.getActionCommand());
}else if(cCode[2] == 10){
cCode[2] = Integer.parseInt(e.getActionCommand());
}
}else if(e.getSource() == n9){
if(cCode[0] == 10){
cCode[0] = Integer.parseInt(e.getActionCommand());
}else if(cCode[1] == 10){
cCode[1] = Integer.parseInt(e.getActionCommand());
}else if(cCode[2] == 10){
cCode[2] = Integer.parseInt(e.getActionCommand());
}
}else if(e.getSource() == play){
if(cPass == "Skyfall" && CodeCheck == "123"){
JOptionPane.showMessageDialog(null, cUser + ", you have cracked the security system");
}else{
JOptionPane.showMessageDialog(null, "No Access Granted" + cUser);
JOptionPane.showMessageDialog(null, CodeCheck);
JOptionPane.showMessageDialog(null, cPass);
JOptionPane.showMessageDialog(null, cUser);
}
}
}
}
}
这是我自己使用Eclipse编写的程序,但是我不明白为什么cUser,cPass和CodeCheck在“动作”发生时不会改变它们的值(单击弹出窗口中的按钮) 。此外,我认为必须有一种更有效的方式来编写这些代码,我真的很感激这两个问题的任何建议和帮助。
提前谢谢!