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



我在使用Jframe的程序中还有另一个问题。我正在制作一个"现金计算机"程序,该程序向用户询问他的名字,姓氏,当前帐户状态和提款金额。我想有两个类别实现该程序执行的两个不同任务的类。班级卡应询问我之前说过的所有数据,然后单击"接受"按钮后,它应该给出一个带有信息的消息框。。"如果用户超过"零状态",则意味着他想撤回超过自己的撤回,则程序将弹出一个带有下降信息的消息框。第二类CreditCard的执行方式完全相同,但允许用户将债务高达1500。我有两个处理程序:一个用于卡的卡片,单击"接受"按钮后正常工作,而第二个则无法使用,这根本不起作用。我知道问题在于适当的继承,但我无法真正解决它。对我而言,将信用卡处理程序存储在信用卡类中很重要(当然可以)。

我的代码如下:卡类:

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");
        }
        }       
    }

信用卡课程:

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");
    }
}

}

对我来说似乎您的代码工作正常。也许您忘了实例化信用卡而不是卡片?

再次,通过给您的处理程序类传递参考参数并使用该参数设置字段来传递参考:

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

最新更新