Java swing - 当点击取消按钮时不循环 [英] Java swing - when cancel button clicked don't loop
问题描述
我有一个gui,即添加了一个登录提示。
I have a gui, that is having a Login prompt added.
while(notValidLogIn){
LoginPrompt.getDetails() //a static method that
}
但是,loginPrompt是一个Jdialog,带有父JFrame。如何停止取消点击的循环,我可以将System.exit(0)置于取消操作中。但是不想停止一切,我想要类似的东西:
Hwoever, the loginPrompt is a Jdialog, with a parent JFrame. How can I stop looping of cancel clicked, I could put System.exit(0) in cancel action performed. But don't want to stop everything, I want something like :
while(notValidLogIn && LoginPrompt.isNotCancelled()){
LoginPrompt.getDetails(); //a static method that creates an instance of login JDialog()
}
推荐答案
在我最近的一个项目中,我已经实现了一个基于事件的解决方案。想法是 JDialog
通知其父 JFrame
登录过程如何进行,最后一个可能会或可能不会继续执行。这样我就没有循环并且保持不同的职责:模式将是这样的:
In a recent project I was working on, I've implemented an event based solution. The idea is JDialog
notify to its parent JFrame
how the login process went and this last one may or may not continue its execution. This way I have no loops and keep separate responsibilities: The schema would be something like this:
这是事件本身。并不复杂:
This is the event itself. Not that complicated:
class LoginEvent extends EventObject {
public static final int LOGIN_SUCCEEDED = 0;
public static final int LOGIN_FAILED = 1;
public static final int LOGIN_DIALOG_CLOSED = 2;
private int id;
public LoginEvent(Object source, int id) {
super(source);
this.id = id;
}
public int getId() {
return id;
}
}
LoginListener
处理这些 LoginEvent
的接口:
public interface LoginListener extends EventListener {
public void handleLoginEvent(LoginEvent evt);
}
登录对话
此类必须使用订阅的 LoginListeners
来保留 List
:
class LoginDialog {
List<LoginListener> listeners = new ArrayList<>();
JDialog dialog;
JButton accept;
JButton cancel;
public void show() {
//create and show GUI components
}
public void close() {
if(dialog != null) {
dialog.dispose();
}
}
...
public void addLoginListener(LoginListener loginEventListener) {
if(!listeners.contains(loginEventListener)) {
listeners.add(loginEventListener);
}
}
public void removeLoginListener(LoginListener loginEventListener) {
listeners.remove(loginEventListener);
}
public void dispatchLoginEvent(LoginEvent evt) {
for(LoginListener loginListener: listeners) {
loginListener.handleLoginEvent(evt);
}
}
}
向<$添加动作侦听器c $ c>接受和取消
按钮:
accept.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// validate login data
if(loginValid) {
dispatchLoginEvent(new LoginEvent(dialog, LoginEvent.LOGIN_SUCCEEDED));
} else {
dispatchLoginEvent(new LoginEvent(dialog, LoginEvent.LOGIN_FAILED));
}
}
});
cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispatchLoginEvent(new LoginEvent(dialog, LoginEvent.LOGIN_DIALOG_CLOSED));
}
});
订阅LoginListener
在你的 JFrame
:
final LoginDialog dialog = new LoginDialog();
dialog.addLoginListener(new LoginListener() {
@Override
public void handleLoginEvent(LoginEvent evt) {
if(evt.getId() == LoginEvent.LOGIN_SUCCEEDED {
dialog.close();
//continue execution
return;
}
if(evt.getId() == LoginEvent.LOGIN_FAILED) {
JOptionPane.showMessageDialog(null, "Login failed!");
return;
}
if(evt.getId() == LoginEvent.CLOSE_LOGIN_DIALOG) {
dialog.close();
// do something when this dialog is closed
}
}
};
dialog.show();
这篇关于Java swing - 当点击取消按钮时不循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!