基本上它是让它出现的东西所以我可以测试它并确保我在正确的轨道上。这是我的主要“计算器”文件的代码:
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个小时试图解决这个问题。
答案 0 :(得分:1)
您永远不会向JFrame添加任何组件。显然,它不包含任何东西。您应该向JFrame子类添加构造函数,并使其向框架添加一些组件。
您还应该缩进代码,使其可读,并遵守Java命名约定。
答案 1 :(得分:0)
在尝试构建基本计算器时存在很多陷阱:在GUI布局中,输入/格式化数字并提供所需的精度等。
您可以使用此示例在开始时跳过大部分内容: https://github.com/plokhotnyuk/calculator/tree/fee1b741aa74d659b8e30ad66d26d9ca6a2f6bc5
主要思想(从端到端测试开始)借鉴了这本神奇的书: http://www.growing-object-oriented-software.com/
随意分叉和破解! 享受TDD& amp;可执行规范!!!