gpt4 book ai didi

java - GUI计算器计算

转载 作者:行者123 更新时间:2023-12-02 05:55:24 26 4
gpt4 key购买 nike

我目前正在使用的 GUI 计算器遇到一些问题。计算器进行编译并进行计算。要进行计算,您必须首先输入一个数字,选择一个函数(+、-、* 或/),选择另一个数字,然后按等于。执行方程式后,必须先清除程序才能继续。我无法弄清楚如何制作它,以便程序在继续计算之前不需要按清除。我希望能够单击一个数字,单击+,单击一个数字,单击等于以获得答案,然后再次按+将另一个数字添加到方程中。任何有关此问题的帮助将不胜感激。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
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.JPanel;
import javax.swing.JTextField;


public class GUICalc extends JPanel implements ActionListener {

private static final long serialVersionUID = 1L;

private JButton[] numberButtons;
private JButton[] upButtons;

private JTextField field;
private double num2, ans;
private int op;

// 0 = gridx 1 gridy 2 gridwidth 3 gridheight
private int[][] numConstraints = new int[][] {
{0, 4, 1, 1},
{0, 1, 1, 1},
{1, 1, 1, 1},
{2, 1, 1, 1},
{0, 2, 1, 1},
{1, 2, 1, 1},
{2, 2, 1, 1},
{0, 3, 1, 1},
{1, 3, 1, 1},
{2, 3, 1, 1},
};

private int[][] upConstraints = new int[][] {
{1, 4, 1, 1},
{3, 4, 1, 1},
{3, 3, 1, 1},
{3, 2, 1, 1},
{3, 1, 1, 1},
{0, 5, 4, 1},
{2, 4, 1, 1},
};

public GUICalc() {
setPreferredSize(new Dimension(666, 666)); //width & heigth

GridBagLayout layout; // used to be private
GridBagConstraints gbc; // used to be private

layout = new GridBagLayout();
layout.columnWidths = new int[] {60, 60, 60, 60};
layout.rowHeights = new int[] {60, 60, 60, 60, 60, 60};
setLayout(layout);

gbc = new GridBagConstraints();

numberButtons = new JButton[10];

numberButtons[0] = new JButton("0");
numberButtons[1] = new JButton("1");
numberButtons[2] = new JButton("2");
numberButtons[3] = new JButton("3");
numberButtons[4] = new JButton("4");
numberButtons[5] = new JButton("5");
numberButtons[6] = new JButton("6");
numberButtons[7] = new JButton("7");
numberButtons[8] = new JButton("8");
numberButtons[9] = new JButton("9");

for(int i = 0; i < numberButtons.length; i++){
gbc.gridx = numConstraints[i][0];
gbc.gridy = numConstraints[i][1];
gbc.gridwidth = numConstraints[i][2];
gbc.gridheight = numConstraints[1][3];
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(2, 2, 2, 2);
numberButtons[i].addActionListener(this);
add(numberButtons[i], gbc);
}

upButtons = new JButton[7];

upButtons[0] = new JButton(".");
upButtons[1] = new JButton("/");
upButtons[2] = new JButton("*");
upButtons[3] = new JButton("-");
upButtons[4] = new JButton("+");
upButtons[5] = new JButton("=");
upButtons[6] = new JButton("C");

for(int i = 0; i < upButtons.length; i++){
gbc.gridx = upConstraints[i][0];
gbc.gridy = upConstraints[i][1];
gbc.gridwidth = upConstraints[i][2];
gbc.gridheight = upConstraints[1][3];
gbc.insets = new Insets(2, 2, 2, 2);
upButtons[i].addActionListener(this);
add(upButtons[i], gbc);


}

field = new JTextField();
field.setBorder(BorderFactory.createLineBorder(Color.BLACK));
field.setEditable(false);
field.setFont(new Font("Arial",Font.PLAIN, 24));
field.setText(null);

gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 4;
gbc.gridheight = 1;

add(field, gbc);

}


public static void main(String [] args){
JFrame frame = new JFrame("Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
frame.add(new GUICalc(), BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

}


@Override
public void actionPerformed(ActionEvent e) {

if(e.getSource() == numberButtons[0]){
field.setText(field.getText() + 0);
}
if(e.getSource() == numberButtons[1]){
field.setText(field.getText() + 1);
}
if(e.getSource() == numberButtons[2]){
field.setText(field.getText() + 2);
}
if(e.getSource() == numberButtons[3]){
field.setText(field.getText() + 3);
}
if(e.getSource() == numberButtons[4]){
field.setText(field.getText() + 4);
}
if(e.getSource() == numberButtons[5]){
field.setText(field.getText() + 5);
}
if(e.getSource() == numberButtons[6]){
field.setText(field.getText() + 6);
}
if(e.getSource() == numberButtons[7]){
field.setText(field.getText() + 7);
}
if(e.getSource() == numberButtons[8]){
field.setText(field.getText() + 8);
}
if(e.getSource() == numberButtons[9]){
field.setText(field.getText() + 9);
}

if(e.getSource() == upButtons[0] && !field.getText().contains(".")) {
field.setText(field.getText() + ".");
}

if(e.getSource() == upButtons[1]) {
ans = Integer.parseInt(field.getText());
op = 4;
field.setText("");
}

if(e.getSource() == upButtons[2]) {
ans = Integer.parseInt(field.getText());
op = 3;
field.setText("");
}
if(e.getSource() == upButtons[3]) {
ans = Integer.parseInt(field.getText());
op = 2;
field.setText("");
}
if(e.getSource() == upButtons[4]) {
ans = Integer.parseInt(field.getText());
op = 1;
field.setText("");
}
if(e.getSource() == upButtons[5]) {
num2 = Integer.parseInt(field.getText());

if(op == 1){
ans = ans + num2;
} else if(op == 2){
ans = ans - num2;

} else if(op == 3){
ans = ans * num2;
} else if(op == 4){
ans = ans / num2;
}

op = 0;
field.setText("" + ans);
}

if(e.getSource() == upButtons[6]) {
ans = 0.0;
field.setText("");
}
}


}

最佳答案

您的程序的问题在于您对 double 进行计算,但将文本解析为整数。将 Integer.parseInt 替换为 Double.parseDouble,它将如您所愿。

关于java - GUI计算器计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23141307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com