gpt4 book ai didi

java - 使用 Swing 控件实现简单的计算器 GUI

转载 作者:行者123 更新时间:2023-12-02 00:06:31 27 4
gpt4 key购买 nike

我想使用只有一个 JTextField 的 Swing 组件制作一个简单的计算器。在将实现ActionEvent的actionPerformed方法中,我想知道:当用户输入特定按钮时执行特定操作需要什么逻辑?

这是我的代码。

package calculator1;

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

import java.awt.event.*;

class MyFirstGUI extends JFrame implements ActionListener {

int val1 = 0;
int val2 = 0;
int sum = 0;

JTextField t1 = new JTextField(10);

JButton b1 = new JButton("+");
JButton b2 = new JButton("*");
JButton b3 = new JButton("/");
JButton b4 = new JButton("=");
int n1, n2;

public MyFirstGUI() {
setLayout(new FlowLayout());
setVisible(true);
setSize(500, 500);
JLabel l1 = new JLabel("Result");

add(l1);
add(t1);
add(b1);
add(b2);
add(b3);
add(b4);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
int val1, val2, sum = 0;

val1 = Integer.parseInt(t1.getText());
if (e.getSource() == b1) {
t1.setText("");

}
if (e.getSource() == b4) {
val2 = Integer.parseInt(t1.getText());
sum = val1 + val2;
t1.setText("" + sum);
}
}
}

class Test {
public static void main(String[] args) {
MyFirstGUI p = new MyFirstGUI();
}
}

最佳答案

希望这对您有帮助!

       import java.util.Arrays;

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

import java.awt.event.*;

class MyFirstGUI extends JFrame implements ActionListener {

int val1;
int val2;
int sum = 0;
int num=0;
JTextField t1 = new JTextField(10);

JButton b1 = new JButton("+");
JButton b2 = new JButton("*");
JButton b3 = new JButton("/");
JButton b4 = new JButton("=");
int n1, n2;

public MyFirstGUI() {
setLayout(new FlowLayout());
setVisible(true);
setSize(500, 500);
JLabel l1 = new JLabel("Result");

add(l1);
add(t1);
add(b1);
add(b2);
add(b3);
add(b4);

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

String[]a = {"+","*","/"};
if (e.getSource() == b1) {
val1 = Integer.parseInt(t1.getText());
t1.setText("");
b4.addActionListener(this);
}
else if (e.getSource() == b2) {
val1 = Integer.parseInt(t1.getText());
t1.setText("");
num=1;
b4.addActionListener(this);
}
else if (e.getSource() == b3) {
val1 = Integer.parseInt(t1.getText());
t1.setText("");
num=2;
b4.addActionListener(this);
}
else if (e.getSource() == b4) {
val2 = Integer.parseInt(t1.getText());

switch(a[num]) {
case "+":
sum = val1+val2;
System.out.println(val1+val2);
t1.setText(sum+"");
break;
case "*":
sum = val1*val2;
System.out.println(val1*val2);
t1.setText(sum+"");
break;
case "/":
sum = val1/val2;
System.out.println(val1/val2);
t1.setText(sum+"");
break;
}
b4.removeActionListener(this);
val1=0;val2=0;
}
}
}

class answer4 {
public static void main(String[] args) {
MyFirstGUI p = new MyFirstGUI();
}
}

我也为所有人添加了。

关于java - 使用 Swing 控件实现简单的计算器 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58152956/

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