如何在JFrame中为不同类中的不同按钮保留处理程序?

时间:2014-01-31 17:20:39

标签: java swing inheritance jframe

我的程序有另一个问题,即使用JFrame。我正在制作一个“取款机”程序,询问用户他的名字,姓氏,经常账户状态和提款金额。我希望有两个类来实现程序执行的两个不同的任务。类卡应该询问用户我之前说过的所有数据,然后点击“接受”按钮后,它应该给出一个消息框,其中包含“Hello [user],你已经提取[金额],你当前的账户状态是[金额] “。如果用户超过“零状态”意味着他想要提取比他更多的东西,程序会弹出一个带有拒绝信息的消息框。第二类CreditCard完全相同,但允许用户债务高达1500.我有两个处理程序:一个用于卡,单击“接受”按钮后工作正常,第二个用于CreditCard,根本不起作用。我知道问题在于正确的继承,但我无法真正解决它。对于我来说,将CreditCard处理程序存储在CreditCard类中非常重要(如果可能的话)。

我的代码如下: 卡类:

public class Card extends JFrame {

public JTextField firstName;
public JTextField lastName;
public JTextField state;
public JTextField withdrawal;
private JButton accept;
public JButton CREDIT_CARD;
private JLabel firstNameLabel;




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

    firstNameLabel = new JLabel("First name");
    add(firstNameLabel);
    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);

    CREDIT_CARD = new JButton("Credit Card");
    add(CREDIT_CARD);



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

}

private class handler1 implements ActionListener {

    public void actionPerformed(ActionEvent event) {

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

        // SHOWING THE FINAL MESSAGE BOX
        if(event.getSource()==accept)
            if(finalState > 0)
            JOptionPane.showMessageDialog(null, "Hello " + firstName.getText() + " " + lastName.getText()  + " .Your current account state is: " + finalState);
            else
                JOptionPane.showMessageDialog(null,"You are out of money on your debit account");
        }
        }       
    }

CreditCard类:

public class CreditCard extends Card {

public CreditCard(){
    handler1 handler = new handler1();
    CREDIT_CARD.addActionListener(handler);
}

private class handler1 implements ActionListener {

    public void actionPerformed(ActionEvent event)
    {
         String state1 = state.getText();
        int state2 = Integer.parseInt(state1);
        String withdrawal1 = withdrawal.getText();
        int withdrawal2 = Integer.parseInt(withdrawal1);
        int finalState = state2 - withdrawal2;

        if(event.getSource()==CREDIT_CARD)
            if(finalState >= -1500)
            JOptionPane.showMessageDialog(null, "Hello " + firstName.getText() + " " + lastName.getText()  + " .Your current account state is: " + finalState);
            else
                JOptionPane.showMessageDialog(null,"Your credit card limit has been reached");
    }
}

}

2 个答案:

答案 0 :(得分:0)

对我而言,您的代码似乎运行正常。 也许你忘了实例化CreditCard而不是Card?

答案 1 :(得分:0)

同样,一种方法是通过让Handler类将引用传递给它的参数并使用该参数来设置字段来传递引用:

import java.awt.event.ActionEvent;

import javax.swing.*;

public class Gui extends JPanel {
   JTextField fooField = new JTextField(10);
   JButton button = new JButton(new Handler("Press Me", this));

   public Gui() {
      add(new JLabel("Foo:"));
      add(fooField);
      add(button);
   }

   public String getFooFieldText() {
      return fooField.getText();
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("Gui");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new Gui());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }

}

class Handler extends AbstractAction {

   private Gui gui;

   public Handler(String name, Gui gui) {
      super(name);
      this.gui = gui;
   }

   @Override
   public void actionPerformed(ActionEvent arg0) {
      String foo = gui.getFooFieldText();
      String text = "Foo: " + foo;
      String title = "Foo Text";
      JOptionPane.showMessageDialog(gui, text, title, JOptionPane.INFORMATION_MESSAGE);
   }

}

最好使用MVC