[英]I am trying to make a simple calculator in JSwing
您好,我正在制作一個包含許多程序的 Java 程序。 其中之一是計算器
問題是我們曾經嘗試多次調用計算器,它乘以我按下按鈕的數字,乘以我打開它的次數。 (第一次它運行良好,第二次它顯示“11”而不是 1 次和第三次(444555)如果我按 45)這里是它的主要代碼
package us.Spunvice.Gui.Calculator;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.border.Border;
import us.Spunvice.Gui.MainMenu.Menu;
@SuppressWarnings("serial")
public class Calculator extends JFrame implements ActionListener {
public static JButton button1 = new JButton();
public static JButton button2 = new JButton();
public static JButton button3 = new JButton();
public static JButton button4 = new JButton();
public static JButton button5 = new JButton();
public static JButton button6 = new JButton();
public static JButton button7 = new JButton();
public static JButton button8 = new JButton();
public static JButton button9 = new JButton();
public static JButton button0 = new JButton();
public static final int FRAME_HEIGHT = 520;
public static final int FRAME_WIDTH = 520;
public static JButton backButton = new JButton();
public static JButton buttonEquals = new JButton();
public static JButton buttonAddition = new JButton();
public static JButton buttonDivision = new JButton();
public static JButton buttonSubtraction = new JButton();
public static JButton buttonMultiplication = new JButton();
public static JLabel label = new JLabel();
public static Border border = BorderFactory.createLineBorder(Color.black, 2);
public static int times = 0;
public Calculator(){
if(Menu.timesOpened == 1) {
CalculatorDeclaration.firstNumber = 0;
CalculatorDeclaration.secondNumber = 0;
CalculatorDeclaration.answers = 0;
CalculatorDeclaration.thePreviousSign = "";
}
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
this.setState(Calculator.NORMAL);
this.setLayout(null);
this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
this.getContentPane().setBackground(Color.CYAN);
label.setBounds(150, 100, 200, 50);
label.setBackground(Color.WHITE);
label.setVisible(true);
label.setOpaque(true);
label.setBorder(border);
button1.setText("1");
button1.setBounds(150, 150, 50, 50);
button1.setFocusable(false);
button1.addActionListener(this);
button2.setText("2");
button2.setBounds(200, 150, 50, 50);
button2.setFocusable(false);
button2.addActionListener(this);
button3.setText("3");
button3.setBounds(250, 150, 50, 50);
button3.setFocusable(false);
button3.addActionListener(this);
button4.setText("4");
button4.setBounds(150, 200, 50, 50);
button4.setFocusable(false);
button4.addActionListener(this);
button5.setText("5");
button5.setBounds(200, 200, 50, 50);
button5.setFocusable(false);
button5.addActionListener(this);
button6.setText("6");
button6.setBounds(250, 200, 50, 50);
button6.setFocusable(false);
button6.addActionListener(this);
button7.setText("7");
button7.setBounds(150, 250, 50, 50);
button7.setFocusable(false);
button7.addActionListener(this);
button8.setText("8");
button8.setBounds(200, 250, 50, 50);
button8.setFocusable(false);
button8.addActionListener(this);
button9.setText("9");
button9.setBounds(250, 250, 50, 50);
button9.setFocusable(false);
button9.addActionListener(this);
button0.setText("0");
button0.setBounds(150, 300, 50, 50);
button0.setFocusable(false);
button0.addActionListener(this);
buttonEquals.setText("=");
buttonEquals.setBounds(200, 300, 100, 50);
buttonEquals.setFocusable(false);
buttonEquals.addActionListener(this);
buttonAddition.setText("+");
buttonAddition.setFocusable(false);
buttonAddition.setBounds(300, 150, 50, 50);
buttonAddition.addActionListener(this);
buttonDivision.setText("/");
buttonDivision.setFocusable(false);
buttonDivision.setBounds(300, 200, 50, 50);
buttonDivision.addActionListener(this);
buttonSubtraction.setText("-");
buttonSubtraction.setFocusable(false);
buttonSubtraction.setBounds(300, 250, 50, 50);
buttonSubtraction.addActionListener(this);
buttonMultiplication.setText("*");
buttonMultiplication.setFocusable(false);
buttonMultiplication.setBounds(300, 300, 50, 50);
buttonMultiplication.addActionListener(this);
backButton.setText("Back");
backButton.setFocusable(false);
backButton.setBounds(200, 400, 100, 50);
backButton.addActionListener(this);
this.add(button1);
this.add(button2);
this.add(button3);
this.add(button4);
this.add(button5);
this.add(button6);
this.add(button7);
this.add(button8);
this.add(button9);
this.add(button0);
this.add(backButton);
this.add(buttonEquals);
this.add(buttonAddition);
this.add(buttonSubtraction);
this.add(buttonMultiplication);
this.add(buttonDivision);
this.add(label);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == button1){
new NumbersPressed(1);
}else if(e.getSource() == button2){
new NumbersPressed(2);
}else if(e.getSource() == button3){
new NumbersPressed(3);
}else if(e.getSource() == button4){
new NumbersPressed(4);
}else if(e.getSource() == button5){
new NumbersPressed(5);
}else if(e.getSource() == button6){
new NumbersPressed(6);
}else if(e.getSource() == button7){
new NumbersPressed(7);
}else if(e.getSource() == button8){
new NumbersPressed(8);
}else if(e.getSource() == button9){
new NumbersPressed(9);
}else if(e.getSource() == button0){
new NumbersPressed(0);
}else if(e.getSource() == buttonEquals){
new EqualsToPressed();
}else if(e.getSource() == buttonAddition){
new AdditionPressed();
}else if(e.getSource() == buttonSubtraction){
new SubractionPressed();
}else if(e.getSource() == buttonMultiplication){
new MultiplicationPressed();
}else if(e.getSource() == buttonDivision){
new DivisionPressed();
}else if(e.getSource() == backButton) {
new BackPressed();
this.dispose();
}
}
}
主菜單是
package us.Spunvice.Gui.MainMenu;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import us.Spunvice.Gui.Calculator.Calculator;
@SuppressWarnings("serial")
public class Menu extends JFrame implements ActionListener {
public static JButton calculatorButton = new JButton();
public static JLabel calculatorLabel = new JLabel();
public static short timesOpened = 0;
public static ImageIcon calculatorIcon = new ImageIcon(
"C:\\Users\\adminn\\Desktop\\Spunvice\\Spunvice\\src\\us\\Spunvice\\Gui\\Assets\\images\\Cal.png");
public Menu() {
this.setVisible(true);
this.setSize(1000, 700);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null);
this.getContentPane().setBackground(Color.CYAN);
calculatorButton.setText("Calculator");
calculatorButton.addActionListener(this);
calculatorButton.setBounds(0, 200, 200, 50);
calculatorButton.setVisible(true);
calculatorLabel.setIcon(calculatorIcon);
calculatorLabel.setBounds(0, -150, 500, 500);
calculatorLabel.setVisible(true);
this.add(calculatorButton);
this.add(calculatorLabel);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == calculatorButton) {
timesOpened += 1 ;
new Calculator();
}
}
}
這是我向標簽添加數字的方法
package us.Spunvice.Gui.Calculator;
public class NumbersPressed {
public NumbersPressed(int number){
if(Calculator.times == 0) {
CalculatorDeclaration.firstNumber = (CalculatorDeclaration.firstNumber * 10) + number;
Calculator.label.setText(String.valueOf(CalculatorDeclaration.firstNumber));
}else {
CalculatorDeclaration.secondNumber = (CalculatorDeclaration.secondNumber * 10) + number;
Calculator.label.setText(String.valueOf(CalculatorDeclaration.secondNumber));
}
}
}
如果你想了解更多信息,這里是 repo https://github.com/Dde1ta/Spunvice感謝閱讀
基本上,您的問題是每次調用Calculator
類的構造函數時都會向每個JButton
添加相同的ActionListener
,並且每次用戶按下類Menu
的calculatorButton
時都會調用構造函數。 這是因為變量是靜態變量。 所以第二次打開計算器時,每個按鈕都有相同ActionListener
兩個副本。 當按鈕被按下時,每個ActionListener
被調用,因此被按下的按鈕上的數字被添加到label
文本中,計算器被打開的次數。
最簡單的修復,為您請求幫助的具體問題,是去除ActionListener
當您關閉計算器,即當用戶按下backButton
類Calculator
。 您可以通過更改類Calculator
中方法actionPerformed
中的if(e.getSource() == backButton)
塊來完成此操作
}else if(e.getSource() == backButton) {
button1.removeActionListener(this);
button2.removeActionListener(this);
button3.removeActionListener(this);
button4.removeActionListener(this);
button5.removeActionListener(this);
button6.removeActionListener(this);
button7.removeActionListener(this);
button8.removeActionListener(this);
button9.removeActionListener(this);
button0.removeActionListener(this);
buttonEquals.removeActionListener(this);
buttonAddition.removeActionListener(this);
buttonSubtraction.removeActionListener(this);
buttonMultiplication.removeActionListener(this);
buttonDivision.removeActionListener(this);
backButton.removeActionListener(this);
this.dispose();
}
無論上述解決方案如何,您確實應該注意問題評論中的所有建議。 還要注意,通常,在Swing應用程序中,作為 GUI 組件的變量被聲明為類的實例成員,而不是靜態成員。 因此,例如, Calculator
類中的button1
應聲明如下:
private JButton button1;
除了 Oracle 的Swing教程 之外,還有幾本關於Swing 的書籍,您可以找到它們的免費下載版本。
您還可以在線找到許多Swing計算器的示例。 只需搜索java swing 計算器,看看其他人是如何構建Swing計算器的。
您的問題在於計算器的初始化。 您只在代碼的開頭初始化按鈕,但是每次創建新的計算器時,都會將動作偵聽器添加到按鈕中,因此按鈕會多次調用動作偵聽器。 您的問題有兩種不同的解決方案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.