jDialog do not dispose



我有一个jFrame,当我按F2调用jDialog时,在这个jDialog中放入一些信息,然后当按下或单击jButton或Enter时,我得到信息并处理jDialog。

然而,jDialog并没有关闭,而是获取了我请求的所有信息。

这是代码:

/**
* Creates new form DialogCPF
*/
public DialogCPF(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
jFormattedTextField1.addKeyListener(new Tecla() {
@Override
public void keyReleased(KeyEvent e) {

}
});
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
// TODO add your handling code here:

ConsumirWS2 WS = new ConsumirWS2();
try {
WS.Buscar();
} catch (Exception ex) {
Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
}
jButtonSalvar.setEnabled(true);
jButtonExcluir.setEnabled(false);
jButtonINSERIR.setEnabled(false);
jButtonALTERAR.setEnabled(false);
jButtonPROCURAR.setEnabled(false);
jTextFieldAPELIDO.setEnabled(true);
jTextFieldNOME.setEnabled(true);
jFormattedTextFieldCPF.setEnabled(true);
jFormattedTextFieldDATA.setEnabled(true);
jTextFieldID.setEnabled(false);
/*DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), false);
dialog.dispose();*/
DialogCPF A = (DialogCPF)evt.getSource();
A.dispose();

}                                        
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DialogCPF.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the dialog */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});

dialog.addKeyListener(new KeyListener(){  
@Override
public void keyTyped(KeyEvent e) {
// Nothing
}
@Override
public void keyPressed(KeyEvent e) {
// Nothing 
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
ConsumirWS2 WS = new ConsumirWS2();
try {
WS.Buscar();
} catch (Exception ex) {
Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
}
jButtonSalvar.setEnabled(true);
jButtonExcluir.setEnabled(false);
jButtonINSERIR.setEnabled(false);
jButtonALTERAR.setEnabled(false);
jButtonPROCURAR.setEnabled(false);
jTextFieldAPELIDO.setEnabled(true);
jTextFieldNOME.setEnabled(true);
jFormattedTextFieldCPF.setEnabled(true);
jFormattedTextFieldDATA.setEnabled(true);
jTextFieldID.setEnabled(false);
DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
//dialog.setEnabled(false);
dialog.dispose();
}
}
});        
dialog.setVisible(true);
}
});
}
// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
public static javax.swing.JFormattedTextField jFormattedTextField1;
private javax.swing.JLabel jLabel1;
public javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
// End of variables declaration                   
}

我已经尝试过这个,这个和这个解决方案,然而,我无法带来解决方案

这个。。。

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ConsumirWS2 WS = new ConsumirWS2();
try {
WS.Buscar();
} catch (Exception ex) {
Logger.getLogger(DialogCPF.class.getName()).log(Level.SEVERE, null, ex);
}
//...
DialogCPF A = (DialogCPF) evt.getSource();
A.dispose();
}

不太可能工作,因为ActionEvent的来源不太可能是对话框。我还关心ConsumirWS2实例的创建,这不是对话框的责任,对话框应该允许调用方提取它需要的信息,可能是通过观察者模式。

这个。。。

dialog.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// Nothing
}
@Override
public void keyPressed(KeyEvent e) {
// Nothing 
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
//...
}
}
});

这是一个坏主意,原因有很多,除了对用户来说不明显之外,任何将焦点从对话框中移开的组件(如文本字段(都会阻止它的执行。相反,请参阅如何将Java默认按钮设置为对ENTER键_released_作出反应?获得更好的解决方案

但当我们在这里的时候。。。

@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
//...
DialogCPF dialog = new DialogCPF(new javax.swing.JFrame(), true);
//dialog.setEnabled(false);
dialog.dispose();
}
}

不起作用。您创建一个对话框的新实例并立即处理它。。。这将如何影响屏幕上触发此事件的对话框实例?!

一般来说,你需要在对话框的当前实例上调用dispose,你最好看看如何制作对话框和如何制作框架,当然你最好避免使用表单编辑器

作为一个概念性的例子

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(50, 50, 50, 50));
JButton btn = new JButton("Collect data");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FormData formData = DataEntryPane.showDialog(TestPane.this, null);
if (formData != null) {
JOptionPane.showMessageDialog(TestPane.this, "I have new data");
} else {
JOptionPane.showMessageDialog(TestPane.this, "No new data for me");
}
}
});
add(btn);
}
}
public static class FormData {
private String importantStuff;
public FormData(String importantStuff) {
this.importantStuff = importantStuff;
}
public String getImportantStuff() {
return importantStuff;
}
}
public static class DataEntryPane extends JPanel {
public interface Observer {
public void formValidationRequired(DataEntryPane source);
}
private Observer observer;
private JTextField textField;
public DataEntryPane(Observer observer, FormData formData) {
this.observer = observer;
setLayout(new GridBagLayout());
textField = new JTextField(10);
add(new JLabel("Some important stuff: "));
add(textField);
if (formData != null) {
textField.setText(formData.getImportantStuff());
}
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (observer != null) {
observer.formValidationRequired(DataEntryPane.this);
}
}
};
textField.addActionListener(actionListener);
}
public String getImportantStuff() {
return textField.getText();
}
public static FormData showDialog(Component parent, FormData data) {
enum State {
OK, CANCEL
}
// You could use a JOptionPane instead, but where's the fun in that
JDialog dialog = new JDialog(SwingUtilities.windowForComponent(parent));
dialog.setTitle("All you data is belong to us");
dialog.setModal(true);
JPanel content = new JPanel(new BorderLayout());
content.setBorder(new EmptyBorder(32, 32, 32, 32));
dialog.setContentPane(content);
DataEntryPane entryPane = new DataEntryPane(new Observer() {
@Override
public void formValidationRequired(DataEntryPane source) {
// Do what ever validation you need
boolean isValid = true;
if (isValid) {
source.putClientProperty("state", State.CANCEL);
dialog.dispose();
}
}
}, data);
entryPane.setBorder(new EmptyBorder(32, 32, 32, 32));
dialog.add(entryPane);
JButton okayButton = new JButton("OK");
JButton cancelButton = new JButton("Cancel");
okayButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
entryPane.putClientProperty("state", State.OK);
dialog.dispose();
}
});
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
entryPane.putClientProperty("state", State.CANCEL);
dialog.dispose();
}
});
JPanel actionPane = new JPanel();
actionPane.setBorder(new EmptyBorder(8, 8, 8, 8));
actionPane.add(okayButton);
actionPane.add(cancelButton);
dialog.add(actionPane, BorderLayout.SOUTH);
dialog.getRootPane().setDefaultButton(okayButton);
dialog.pack();
dialog.setLocationRelativeTo(parent);
dialog.setVisible(true);
State state = (State) entryPane.getClientProperty("state");
// Only process the information if the user selected "okay"
if (state != null && State.OK == state) {
System.out.println("Validate user input");
// Do what ever validation you need
boolean isValid = true;
if (isValid) {
String importantStuff = entryPane.getImportantStuff();
return new FormData(importantStuff);
}
} else {
System.out.println("User cancelled operation");
}
return null;
}
}
}

最新更新