我对Java有点陌生,我一直在尝试创建一个计算器。除了 . 按钮,谁能帮我弄清楚如何做到这一点,我已经尝试了我所知道的一切,但没有任何效果
这是我到目前为止得到的代码:
package Java24;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator extends JFrame implements ActionListener{
JPanel row1 = new JPanel();
JTextField display = new JTextField("0");
Font font = new Font("Calibri", Font.PLAIN, 24);
JPanel row2 = new JPanel();
JButton c = new JButton("C");
JPanel row3 = new JPanel();
JButton num7 = new JButton("7");
JButton num8 = new JButton("8");
JButton num9 = new JButton("9");
JButton divide = new JButton("/");
JPanel row4 = new JPanel();
JButton num4 = new JButton("4");
JButton num5 = new JButton("5");
JButton num6 = new JButton("6");
JButton multiply = new JButton("*");
JPanel row5 = new JPanel();
JButton num1 = new JButton("1");
JButton num2 = new JButton("2");
JButton num3 = new JButton("3");
JButton minus = new JButton("-");
JPanel row6 = new JPanel();
JButton num0 = new JButton("0");
JButton dot = new JButton(".");
JButton equals = new JButton("=");
JButton plus = new JButton("+");
public Calculator(){
super("Calculator");
setSize(450,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(6,1);
setLayout(layout);
GridLayout layout1 = new GridLayout(1,1);
display.setFont(font);
display.setEnabled(false);
row1.setLayout(layout1);
row1.add(display);
add(row1);
c.addActionListener(this);
c.setFont(font);
row2.setLayout(layout1);
row2.add(c);
add(row2);
num7.setFont(font);
num8.setFont(font);
num9.setFont(font);
divide.setFont(font);
num7.addActionListener(this);
num8.addActionListener(this);
num9.addActionListener(this);
divide.addActionListener(this);
GridLayout layout2 = new GridLayout(1,4);
row3.setLayout(layout2);
row3.add(num7);
row3.add(num8);
row3.add(num9);
row3.add(divide);
add(row3);
num4.setFont(font);
num5.setFont(font);
num6.setFont(font);
multiply.setFont(font);
num4.addActionListener(this);
num5.addActionListener(this);
num6.addActionListener(this);
multiply.addActionListener(this);
row4.setLayout(layout2);
row4.add(num4);
row4.add(num5);
row4.add(num6);
row4.add(multiply);
add(row4);
num1.setFont(font);
num2.setFont(font);
num3.setFont(font);
minus.setFont(font);
num1.addActionListener(this);
num2.addActionListener(this);
num3.addActionListener(this);
minus.addActionListener(this);
row5.setLayout(layout2);
row5.add(num1);
row5.add(num2);
row5.add(num3);
row5.add(minus);
add(row5);
num0.setFont(font);
dot.setFont(font);
equals.setFont(font);
plus.setFont(font);
num0.addActionListener(this);
dot.addActionListener(this);
equals.addActionListener(this);
plus.addActionListener(this);
row6.setLayout(layout2);
row6.add(num0);
row6.add(dot);
row6.add(equals);
row6.add(plus);
add(row6);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
String button = e.getActionCommand();
if(button.equals("C")){
display.setText("0");
}
if(button.equals("7")){
int lol = Integer.parseInt(display.getText());
if(lol != 0){
int num = 7;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}else{
int num = 7;
display.setText("" + num);
}
}
if(button.equals("8")){
int lol = Integer.parseInt(display.getText());
if(lol != 0){
int num = 8;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}else{
int num = 8;
display.setText("" + num);
}
}
if(button.equals("9")){
int lol = Integer.parseInt(display.getText());
if(lol != 0){
int num = 9;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}else{
int num = 9;
display.setText("" + num);
}
}
if(button.equals("4")){
int lol = Integer.parseInt(display.getText());
if(lol != 0){
int num = 4;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}else{
int num = 4;
display.setText("" + num);
}
}
if(button.equals("5")){
int lol = Integer.parseInt(display.getText());
if(lol != 0){
int num = 5;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}else{
int num = 5;
display.setText("" + num);
}
}
if(button.equals("6")){
int lol = Integer.parseInt(display.getText());
if(lol != 0){
int num = 6;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}else{
int num = 6;
display.setText("" + num);
}
}
if(button.equals("1")){
int lol = Integer.parseInt(display.getText());
if(lol != 0){
int num = 1;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}else{
int num = 1;
display.setText("" + num);
}
}
if(button.equals("2")){
int lol = Integer.parseInt(display.getText());
if(lol != 0){
int num = 2;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}else{
int num = 2;
display.setText("" + num);
}
}
if(button.equals("3")){
int lol = Integer.parseInt(display.getText());
if(lol != 0){
int num = 3;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}else{
int num = 3;
display.setText("" + num);
}
}
if(button.equals("0")){
int lol = Integer.parseInt(display.getText());
if(lol != 0){
int num = 0;
int prevNum = Integer.parseInt(display.getText());
display.setText("" + prevNum + num);
}else{
int num = 0;
display.setText("" + num);
}
}
if(button.equals("+")){
plus();
}
if(button.equals("-")){
minus();
}
if(button.equals("*")){
multiply();
}
if(button.equals("/")){
divide();
}
if(button.equals("=")){
if(plusNum != null){
int secondNum = Integer.parseInt(display.getText());
int sum = firstNum + secondNum;
String secondNumber = String.valueOf(secondNum);
display.setText("" + sum);
if(plusNum != null){
remove(plusNum);
}
if(minusNum != null){
remove(minusNum);
}
if(multiplyNum != null){
remove(multiplyNum);
}
if(divideNum != null){
remove(divideNum);
}
datNum = new JLabel(secondNumber);
add(datNum);
}
if(minusNum != null){
int secondNum = Integer.parseInt(display.getText());
int sum = firstNum - secondNum;
String secondNumber = String.valueOf(secondNum);
display.setText("" + sum);
if(plusNum != null){
remove(plusNum);
}
if(minusNum != null){
remove(minusNum);
}
if(multiplyNum != null){
remove(multiplyNum);
}
if(divideNum != null){
remove(divideNum);
}
datNum = new JLabel(secondNumber);
add(datNum);
}
if(multiplyNum != null){
int secondNum = Integer.parseInt(display.getText());
int sum = firstNum * secondNum;
String secondNumber = String.valueOf(secondNum);
display.setText("" + sum);
if(plusNum != null){
remove(plusNum);
}
if(minusNum != null){
remove(minusNum);
}
if(multiplyNum != null){
remove(multiplyNum);
}
if(divideNum != null){
remove(divideNum);
}
datNum = new JLabel(secondNumber);
add(datNum);
}
if(divideNum != null){
int secondNum = Integer.parseInt(display.getText());
int sum = firstNum / secondNum;
String secondNumber = String.valueOf(secondNum);
display.setText("" + sum);
if(plusNum != null){
remove(plusNum);
}
if(minusNum != null){
remove(minusNum);
}
if(multiplyNum != null){
remove(multiplyNum);
}
if(divideNum != null){
remove(divideNum);
}
datNum = new JLabel(secondNumber);
add(datNum);
}
}
}
int firstNum;
JLabel plusNum;
JLabel minusNum;
JLabel multiplyNum;
JLabel divideNum;
JLabel datNum;
void plus(){
firstNum = Integer.parseInt(display.getText());
String firstNumber = String.valueOf(firstNum);
if(minusNum != null){
minusNum = null;
}
if(multiplyNum != null){
multiplyNum = null;
}
if(divideNum != null){
divideNum = null;
}
plusNum = new JLabel(firstNumber);
add(plusNum);
display.setText("0");
}
void minus(){
firstNum = Integer.parseInt(display.getText());
String firstNumber = String.valueOf(firstNum);
if(plusNum != null){
plusNum = null;
}
if(multiplyNum != null){
multiplyNum = null;
}
if(divideNum != null){
divideNum = null;
}
minusNum = new JLabel(firstNumber);
add(minusNum);
display.setText("0");
}
void multiply(){
firstNum = Integer.parseInt(display.getText());
String firstNumber = String.valueOf(firstNum);
if(plusNum != null){
plusNum = null;
}
if(minusNum != null){
minusNum = null;
}
if(divideNum != null){
divideNum = null;
}
multiplyNum = new JLabel(firstNumber);
add(multiplyNum);
display.setText("0");
}
void divide(){
firstNum = Integer.parseInt(display.getText());
String firstNumber = String.valueOf(firstNum);
if(plusNum != null){
plusNum = null;
}
if(minusNum != null){
minusNum = null;
}
if(multiplyNum != null){
multiplyNum = null;
}
divideNum = new JLabel(firstNumber);
add(divideNum);
display.setText("0");
}
public static void main(String[] args){
Calculator calc = new Calculator();
}
}