当我尝试在java中调用该类时,为什么我会得到一个空白表单

时间:2013-05-29 22:22:50

标签: java class

我正在尝试在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课程中的问题是什么?

请帮帮我。任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

特别是在事件处理程序(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可以进入构造函数。但是我已经让你工作得很好了。