[英]How to implement multiple buttons using Java's Swing library and ActionEvent
我正在自學如何在 Swing 中使用 Java 的圖形 UI,目前正在制作簡單的計算器。 但是,我在我的應用程序上實現 +、- 和清除按鈕時遇到問題。
在展示我的代碼之前,我想指出我省略了工作正常且與我當前問題無關的部分。
我還想指出,我的程序有單獨的包“ calculator.applicationlogic
”,其中包含接口NumberMath
及其實現CalculateNumber
。 NumberMath
和CalculateNumber
用於記錄數字,因此它所做的只是返回當前數字,添加到當前數字,從當前數字中減去,並通過設置為0 清除記錄數字。
以下是實現 UI 的文件:
// GraphicCalculator.java
package calculator.ui;
import calculator.applicationlogic.*;
import calculator.ui.*;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import javax.swing.JTextField;
import javax.swing.JPanel;
import javax.swing.JButton;
public class GraphicCalculator implements Runnable{
private JFrame frame;
private NumberMath num;
public GraphicCalculator(NumberMath num){
this.num = num;
};
@Override
public void run(){
... config for how windows look ...
}
// displays output and input
private void createComponents(Container container){
GridLayout layout = new GridLayout(3, 1);
container.setLayout(layout);
JTextField output = new JTextField("0");
output.setEnabled(false);
JTextField input = new JTextField();
container.add(output);
container.add(input);
container.add(createPanel(input, output));
}
// Button that has +, -, and clear (clear denoted with letter Z)
private JPanel createPanel(JTextField in, JTextField out){
JPanel panel = new JPanel(new GridLayout(1,3));
// create buttons
JButton addButton = new JButton("+");
JButton subButton = new JButton("-");
JButton clearButton = new JButton("Z");
// Action
CalculateListener listener = new CalculateListener(this.num, in, out);
addButton.addActionListener(listener);
subButton.addActionListener(listener);
clearButton.addActionListener(listener);
// add buttons to panel
panel.add(addButton);
panel.add(subButton);
panel.add(clearButton);
return panel;
}
public JFrame getFrame(){
return frame;
}
}
以下是對按鈕實施操作的 EventListener:
// CalculateListener.java
package calculator.ui;
import calculator.applicationlogic.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JButton;
public class CalculateListener implements ActionListener{
private NumberMath value;
private JTextField input;
private JTextField output;
public CalculateListener(NumberMath value, JTextField input, JTextField output){
this.value = value;
this.input = input;
this.output = output;
}
@Override
public void actionPerformed(ActionEvent ae){
if(ae.getSource().getClass().equals(JButton.class)){
String userInput = this.input.getText();
int inputValue = Integer.parseInt(userInput);
JButton buttonPressed = (JButton)ae.getSource();
// add
// ****** buttonPressed.getName() returns Null, which is causing program to crash ******//
if(buttonPressed.getName().equals("+")){
this.value.addNum(inputValue);
}
// sub
if(buttonPressed.getName().equals("-")){
this.value.subNum(inputValue);
}
// clear
if(buttonPressed.getName().equals("Z")){
this.value.clearNum();
}
this.output.setText(String.valueOf(this.value.getNum()));
this.input.setText("");
}
else{
System.err.println("No Button");
}
}
}
由於我遇到了 ActionEvent 無法識別按鈕名稱的問題,因此我參考了這篇文章來確定不同的按鈕。 在編譯時,由於在獲取ae.getSource()
名稱時為“null”,因此我在計算器中輸入數字后程序就會崩潰。 下面是我的程序的樣子和崩潰結果:
在上面的例子中,程序在輸入字段輸入數字 2 並按下“+”按鈕后崩潰。 我想知道是否有人可以指導我為什么CalculateListener.java
上的buttonPressed.getName()
返回空值以及如何解決這個問題。
提前謝謝你的幫助。
簡單地改變
buttonPressed.getName().equals("+")
到
buttonPressed.getText().equals("+")
並對其他檢查執行相同操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.