我对我的简单安装程序/激活器有疑问。在平台启动期间恢复。我正在阅读教程http://platform.netbeans.org/tutorials/60/nbm-login.html,一切顺利,但现在我已准备好自己的JPanel表单,用于对话框操作按钮,因为如果用户名或通行证不正确,我不想继续。但是,如果他们是正确的,我现在也无法继续......
public class LoginForm extends javax.swing.JPanel {
public LoginForm() {
initComponents();
}
private void initComponents() {...}
protected javax.swing.JButton jButtonExit;
protected javax.swing.JButton jButtonLogin;
protected javax.swing.JButton jButtonRemindPass;
private javax.swing.JLabel jLabelMsg;
private javax.swing.JLabel jLabelPassword;
private javax.swing.JLabel jLabelUsername;
private javax.swing.JPasswordField jPasswordFieldPass;
private javax.swing.JTextField jTextFieldUser;
public String getUsername() {
return this.jTextFieldUser.getText();
}
public String getPassword() {
return new String(this.jPasswordFieldPass.getPassword());
}
public void setErrorLabel(String msg) {
jLabelMsg.setForeground(Color.RED);
jLabelMsg.setText(msg);
}
public void setInfoLabel(String msg) {
jLabelMsg.setForeground(Color.BLUE);
jLabelMsg.setText(msg);
}
}
在Installer.java中,我创建了一个NotifyDescriptor,并使用空数组创建了setOptions。现在我该如何强制通知
DialogDisplayer.getDefault().notifyLater(nd);
继续前进,因为我的平台在加载过程中堆叠。
public class Installer extends ModuleInstall {
LoginForm lf;
NotifyDescriptor nd;
@Override
public void restored() {
createLoginDialog();
}
private void createLoginDialog() {
lf = new LoginForm();
lf.jButtonLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
authenticate();
}
});
lf.jButtonExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
exit();
}
});
nd = new NotifyDescriptor.Message(lf, NotifyDescriptor.PLAIN_MESSAGE);
nd.setOptions(new Object[]{});
nd.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (NotifyDescriptor.CLOSED_OPTION.equals(evt.getNewValue())) {
exit();
}
}
});
DialogDisplayer.getDefault().notifyLater(nd);
}
private void authenticate() {
if (lf.getUsername().equals("user")) {
if (lf.getPassword().equals("pass") {
System.out.println("Login and pass OK!");
} else {
lf.setErrorLabel("Incorrect password");
}
} else {
lf.setErrorLabel("Unknown username");
}
}
private void exit() {
LifecycleManager.getDefault().exit();
}
}
答案 0 :(得分:0)
更好的方法是通过DialogDescriptor创建监听器:
loginPanel = new LoginPanel(this, authenticationCookies);
loginPanel.setName("loginPanel");
loginDescriptor = new DialogDescriptor(loginPanel,
"loginDialog",
Boolean.TRUE,
new Object[]{turnOff, login}, //my owna JButtons
login,
DialogDescriptor.DEFAULT_ALIGN,
HelpCtx.DEFAULT_HELP,
this, // my class implements actionListener !
Boolean.FALSE);
loginDescriptor.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(DialogDescriptor.PROP_VALUE) && evt.getNewValue() == DialogDescriptor.CLOSED_OPTION) {
LifecycleManager.getDefault().exit();
}
}
});
loginPanel.setDescriptor(loginDescriptor);
//
createDialog = DialogDisplayer.getDefault().createDialog(loginDescriptor);
}
现在是我的动作监听器
@Override
public void actionPerformed(ActionEvent e) {
synchronized (this) {
if (e.getSource() == DialogDescriptor.CANCEL_OPTION) {
LifecycleManager.getDefault().exit();
} else {
actionCommand = e.getActionCommand();
if (login.equals(actionCommand)) {
} else if (turnOff.equals(actionCommand)) {
LifecycleManager.getDefault().exit();
} else if (button.equals(e.getSource())) {
loginDescriptor.setClosingOptions(null);
} else if (cancel.equals(actionCommand)) {
task.cancel();
inAwt(loginDescriptor, new Object[]{login, turnOff});
loginDescriptor.setMessage(loginPanel);
loginPanel.warning("aborted");
}
}
}
}
日尔卡