我目前有一些問題與我正在開發的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("");
}
}
}
太謝謝你了!我甚至沒有注意到我使用過整數。該計劃的所有問題都已解決。再次謝謝你! – YellowSoloCup