java中子对话框的实现,可以用回调函数的,但只实现了通过属性传值
package JFrameFather;
import java.awt.BorderLayout;
public class JFrameFather extends JFrame {
private JPanel contentPane;
private JTextField textField;
public JTextField getTextField() {
return textField;
}
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrameFather frame = new JFrameFather();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public JFrameFather() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel("\u4EBA\u5458\u540D\u5355\u8868");
contentPane.add(lblNewLabel, BorderLayout.NORTH);
textField = new JTextField();
contentPane.add(textField, BorderLayout.CENTER);
textField.setColumns(10);
JButton button_add = new JButton("\u65B0\u589E");
button_add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialogSon jdi = new JDialogSon(JFrameFather.this,true);
jdi.setVisible(true);
}
});
contentPane.add(button_add, BorderLayout.SOUTH);
}
}
package JFrameFather;
import java.awt.BorderLayout;
public class JDialogSon extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
JDialogSon dialog = new JDialogSon(null,true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public JDialogSon(final JFrameFather owner, boolean modal) {
super(owner, modal);
setBounds(100, 100, 285, 109);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(new BorderLayout(20, 10));
JPanel panel = new JPanel();
contentPanel.add(panel, BorderLayout.SOUTH);
{
JButton button = new JButton("\u786E\u5B9A");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
owner.getTextField().setText(textField.getText());
JDialogSon.this.dispose();
owner.repaint();
}
});
panel.add(button);
}
{
textField = new JTextField();
contentPanel.add(textField, BorderLayout.NORTH);
textField.setColumns(10);
}
}
}