簡體   English   中英

如何在JFrame的不同類中保留用於不同按鈕的處理程序?

[英]How can I keep handlers for different buttons in different classes in JFrame?

我的程序在使用JFrame時遇到另一個問題。 我正在制作一個“取款機”程序,該程序向用戶詢問他的名字,姓氏,當前帳戶狀態和提款金額。 我想要兩個類來實現程序執行的兩個不同任務。 班級卡應詢問用戶我之前說過的所有數據,然后單擊“接受”按鈕后,應顯示一個帶有“您好[用戶],您已提取[金額],當前帳戶狀態為[金額]”的消息框。 ”。 如果用戶超過“零狀態”,則意味着他要提款超過自己的提款額,程序將彈出一個帶有拒絕信息的消息框。 第二類CreditCard的功能完全相同,但允許用戶欠款高達1500。我有兩個處理程序:一個用於Card,它在單擊“接受”按鈕后可以正常工作,另一個用於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");
        }
        }       
    }

信用卡類別:

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

}

對我來說,您的代碼似乎運行良好。 也許您忘記了實例化信用卡而不是卡?

同樣,一種實現方法是傳遞引用,方法是為您的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM