首页 > 解决方案 > 我正在尝试在 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 感谢阅读

标签: javaswing

解决方案


基本上,您的问题是每次调用 class 的构造函数时都添加相同ActionListenerJButton内容,并且每次用户按下in classCalculator时都会调用构造函数。这是因为变量是静态变量。所以第二次打开计算器时,每个按钮都有两个相同的. 当按钮被按下时,每个都被调用,因此按下按钮上的数字被添加到计算器打开次数的文本中。calculatorButtonMenuActionListenerActionListenerlabel

对于您请求帮助的特定问题,最简单的解决方法是ActionListener在您关闭计算器时删除 ,即当用户按下backButtonclass时Calculator。您可以通过更改类if(e.getSource() == backButton)中的方法中的块来做到这一点actionPerformedCalculator

}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 中组件的变量被声明为类的实例成员,而不是静态成员。因此,例如,button1在类中Calculator应声明如下:

private JButton button1;

除了 Oracle 的Swing教程之外,还有几本关于Swing的书籍,您可以找到它们的免费下载版本。

您还可以在线找到许多Swing计算器的示例。只需搜索java swing 计算器,看看其他人是如何构建Swing计算器的。


推荐阅读