gpt4 book ai didi

java - 如何使这个 JFrame 代码更简单?

转载 作者:行者123 更新时间:2023-12-02 11:10:05 24 4
gpt4 key购买 nike

我刚刚开始学习JFrame,尝试制作一个简单的计算器。它可以工作,但它也是重复的,必须为每个按钮创建一个 ActionListener。有没有办法让它变得更简单?

public class gui extends JFrame
{
private JButton one;
private JButton two;
private JButton three;
private JButton four;
private JButton five;
private JButton six;
private JButton seven;
private JButton eight;
private JButton nine;
public double first = 0;
public double second = 0;
public double sum = 0;

public gui()
{
super("title");
setLayout(new FlowLayout());

one = new JButton("1"); //makes a new button to click.
two = new JButton("2");
three = new JButton("3");
four = new JButton("4");
five = new JButton("5");
six = new JButton("6");
seven = new JButton("7");
eight = new JButton("8");
nine = new JButton("9");
one.addActionListener( //MAKES NEW INNER CLASS TO DEFINE WHAT ONE DOES
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
// checks if clicked.
if (first == 0) first = 1;
else second = 1;

if (first != 0 && second != 0)
{
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
});
two.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 2;
}else {
second = 2;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
three.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 3;
}else {
second = 3;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
four.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 4;
}else {
second = 4;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
five.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 5;
}else {
second = 5;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
six.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 6;
}else {
second = 6;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
seven.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 7;
}else {
second = 7;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
eight.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 8;
}else {
second = 8;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;
}
}
}
);
nine.addActionListener(

new ActionListener() {

public void actionPerformed(ActionEvent event) {
if(first == 0) {
first = 9;
}else {
second = 9;
}
if(first != 0 && second != 0) {
sum = first + second;
System.out.println(sum);
first = 0;
second = 0;

}
}
}
);

add(one);
add(two);
add(three);
add(four);
add(five);
add(six);
add(seven);
add(eight);
add(nine);

}

}

而且我找不到一种方法来“组合”我拥有的两个数字。如果用户按 9 和 2,我想将这两个数字组合起来得到 92。我该怎么做?

最佳答案

要消除大量重复,您可以做的一件事是使用单个 ActionListener适用于您的所有按钮。您可以设置actionCommand每个按钮上的属性,操作监听器将使用该属性来检测按下了哪个按钮。

这是一个一般说明:

private ActionListener btnListener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
switch (event.getActionCommand()) {
case "0":
case "1":
// ...
case "9":
enterDigit(event.getActionCommand());
break;
}
}
};

public gui(){
one = new JButton("1");
one.setActionCommand("1");
one.addActionListener(btnListener);
two = new JButton("2");
two.setActionCommand("2");
two.addActionListener(btnListener);
// ...
}

至于如何组合单独的数字按钮按下来形成更大的数字,可能有多种方法,具体取决于您想要如何存储数字。
使用String开始可能是最简单的。 s,在这种情况下,您只需附加 String包含输入数字(来自 JButton 的 actionCommand )到现有 String 上先前输入的数字:

private String numberInDisplay = "";

// remember enterDigit from the ActionListener above? This is it...
private void enterDigit(String digit){
numberInDisplay = numberInDisplay + digit;
}

如果您想使用数字类型,例如 long ,您只需将现有数字乘以十并添加新数字的值即可:

private long numberInDisplay = 0;
private void enterDigit(String digit){
numberInDisplay = numberInDisplay * 10 + Long.valueOf(digit);

当然,这是一个相当简化的示例。如果您想处理小数点、负号、科学计数法等,将会有一些额外的复杂性,但它至少应该让您开始走上正确的道路。

关于java - 如何使这个 JFrame 代码更简单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50670688/

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