将计算结果提供给JFrame MessageBox

时间:2014-01-30 19:57:26

标签: java swing parsing

我希望我的程序使用文本字段执行JFrame:名字,姓氏,帐户状态和提款金额,向用户询问该信息,然后显示消息框:“John John Smith,撤回后,你当前的账户状态是:(退出后的状态)。我是唯一可行的方式(起初我想创建一个单独的类,我会解析状态和提款金额,然后进行计算,但我有那个问题。所以我在Card类中做了。问题是(这是我想问你的)程序不仅没有进行计算,而且根本没有编译。当我从我的程序中删除计算代码,它编译,但很明显,只在消息框中返回“Hello firstName lastName”。

public class Card extends JFrame {

    private JTextField firstName;
    private JTextField lastName;
    private JTextField state;
    private JTextField withdrawal;
    private JButton accept;


    public Card() {
        super("Cash Machine");
        setLayout(new FlowLayout());

        firstName = new JTextField("First name");
        add(firstName);

        lastName = new JTextField("Last name");
        add(lastName);

        state = new JTextField("Current account state");
        add(state);

        withdrawal = new JTextField("Amount of withdrawal");
        add(withdrawal);

        accept = new JButton("Accept");
        add(accept);


        newHandler handler = new newHandler();
        accept.addActionListener(handler);

    }
    String state1 = state.getText();
    int state2 = Integer.parseInt(state1);
    String withdrawal1 = withdrawal.getText();
    int withdrawal2 = Integer.parseInt(withdrawal1);
    int finalState = state2 - withdrawal2;



    private class newHandler implements ActionListener {

        ArrayList<String> first_names = new ArrayList<String>();
        ArrayList<String> last_names = new ArrayList<String>();
        public void actionPerformed(ActionEvent event) {


            // SHOWING THE FINAL MESSAGE BOX
            if(event.getSource()==accept)

                JOptionPane.showMessageDialog(null, "Hello " + firstName.getText() + " " + lastName.getText() + " " + state.getText() + " .Your current account state is: " + finalState);
            }
            }       
        }

1 个答案:

答案 0 :(得分:2)

对不起,我的不好。你在课堂上发表的所有陈述都是作业陈述,所以 技术上 是允许的,但是他们的位置仍然给你的问题。

您的问题是,您已经在课堂上将计算结果作为作业语句的一部分,因此您在用户有机会输入数据之前进行计算

相反,请在处理程序类中进行计算,以便文本字段中包含一些数据。

像这样:

//   String state1 = state.getText();
//   int state2 = Integer.parseInt(state1);
//   String withdrawal1 = withdrawal.getText();
//   int withdrawal2 = Integer.parseInt(withdrawal1);
//   int finalState = state2 - withdrawal2;


private class newHandler implements ActionListener {
  ArrayList<String> first_names = new ArrayList<String>();
  ArrayList<String> last_names = new ArrayList<String>();

  public void actionPerformed(ActionEvent event) {

     // SHOWING THE FINAL MESSAGE BOX
     if (event.getSource() == accept) {

        String state1 = state.getText();
        int state2 = Integer.parseInt(state1);
        String withdrawal1 = withdrawal.getText();
        int withdrawal2 = Integer.parseInt(withdrawal1);
        int finalState = state2 - withdrawal2;

        JOptionPane.showMessageDialog(null, "Hello " + firstName.getText()
              + " " + lastName.getText() + " " + state.getText()
              + " .Your current account state is: " + finalState);
     }
  }
}