I have created a swings application and there is a "Start" button on the GUI. I want that whenever I clicked on that "Start" button, the start button should be disabled and the "Stop" button be enabled.
我创建了一个swing应用程序,在GUI上有一个“Start”按钮。我希望每当我点击“启动”按钮时,启动按钮应该被禁用,“停止”按钮被启用。
For that I have written the following code in the "ActionPeformed(...)" method of the "Start" button
为此,我在“开始”按钮的“ActionPeformed(…)”方法中编写了以下代码。
startButton.setEnabled(false);
stopButton.setEnabled(true);
But the above code is not creating the desired affect on the GUI.
但是上面的代码并没有在GUI上创建所需的影响。
Is the above code correct for what I want to do?
以上代码是否正确,我想做什么?
It's not working with "repaint()" too.
它也不能和“repaint()”一起工作。
Edit:
编辑:
The code is very long so I can't paste all the code. I can tell, though, more about the code.
代码很长,所以我不能粘贴所有的代码。不过,我可以告诉你更多关于代码的事情。
In the "ActionPeformed" method of "start" button, after calling the above two statements, I am executing a "SwingWorker" thread.
在“启动”按钮的“ActionPeformed”方法中,在调用上述两个语句之后,我正在执行一个“SwingWorker”线程。
Is this thread creating any problem?
这个线程会产生任何问题吗?
3 个解决方案
#1
19
For that I have written the following code in the "ActionPeformed(...)" method of the "Start" button
为此,我在“开始”按钮的“ActionPeformed(…)”方法中编写了以下代码。
You need that code to be in the actionPerformed(...)
of the ActionListener
registered with the Start button, not for the Start button itself.
您需要该代码在ActionListener中以Start按钮注册,而不是开始按钮本身。
You can add a simple ActionListener
like this:
您可以添加这样一个简单的ActionListener:
JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
}
);
note that your startButton above will need to be final
in the above example if you want to create the anonymous listener in local scope.
请注意,如果您想在本地范围内创建匿名侦听器,那么以上的startButton将需要在上面的示例中为final。
#2
7
The code is very long so I can't paste all the code.
代码很长,所以我不能粘贴所有的代码。
There could be any number of reasons why your code doesn't work. Maybe you declared the button variables twice so you aren't actually changing enabling/disabling the button like you think you are. Maybe you are blocking the EDT.
您的代码不能工作的原因有很多。也许您已经两次声明了按钮变量,所以实际上并没有像您认为的那样更改启用/禁用按钮。也许你挡住了EDT。
You need to create a SSCCE to post on the forum.
您需要创建一个SSCCE在论坛上发布。
So its up to you to isolate the problem. Start with a simple frame thas two buttons and see if your code works. Once you get that working, then try starting a Thread that simply sleeps for 10 seconds to see if it still works.
所以你要把问题孤立出来。以一个简单的框架作为两个按钮,看看你的代码是否有效。一旦你开始工作,那么试着启动一个线程,它只休眠10秒钟,看看它是否仍然有效。
Learn how the basice work first before writing a 200 line program.
在写200行程序之前,先了解基本原理。
Learn how to do some basic debugging, we are not mind readers. We can't guess what silly mistake you are doing based on your verbal description of the problem.
学习如何做一些基本的调试,我们不介意读者。根据你对问题的口头描述,我们猜不出你在做什么愚蠢的错误。
#3
3
This works.
这个作品。
public class TestButton {
public TestButton() {
JFrame f = new JFrame();
f.setSize(new Dimension(200,200));
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
final JButton stop = new JButton("Stop");
final JButton start = new JButton("Start");
p.add(start);
p.add(stop);
f.getContentPane().add(p);
stop.setEnabled(false);
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start.setEnabled(true);
stop.setEnabled(false);
}
});
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start.setEnabled(false);
stop.setEnabled(true);
}
});
f.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new TestButton();
}
}
}
#1
19
For that I have written the following code in the "ActionPeformed(...)" method of the "Start" button
为此,我在“开始”按钮的“ActionPeformed(…)”方法中编写了以下代码。
You need that code to be in the actionPerformed(...)
of the ActionListener
registered with the Start button, not for the Start button itself.
您需要该代码在ActionListener中以Start按钮注册,而不是开始按钮本身。
You can add a simple ActionListener
like this:
您可以添加这样一个简单的ActionListener:
JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
}
);
note that your startButton above will need to be final
in the above example if you want to create the anonymous listener in local scope.
请注意,如果您想在本地范围内创建匿名侦听器,那么以上的startButton将需要在上面的示例中为final。
#2
7
The code is very long so I can't paste all the code.
代码很长,所以我不能粘贴所有的代码。
There could be any number of reasons why your code doesn't work. Maybe you declared the button variables twice so you aren't actually changing enabling/disabling the button like you think you are. Maybe you are blocking the EDT.
您的代码不能工作的原因有很多。也许您已经两次声明了按钮变量,所以实际上并没有像您认为的那样更改启用/禁用按钮。也许你挡住了EDT。
You need to create a SSCCE to post on the forum.
您需要创建一个SSCCE在论坛上发布。
So its up to you to isolate the problem. Start with a simple frame thas two buttons and see if your code works. Once you get that working, then try starting a Thread that simply sleeps for 10 seconds to see if it still works.
所以你要把问题孤立出来。以一个简单的框架作为两个按钮,看看你的代码是否有效。一旦你开始工作,那么试着启动一个线程,它只休眠10秒钟,看看它是否仍然有效。
Learn how the basice work first before writing a 200 line program.
在写200行程序之前,先了解基本原理。
Learn how to do some basic debugging, we are not mind readers. We can't guess what silly mistake you are doing based on your verbal description of the problem.
学习如何做一些基本的调试,我们不介意读者。根据你对问题的口头描述,我们猜不出你在做什么愚蠢的错误。
#3
3
This works.
这个作品。
public class TestButton {
public TestButton() {
JFrame f = new JFrame();
f.setSize(new Dimension(200,200));
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
final JButton stop = new JButton("Stop");
final JButton start = new JButton("Start");
p.add(start);
p.add(stop);
f.getContentPane().add(p);
stop.setEnabled(false);
stop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start.setEnabled(true);
stop.setEnabled(false);
}
});
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start.setEnabled(false);
stop.setEnabled(true);
}
});
f.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new TestButton();
}
}
}