Java 在swing中使用布尔变量代替ActionListner接口
我有一个小问题,请让我解释一下情况。我有一个swing jframe,其中有一个名为“start”的按钮,它以秒为单位启动计时器,因此每当我点击start时,它会将按钮本身转换为“reset”,这会使秒数为零,并再次将其自身转换为“start”。我关心的是,对于这两种情况,我必须运行两组代码,我使用了两个实现ActionListener接口的类,有没有办法将这两组代码包含在实现ActionListener的同一个类中,并根据一个布尔变量切换代码块,该布尔变量的值随按钮的变化而变化 我尝试过,但我面临着性能问题,比如冻结应用程序,甚至不能完全按照预期工作 请在下面查看我的代码Java 在swing中使用布尔变量代替ActionListner接口,java,swing,actionlistener,abstract-action,action-interface,Java,Swing,Actionlistener,Abstract Action,Action Interface,我有一个小问题,请让我解释一下情况。我有一个swing jframe,其中有一个名为“start”的按钮,它以秒为单位启动计时器,因此每当我点击start时,它会将按钮本身转换为“reset”,这会使秒数为零,并再次将其自身转换为“start”。我关心的是,对于这两种情况,我必须运行两组代码,我使用了两个实现ActionListener接口的类,有没有办法将这两组代码包含在实现ActionListener的同一个类中,并根据一个布尔变量切换代码块,该布尔变量的值随按钮的变化而变化 我尝试过,但我
public class SuperTimer extends JFrame
{
JButton start;
private final StartCountDown countdown;
public SuperTimer()
{
countdown= new StartCountDown();
start.addActionListener(countdown);
}
public class StartCountDown implements ActionListener
{
public void actionPerformed(ActionEvent l)
{
if(l.getSource()==start)
{
count = Long.parseLong(input.getText());
start.setText("Reset");
reset = new Reset();
start.removeActionListener(countdown);
start.addActionListener(reset);
invalid.setVisible(false);
}
TimeClass tc = new TimeClass(count);
timer = new Timer(1000,tc);
timer.start();
}
}
public class Reset implements ActionListener
{
public void actionPerformed(ActionEvent j)
{
start.setText("Start");
time.setText("00:00:00");
input.setText("");
timer.stop();
timeup.setVisible(false);
start.removeActionListener(reset);
start.addActionListener(countdown);
}
}
}**
只使用一个ActionListener或者更好的AbstractAction,给它一个布尔变量,然后让它根据布尔变量更改其状态 e、 g
@我真的很抱歉。您的回答帮助我更轻松地构建应用程序。再一次我很抱歉,我不知道堆栈溢出的规则,因为我是这个论坛的新手。非常感谢你的帮助,没关系。谢谢你的回复。我们是志愿者,不希望忽视自己的努力,所以你的回答意义重大。
import java.awt.event.ActionEvent;
import javax.swing.*;
public class ActionWithMultipleBehaviors extends JPanel {
private TimerButtonAction timerBtnAction = new TimerButtonAction("Start", "Reset");
private JButton timerButton = new JButton(timerBtnAction);
public ActionWithMultipleBehaviors() {
add(timerButton);
}
class TimerButtonAction extends AbstractAction {
private boolean stateStart = true;
private String name;
private String secondName;
public TimerButtonAction(String name, String secondName) {
super(name);
this.name = name;
this.secondName = secondName;
}
@Override
public void actionPerformed(ActionEvent e) {
String newName;
if (stateStart) {
newName = secondName;
// TODO: add start timer code
} else {
newName = name;
// TODO: add reset timer code
}
putValue(NAME, newName);
stateStart = !stateStart;
}
}
private static void createAndShowGui() {
ActionWithMultipleBehaviors mainPanel = new ActionWithMultipleBehaviors();
JFrame frame = new JFrame("ActionWithMultipleBehaviors");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}