我有一个带有按钮的 JPanel,按下该按钮时应该将我发送到另一个屏幕,但是我一直收到错误消息。我试着用谷歌搜索它,但找不到任何关于它的信息。
完整代码
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.JRadioButton;
import javax.swing.JPasswordField;
import javax.swing.ButtonGroup;//to put radio buttons together
public class Graphics extends JFrame {//this class is to display the GUI on the screen
private JButton enter;//enter on login
private JButton exit; //exit program
private JButton button1; //enter on menu
private JButton button2; //enter on withdraw
private JButton button3; //enter on deposit
private JButton back;//button to return to main menu from other options
private JRadioButton balance; //menu for operation selection
private JRadioButton withdraw;
private JRadioButton deposit;
private ButtonGroup buttonMenu;//to put radio buttons together
private JTextField loginBox; //to enter username
private JPasswordField password;
private JTextField input1;//to enter withdraw amount
private JTextField input2;//deposit amount
private JPanel loginPanel;//first screen
private JPanel menuPanel;
private JPanel withdrawPanel;
private JPanel depositPanel;
private JPanel balancePanel;
private JPanel setPanel; //to hold the other panels
private CardLayout cl;//to set the different panels
private int login1;
private int password1;
private double login2;
private double password2;
ATM atmObject = new ATM();
public Graphics(){ //constructor
super("My ATM");
//ATM object = new ATM();//object to call methods in ATM
//to access information stored
CardLayout cl = new CardLayout();
setPanel = new JPanel();//panel to store the other panels
setPanel.setLayout(cl);//create the different set of windows
/***define and initialize all buttons, labels etc.***/
JLabel login = new JLabel("Login"); //to put next to login field
JLabel passcode = new JLabel("Password");
loginBox = new JTextField(6);//to enter login
password = new JPasswordField(6);//to enter password
input1 = new JTextField(6);
input2 = new JTextField(6);
enter = new JButton("Enter");
back = new JButton("GO Back");
exit = new JButton("Exit");
button1 = new JButton("Enter");//button for main menu
button2 = new JButton("Enter");//button for withdraw
button3 = new JButton("Enter");//button for deposit
balance = new JRadioButton("balance inquiry", false);//menu for operation selection
withdraw = new JRadioButton("withdraw from account", false);
deposit = new JRadioButton("deposit to account", false);
/***end of definitions***/
/*****set-up login screen****/
loginPanel = new JPanel();
loginPanel.add(login);//label
loginPanel.add(loginBox);//textbox
loginPanel.add(passcode);//label
loginPanel.add(password);//input password
loginPanel.add(enter);
loginPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(loginPanel, "LoginScreen");//add login screen to panel and set
//items orientation to the left
//here add code to check password on file
//obtain password as set of characters
//int cool = Integer.parseInt(loginBox.getText()); in a single step
//String inputA = loginBox.getText();//obtain input from user as a string
//JTextField inputB = new JTextField();
//login1 = Integer.parseInt(inputA); //convert it to int so it can be used
//char[] passcode2 = password.getPassword(); //create array of chars to get password
//password1 = Integer.parseInt(String.valueOf(passcode2));//changing characters to int
/****setup main menu***/
menuPanel = new JPanel();
buttonMenu = new ButtonGroup();
menuPanel.add(balance);
menuPanel.add(withdraw);
menuPanel.add(deposit);
//make all radio buttons connected
buttonMenu.add(balance);
buttonMenu.add(withdraw);
buttonMenu.add(deposit);
menuPanel.add(button1);//add entebr button
menuPanel.add(exit);
menuPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(menuPanel, "MainMenu");//add window to display
/***setup balance inquiry screen***/
balancePanel = new JPanel();
JLabel balance = new JLabel("the balance in your account is :");
JTextField balanceDisplay = new JTextField(5);//to display balance
balanceDisplay.setEditable(false);
//here code to call balance from ATM
balancePanel.add(balance);
balancePanel.add(balanceDisplay);
balancePanel.add(back);
balancePanel.add(exit);
balancePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(balancePanel, "Blance");//add window to display
/**end of balance inquiry**/
/****setup screen to withdraw money***/
withdrawPanel = new JPanel();
JLabel message = new JLabel("enter amount to withdraw");
withdrawPanel.add(message);//display message
withdrawPanel.add(input1);
withdrawPanel.add(button2);
withdrawPanel.add(back);
withdrawPanel.add(exit);
withdrawPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
//here code to call method to withdraw money
setPanel.add(withdrawPanel);//add window to display
/***setup screen to deposit***/
depositPanel = new JPanel();
JLabel message2 = new JLabel("Enter amount to deposit");
depositPanel.add(message2);
depositPanel.add(input2);
depositPanel.add(back);
depositPanel.add(exit);
depositPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
setPanel.add(depositPanel, "Deposit");
/***end deposit panel***/
getContentPane().add(setPanel);
//create event handler and make buttons events
theHandler handler = new theHandler();//builds an action listener object
enter.addActionListener(handler);
exit.addActionListener(handler);
back.addActionListener(handler);
button1.addActionListener(handler);
button2.addActionListener(handler);
button3.addActionListener(handler);
//add the frame to the window
//boolean access = atmObject.Info(id, password1);
//int i;
// if(access == true){
//enter.addActionListener(handler);//if login is correct call the event handler
}
//class inside class
//inner class inherits everything from outer class
private class theHandler implements ActionListener{//this class handles the events
//waits for an event to happen and execute the code
public void actionPerformed(ActionEvent event){//actionPerformed is a built-in method
//ActionEvent is also built-in
//String string = "";
boolean info = false;//to check if the login and password are correct
if(event.getSource()== enter){//getSource = where the event occurred
JOptionPane.showMessageDialog(null, "Hello");
//String inputA = loginBox.getText();//obtain input from user as a string
// login1 = Integer.parseInt(inputA); //convert it to int so it can be used
//char[] passcode2 = password.getPassword(); //create array of chars to get password
//password1 = Integer.parseInt(String.valueOf(passcode2));//changing characters to int
//info = atmObject.Info(login1, password1);
// if(info==true)
cl.show(setPanel, "MainMenu");
}
}
}
我放置了弹出窗口以查看按钮是否有问题,但是当按下按钮时弹出窗口会显示。所以事件处理适用于按钮。当我按下按钮时,我不断收到消息。
Graphics$theHandler.actionPerformed
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
都是这样的。我有另一个程序的类似代码,但它没有给我任何错误。为什么说来源不明?
这是我的主要课程
import javax.swing.JFrame;
public class Display {//this class is to call the other methods
public static void main(String[] args) {
Graphics atmObject = new Graphics();
atmObject.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
atmObject.setSize(350, 100);
atmObject.setVisible(true);
}
}
我有另一个类,但是我还没有在我的代码中使用它。我为我的另一个 ATM 类创建了一个对象,但它还没有在代码中使用,所以它不应该干扰代码。