gpt4 book ai didi

Java Swing 计算器 - 缺少 main 方法

转载 作者:行者123 更新时间:2023-12-01 21:59:41 25 4
gpt4 key购买 nike

今天我编写了一个图形计算器,它仅通过使用按钮进行操作,完成后我意识到我没有在整个代码中定义主要方法。我已经考虑了一段时间,但以我有限的知识,无法想出解决这个问题的方法。

我正在考虑用 main 替换用于构造计算器的方法,但在访问 main 之外的非静态变量时遇到了问题。

我的引用代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GraphicalCalculator{

// Hold first and second numbers, to be acted upon
private double num1 = 0;
private double num2 = 0;
private char function = ' ';

// User's 'screen'
private JTextField Scr;

public GraphicalCalculator(){

// Numerical Buttons (0-9)
JButton[] bNum = new JButton[10];
for(int i = 9; i >= 0; i--){
bNum[i] = new JButton(Integer.toString(i));
}

// Function buttons
JButton bClear = new JButton("Clear");
JButton bCalc = new JButton("Calculate");
JButton bAdd = new JButton("+");
JButton bSub = new JButton("-");
JButton bMul = new JButton("*");
JButton bDiv = new JButton("/");

// TextField which acts as user's screen
Scr = new JTextField("");
Scr.setEnabled(false);

// Calculator frame, contains entire GUI
JFrame frame = new JFrame("Calculator");
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

// Main pane, contains 3 sub-panes
JPanel pane = (JPanel) frame.getContentPane();
pane.setLayout(new GridLayout(3, 1));

// Add numerical buttons to Panel
JPanel numButtons = new JPanel();
numButtons.setLayout(new GridLayout(4, 3));
for(int i = 9; i >= 0; i--){
numButtons.add(bNum[i]);
}

// Add function buttons to Panel
JPanel fncButtons = new JPanel();
fncButtons.setLayout(new GridLayout(3, 2));
fncButtons.add(bAdd);
fncButtons.add(bSub);
fncButtons.add(bMul);
fncButtons.add(bDiv);
fncButtons.add(bClear);
fncButtons.add(bCalc);

// Implementing action listeners for each numerical button
bNumAction[] bNumActions = new bNumAction[10];
for(int i = 0; i <= 9; i++){
bNumActions[i] = new bNumAction(bNum[i]);
bNum[i].addActionListener(bNumActions[i]);
}

// Implementing action listeners for each function button
bAdd add = new bAdd();
bAdd.addActionListener(add);
bSub sub = new bSub();
bSub.addActionListener(sub);
bMul mul = new bMul();
bMul.addActionListener(mul);
bDiv div = new bDiv();
bDiv.addActionListener(div);
bClear clear = new bClear();
bClear.addActionListener(clear);
bCalc calc = new bCalc();
bCalc.addActionListener(calc);

pane.add(Scr);
pane.add(numButtons);
pane.add(fncButtons);
frame.add(pane);
}

private class bNumAction implements ActionListener{

// Holds value of button pressed
private String temp;

// Retrieve value of button pressed
public bNumAction(JButton pressed){
this.temp = pressed.getText();
}

// If there is currently a value on the screen, add it it, otherwise set screen value to input
public void actionPerformed(ActionEvent e){
if(!Scr.getText().equals("")){
Scr.setText(Scr.getText() + temp);
} else {
Scr.setText("");
actionPerformed(e);
}
}
}

// Following 4 classes transmit function key pressed to numIn
private class bAdd implements ActionListener{
public void actionPerformed(ActionEvent e){
numIn('+');
}
}

private class bSub implements ActionListener{
public void actionPerformed(ActionEvent e){
numIn('-');
}
}

private class bMul implements ActionListener{
public void actionPerformed(ActionEvent e){
numIn('*');
}
}

private class bDiv implements ActionListener{
public void actionPerformed(ActionEvent e){
numIn('/');
}
}

// Stores value of input number and function used, then clears for next input
public void numIn (char fnc){
if(num1 == 0){
num1 = Double.parseDouble(Scr.getText());
Scr.setText("");
} else {
num2 = Double.parseDouble(Scr.getText());
Scr.setText("");
}
function = fnc;
}

// Clears all values if Clear button is pressed
private class bClear implements ActionListener{
public void actionPerformed(ActionEvent e){
Scr.setText("");
num1 = 0;
num2 = 0;
function = ' ';
}
}

// Calculates result when Calculate button is pressed, stores result as num1 for use in continued calculation
private class bCalc implements ActionListener{
public void actionPerformed(ActionEvent e){
num2 = Double.parseDouble(Scr.getText());
if(function == '+'){
Scr.setText(Double.toString((Math.round((num1 / num2) * 100)) / 100));
} else if(function == '-'){
Scr.setText(Double.toString(num1 - num2));
} else if(function == '*'){
Scr.setText(Double.toString(num1 * num2));
} else if(function == '/'){
Scr.setText(Double.toString(num1 / num2));
} else {
Scr.setText(String.valueOf(num1));
}
num1 = Double.parseDouble(Scr.getText());
}
}
}

任何解决方案或解决方法的建议将不胜感激。

最佳答案

你的构造函数似乎正在完成设置 GUI 的所有工作。因此,您所需要做的就是创建类的实例:

public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GraphicalCalculator();
}
});
}

关于Java Swing 计算器 - 缺少 main 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33792944/

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