如何在ActionListener中退出方法 [英] How to exit a method in ActionListener
问题描述
我有一个ActionListener
连接到JTextField
,并且想要键入内容,以便退出ActionListener
所在的方法.
代码:
main() {
Security(x,x,x);
}
public void Security(JTextArea out, JTextField in) {
in.setText("");
in.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (in.getText().contains("exitsys")) {
out.append("Security:Security System Deactivated\n");
return;
}
in.setText("");
}
});
out.append("Security:Security System Activated\n");
fileWrite(":SYSTEM_INITIATED@" + time(), out);
}
我要键入"exitsys"
并返回主类方法"main()"
.
fileWrite
方法使用PrintWriter
输出数据.
问题摘要:我尝试调用return;但它不会返回方法main(),我该如何解决?
基本上,您需要某种模式对话框,通过该对话框,您可以有效地在显示对话框的那一刻停止程序的执行直到对话框被关闭(关闭),然后执行才会继续...
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JDialog dialog = new JDialog();
dialog.setTitle("Testing");
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.add(new TestPane());
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
System.out.println("Now back in the main...");
}
});
}
public class TestPane extends JPanel {
private JTextField field;
public TestPane() {
setLayout(new GridBagLayout());
field = new JTextField(10);
field.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("exitsys".equals(field.getText())) {
SwingUtilities.getWindowAncestor(field).dispose();
}
}
});
add(field);
}
}
}
有关详细信息,请参见如何制作对话框 >
I have an ActionListener
connected to a JTextField
and want to type something so that it will exit the method the ActionListener
is in.
Code:
main() {
Security(x,x,x);
}
public void Security(JTextArea out, JTextField in) {
in.setText("");
in.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (in.getText().contains("exitsys")) {
out.append("Security:Security System Deactivated\n");
return;
}
in.setText("");
}
});
out.append("Security:Security System Activated\n");
fileWrite(":SYSTEM_INITIATED@" + time(), out);
}
I want to type "exitsys"
and return to the main class method "main()"
.
The fileWrite
method uses a PrintWriter
to output data.
QUESTION SUMMARY: I try calling return; but it does not return to the method main(), how do i fix this?
Basically what you need is some kind of modal dialog, which will allow you to, effectively, halt the execution of your program at the point the dialog is made visible until the dialog is dismissed (closed), when the execution will continue...
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JDialog dialog = new JDialog();
dialog.setTitle("Testing");
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.add(new TestPane());
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
System.out.println("Now back in the main...");
}
});
}
public class TestPane extends JPanel {
private JTextField field;
public TestPane() {
setLayout(new GridBagLayout());
field = new JTextField(10);
field.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if ("exitsys".equals(field.getText())) {
SwingUtilities.getWindowAncestor(field).dispose();
}
}
});
add(field);
}
}
}
See How to Make Dialogs for more details
这篇关于如何在ActionListener中退出方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!