Java - 单击按钮时执行某些操作,然后将其还原
Java - Doing something when button clicked, then revert it
好的,所以我正在做一个 Java window(一个 JFrame,它的用途并不重要)并且我在上面有一个按钮。我想要处理的是单击按钮时,它会更改其文本,然后应用程序会执行某些操作,完成后按钮会恢复其初始文本。
像这样...
JButton myButton = new JButton("Initial");
myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((JButton) e.getSource()).setText("New");
//Do other things
//Do more other things
((JButton) e.getSource()).setText("Initial");
}
});
这就是我到目前为止尝试过的方法,但我知道它没有按预期工作(所有代码都执行。我不是真正的专家,我正在做这些事情来学习,所以我有不知道有没有办法。
我已经在网上寻找过这个问题的解决方案,但我没有找到任何东西(也许我没有正确搜索),所以我希望有人可以帮助我解决这个问题!
PS:对不起,如果我的英语不完美,我知道。问我这个问题是否有不清楚的地方。感谢大家!
凯文
编辑:该应用程序是一个数独求解器,因此需要一段时间 //做其他事情。这就是为什么我试图更改 solve 按钮文本(所以它说 is solving 并且当它完成时它说 已解决).
您尝试过:
JButton myButton = new JButton("Initial");
myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myButton.setText("New");
//Do other things
//Do more other things
myButton.setText("Initial");
}
});
在你的例子中你遗漏了 actionPerformed
并且你没有直接访问按钮(我不能说你的例子中的 e
是什么)
只需将当前文本保存在局部变量中,并在执行其他操作后将其恢复原状。
您还应该确保它确实是您单击的按钮,或者至少在投射之前检查 instanceof JButton。
final JButton myButton = new JButton("Initial");
myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == myButton) {
String initialText = myButton.getText();
myButton.setText("New");
// Do other things
// Do more other things
myButton.setText(initialText);
}
}
});
您还遗漏了 actionPerformed 方法,问题中的代码无法编译 - 我猜您只是在编辑器中编写了它。
你的逻辑没有错!看看我下面的例子:
public class MainFrame extends JFrame {
public MainFrame() {
setMinimumSize(new Dimension(200, 100));
JButton myButton = new JButton("Initial");
add(myButton);
myButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
final JButton triggerBtn = (JButton) e.getSource();
final String originalValue = triggerBtn.getText();
triggerBtn.setText("New");
JOptionPane.showMessageDialog(null, "Speak softly and carry a big stick and you will go far.");
triggerBtn.setText(originalValue);
}
});
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
}
});
}
}
如果您 运行 这样做,您会看到按钮已更改。如果您要将 showMessageDialog
行更改为 Thread.sleep(10*1000)
,您将 看不到变化!这是因为您 运行 在调度程序线程和文本上设置事件,即使它已更改,在您的方法完成之前也不允许触发更改事件。
如果您在同一个线程上进行工作,请考虑以下替代方案:
myButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
final JButton triggerBtn = (JButton) e.getSource();
final String originalValue = triggerBtn.getText();
triggerBtn.setText("New");
SwingWorker<Void, Void> sw = new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
Thread.sleep(10*1000);
return null;
}
@Override
protected void done() {
triggerBtn.setText(originalValue);
}
};
sw.execute();
}
});
这会设置文本,并异步启动 SwingWorker
到 运行 作业。完成后,调度程序线程将更新文本,而不需要调度程序线程等待它完成(因此事件得到正确处理)。
让我知道这是否适合你!
好的,所以我正在做一个 Java window(一个 JFrame,它的用途并不重要)并且我在上面有一个按钮。我想要处理的是单击按钮时,它会更改其文本,然后应用程序会执行某些操作,完成后按钮会恢复其初始文本。 像这样...
JButton myButton = new JButton("Initial");
myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
((JButton) e.getSource()).setText("New");
//Do other things
//Do more other things
((JButton) e.getSource()).setText("Initial");
}
});
这就是我到目前为止尝试过的方法,但我知道它没有按预期工作(所有代码都执行。我不是真正的专家,我正在做这些事情来学习,所以我有不知道有没有办法。
我已经在网上寻找过这个问题的解决方案,但我没有找到任何东西(也许我没有正确搜索),所以我希望有人可以帮助我解决这个问题!
PS:对不起,如果我的英语不完美,我知道。问我这个问题是否有不清楚的地方。感谢大家!
凯文
编辑:该应用程序是一个数独求解器,因此需要一段时间 //做其他事情。这就是为什么我试图更改 solve 按钮文本(所以它说 is solving 并且当它完成时它说 已解决).
您尝试过:
JButton myButton = new JButton("Initial");
myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myButton.setText("New");
//Do other things
//Do more other things
myButton.setText("Initial");
}
});
在你的例子中你遗漏了 actionPerformed
并且你没有直接访问按钮(我不能说你的例子中的 e
是什么)
只需将当前文本保存在局部变量中,并在执行其他操作后将其恢复原状。 您还应该确保它确实是您单击的按钮,或者至少在投射之前检查 instanceof JButton。
final JButton myButton = new JButton("Initial");
myButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == myButton) {
String initialText = myButton.getText();
myButton.setText("New");
// Do other things
// Do more other things
myButton.setText(initialText);
}
}
});
您还遗漏了 actionPerformed 方法,问题中的代码无法编译 - 我猜您只是在编辑器中编写了它。
你的逻辑没有错!看看我下面的例子:
public class MainFrame extends JFrame {
public MainFrame() {
setMinimumSize(new Dimension(200, 100));
JButton myButton = new JButton("Initial");
add(myButton);
myButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
final JButton triggerBtn = (JButton) e.getSource();
final String originalValue = triggerBtn.getText();
triggerBtn.setText("New");
JOptionPane.showMessageDialog(null, "Speak softly and carry a big stick and you will go far.");
triggerBtn.setText(originalValue);
}
});
}
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
MainFrame mainFrame = new MainFrame();
mainFrame.setVisible(true);
}
});
}
}
如果您 运行 这样做,您会看到按钮已更改。如果您要将 showMessageDialog
行更改为 Thread.sleep(10*1000)
,您将 看不到变化!这是因为您 运行 在调度程序线程和文本上设置事件,即使它已更改,在您的方法完成之前也不允许触发更改事件。
如果您在同一个线程上进行工作,请考虑以下替代方案:
myButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
final JButton triggerBtn = (JButton) e.getSource();
final String originalValue = triggerBtn.getText();
triggerBtn.setText("New");
SwingWorker<Void, Void> sw = new SwingWorker<Void, Void>() {
protected Void doInBackground() throws Exception {
Thread.sleep(10*1000);
return null;
}
@Override
protected void done() {
triggerBtn.setText(originalValue);
}
};
sw.execute();
}
});
这会设置文本,并异步启动 SwingWorker
到 运行 作业。完成后,调度程序线程将更新文本,而不需要调度程序线程等待它完成(因此事件得到正确处理)。
让我知道这是否适合你!