gpt4 book ai didi

java - 按钮启用为 0

转载 作者:行者123 更新时间:2023-12-01 18:52:36 25 4
gpt4 key购买 nike

所以,我的任务是这样的。编写一个程序,打开一个包含按钮的窗口。该按钮通过“0”(零)启用。每单击一次该按钮,其标签中的数字就会增加 1。

我做了将按钮增加 1 的部分,但我被困在应该启用按钮 0 的部分。我的代码如下:

public class Butt0n extends Frame implements ActionListener {
private TextField text;
private Button button;
private Frame f;
private int count = 0;
public Butt0n () {
setLayout(new FlowLayout());
text = new TextField(count + "", 10); //
text.setEditable(false);
add(text);

button = new Button("Count");
add(button);
button.addActionListener(this);
setTitle("Counter");
setSize(300, 200);

setVisible(true);

}


public static void main(String[] args) {
new Butt0n();
}


@Override
public void actionPerformed(ActionEvent evt) {
++count;
text.setText(count + "");
}
}

任何帮助或提示将不胜感激。

最佳答案

如果我很理解你,你应该写这样的内容:

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Butt0n extends Frame implements ActionListener {
private Button button;
private int count = 0;

public Butt0n () {
setLayout(new FlowLayout());
button = new Button(count+ "");

if(button.getLabel().equals("0")){
button.setEnabled(true);
}

add(button);
button.addActionListener(this);
setTitle("Counter");
setSize(300, 200);

setVisible(true);

}


public static void main(String[] args) {
new Butt0n();
}


@Override
public void actionPerformed(ActionEvent evt) {
++count;
button.setLabel(count + "");
}
}

关于java - 按钮启用为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59706358/

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