我正在尝试在Java中创建ATM仿真。
我在调用另一个类时遇到了一些问题。当我运行WithdrawCash类时,它运行得很完美,但是当我在FastCash中通过从选项中选择一个空白表格时来调用它。
以下是WithdrawCash的代码:
import java.io.*;
import javax.swing.*;
import java.awt.*;`
import java.awt.event.*;
public class WithdrawCash extends JFrame implements ActionListener{
public static void main(String args[]){
WithdrawCash frame=new WithdrawCash();
frame.setBounds(400,300,400,150);
frame.createGUI();
frame.setTitle("Withdraw Cash Menu:");
frame.setVisible(true);
}//closes main
private JLabel title, menu;
private JTextField number;
private JButton SUBMIT;
private void createGUI(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container wm=getContentPane();
wm.setLayout(new FlowLayout());
title = new JLabel("Please choose from the options below (a-d):");
menu = new JLabel("a)Fast Cash b)Normal Cash c)Main Menu d)Logout");
number = new JTextField(10);
SUBMIT = new JButton("SUBMIT");
wm.add(title);
wm.add(menu);
wm.add(number);
wm.add(SUBMIT);
SUBMIT.addActionListener(this);
}//closes createGUI
public void actionPerformed(ActionEvent evt){
String value1=number.getText();
String a,b,c,d,A,B,C,D;
if(number.getText().length()==0){
JOptionPane.showMessageDialog(null, "Please enter the menu option");
number.requestFocus();
}//closes if
switch(value1){
case "a":
case "A": //Fast Cash
JOptionPane.showMessageDialog(null, "Fast Cash");
break;
case "b":
case "B": //Normal Cash
JOptionPane.showMessageDialog(null, "Normal Cash");
break;
case "c":
case "C": //Main Menu
new AccountOpen().setVisible(true);
this.dispose();
break;
case "d":
case "D": //Logout
this.dispose();
break;
default:
JOptionPane.showMessageDialog(null, "Invalid option!! Please type between a-d");
number.setText(null);
number.requestFocus();
}//closes switch
}//closes actionPerformed
}//closes WithdrawCash
以及FastCash的代码:
import java.io.*;
import java.sql.*;
import javax.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FastCash extends JFrame implements ActionListener{
public static void main(String args[]){
FastCash frame=new FastCash();
frame.setBounds(400,300,400,150);
frame.createGUI();
frame.setTitle("Fast Cash Menu:");
frame.setVisible(true);
}//closes main
private JLabel title, menu, menu2;
private JTextField number;
private JButton SUBMIT;
private void createGUI(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container fc=getContentPane();
fc.setLayout(new FlowLayout());
title = new JLabel("Please choose from the options below (1-10):");
menu = new JLabel("1)500 2)1000 3)2000 4)5000 5)10000 6)15000");
menu2 = new JLabel("7)20000 8)Withdrawal menu 9)Main Menu 10)Logout");
number = new JTextField(10);
SUBMIT = new JButton("SUBMIT");
fc.add(title);
fc.add(menu);
fc.add(menu2);
fc.add(number);
fc.add(SUBMIT);
SUBMIT.addActionListener(this);
}//closes createGUI
public void actionPerformed(ActionEvent evnt){
int value1=Integer.parseInt(number.getText());
if(number.getText().length()==0){
JOptionPane.showMessageDialog(null, "Please enter the menu option");
number.requestFocus();
}//closes if
switch(value1){
case 1:
JOptionPane.showMessageDialog(null, "500");
break;
case 2:
JOptionPane.showMessageDialog(null, "1000");
break;
case 3:
JOptionPane.showMessageDialog(null, "2000");
break;
case 4:
JOptionPane.showMessageDialog(null, "5000");
break;
case 5:
JOptionPane.showMessageDialog(null, "10000");
break;
case 6:
JOptionPane.showMessageDialog(null, "15000");
break;
case 7:
JOptionPane.showMessageDialog(null, "20000");
break;
case 8:
new WithdrawCash().setVisible(true);
this.dispose();
break;
case 9: //Main Menu
new AccountOpen().setVisible(true);
this.dispose();
break;
case 10:
this.dispose();
break;
default:
JOptionPane.showMessageDialog(null, "Invalid option!! Please type between 1-10");
number.setText(null);
number.requestFocus();
}//closes switch
}//closes actionPerformed
}//closes FastCash
Nb:当我尝试打开AccountOpen(我的主菜单)时,它从我的FastCash类完美打开,那么WithdrawCash类有什么问题?
请帮我。任何帮助,将不胜感激。谢谢。
最佳答案
特别是在事件处理程序(actionPerformed)中,而且在frame.setVisible(true);
上,也应该调用
public void actionPerformed(ActionEvent evt){
java.awt.EventQueue.invokeLater(new Runnabble() {
@Override
public void run() {
// ...Old code of actionPerformed here...
}
});
}
当处于actionPerformed状态时,您位于事件处理线程上,应该快速返回,以使GUI保持响应状态。另外,其他事件也不会处理。
因此,以上内容确保了封闭的代码在稍后完成。
上面的答案没有解决。
快速解决方案:在FastCash中添加初始化。
case 8:
new WithdrawCash().setVisible(true);
应该成为
case 8:
WithdrawCash frame = new WithdrawCash();
frame.setBounds(400, 300, 400, 150);
frame.createGUI();
frame.setTitle("Withdraw Cash Menu:");
frame.setVisible(true);
并且
createGUI
不能再为private
。风格上应该说其他的东西; createGUI可以进入构造函数。但是我已经使你工作了。