gpt4 book ai didi

java - 我正在尝试使用 JFrame 创建计算器,但遇到一些困难

转载 作者:行者123 更新时间:2023-12-01 15:17:51 24 4
gpt4 key购买 nike

基本上,它是让东西显示出来,这样我就可以测试它并确保我走在正确的轨道上。这是我的主“计算器”文件的代码:

package simplecalculator;        

import javax.swing.JFrame;
import javax.swing.UIManager;

public class Calculator {

public static void main(String[] args) {

JFrame calculatorFrame = new Listener();

calculatorFrame.setSize(1000, 0x3e8);

calculatorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calculatorFrame.setVisible(true);
}
}

我有一个单独的类监听器文件:

package simplecalculator;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Listener extends JFrame {
private JLabel enterFirstNumber;
private JLabel enterSecondNumber;
private JLabel resultLabel;
private JTextField getFirstNumber;
private JTextField getSecondNumber;
private JButton addition;
private JButton subtraction;
private JButton multiplication;
private JButton division;
private JPanel panelOne;
private JPanel panelTwo;
private JPanel panelThree;

private static final int frameWidth = 1000;
private static final int frameHeight = 1000;

int firstNumber;
int secondNumber;
double finalNumber;

public void Calc(){

setSize(frameWidth, frameHeight);

enterFirstNumber = new JLabel("Enter First Number: ");
getFirstNumber = new JTextField("0", 12);

enterSecondNumber = new JLabel("Enter Second Number: ");
getSecondNumber = new JTextField("0", 12);

}

public void buttons()
{
addition = new JButton("+");
addition.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstNumber = Integer.parseInt(getFirstNumber.getText());
secondNumber = Integer.parseInt(getSecondNumber.getText());
finalNumber = firstNumber + secondNumber;
resultLabel.setText("" + finalNumber);
}
});


subtraction = new JButton("-");
subtraction.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstNumber = Integer.parseInt(getFirstNumber.getText());
secondNumber = Integer.parseInt(getSecondNumber.getText());
finalNumber = firstNumber - secondNumber;
resultLabel.setText("" + finalNumber);
}
});

multiplication = new JButton("*");
multiplication.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstNumber = Integer.parseInt(getFirstNumber.getText());
secondNumber = Integer.parseInt(getSecondNumber.getText());
finalNumber = firstNumber * secondNumber;
resultLabel.setText("" + finalNumber);
}
});

division = new JButton("/");
division.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
firstNumber = Integer.parseInt(getFirstNumber.getText());
secondNumber = Integer.parseInt(getSecondNumber.getText());
finalNumber = firstNumber / secondNumber;
resultLabel.setText("" + finalNumber);
}
});
}

private void panels(){
panelOne = new JPanel();
panelOne.setLayout(new GridLayout(2, 2));
panelOne.add(enterFirstNumber);
panelOne.add(getFirstNumber);
panelOne.add(enterSecondNumber);
panelOne.add(getSecondNumber);

panelTwo = new JPanel();
panelTwo.setLayout(new GridLayout(2, 2));
panelTwo.add(addition);
panelTwo.add(subtraction);
panelTwo.add(multiplication);
panelTwo.add(division);

panelThree = new JPanel();
panelThree.add(resultLabel);
}
}

基本上我需要知道的是:

1)我走的路正确吗?2) 我怎样才能得到一个真正的计算器而不仅仅是一个空白的小程序?

抱歉,如果我听起来像个菜鸟;我是一名编程学生,我已经花了大约 12 个小时试图解决这个问题。

最佳答案

您永远不会向 JFrame 添加任何组件。很明显,它不包含任何东西。您应该向 JFrame 子类添加一个构造函数,并使其向框架添加一些组件。

您还应该缩进代码,使其可读,并尊重 Java 命名约定。

关于java - 我正在尝试使用 JFrame 创建计算器,但遇到一些困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11385313/

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