用到父子視窗傳值,去網上搜了一堆廢話,感覺很不爽,自己研究了半天,原來很簡單。這個例子還用到了線程。不用會更清楚。不多說了,上代碼,注意是怎樣傳值的 //////對話方塊package des;import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;//VS4E -- DO NOT REMOVE THIS LINE!
public class test extends JDialog { private static final long serialVersionUID = 1L;
private JButton jButton0;
private JLabel jLabel0;
private JTextArea jTextArea0;
private JScrollPane jScrollPane0;
freamtest parent;
private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
public test() {
initComponents();
} public test(freamtest parent) {
super(parent);
this.parent=parent;
initComponents();
} public test(freamtest parent, boolean modal) {
super(parent, modal);
this.parent=parent;
initComponents();
} public test(freamtest parent, String title) {
super(parent, title);
this.parent=parent;
initComponents();
} public test(freamtest parent, String title, boolean modal) {
super(parent, title, modal);
this.parent=parent;
initComponents();
} public test(freamtest parent, String title, boolean modal,
GraphicsConfiguration arg) {
super(parent, title, modal, arg);
this.parent=parent;
initComponents();
} public test(Dialog parent) {
super(parent);
initComponents();
} public test(Dialog parent, boolean modal) {
super(parent, modal);
initComponents();
} public test(Dialog parent, String title) {
super(parent, title);
initComponents();
} public test(Dialog parent, String title, boolean modal) {
super(parent, title, modal);
initComponents();
} public test(Dialog parent, String title, boolean modal,
GraphicsConfiguration arg) {
super(parent, title, modal, arg);
initComponents();
} public test(Window parent) {
super(parent);
initComponents();
} public test(Window parent, ModalityType modalityType) {
super(parent, modalityType);
initComponents();
} public test(Window parent, String title) {
super(parent, title);
initComponents();
} public test(Window parent, String title, ModalityType modalityType) {
super(parent, title, modalityType);
initComponents();
} public test(Window parent, String title, ModalityType modalityType,
GraphicsConfiguration arg) {
super(parent, title, modalityType, arg);
initComponents();
} private void initComponents() {
setFont(new Font("Dialog", Font.PLAIN, 12));
setBackground(Color.white);
setForeground(Color.black);
setLayout(new GroupLayout());
add(getJButton0(), new Constraints(new Leading(297, 10, 10), new Leading(45, 10, 10)));
add(getJLabel0(), new Constraints(new Leading(149, 10, 10), new Leading(21, 12, 12)));
add(getJScrollPane0(), new Constraints(new Leading(89, 100, 12, 12), new Leading(64, 80, 10, 10)));
addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent event) {
windowWindowClosed(event);
}
public void windowClosing(WindowEvent event) {
windowWindowClosing(event);
}
});
setSize(462, 240);
} private JScrollPane getJScrollPane0() {
if (jScrollPane0 == null) {
jScrollPane0 = new JScrollPane();
jScrollPane0.setViewportView(getJTextArea0());
}
return jScrollPane0;
} private JTextArea getJTextArea0() {
if (jTextArea0 == null) {
jTextArea0 = new JTextArea();
jTextArea0.setText("jTextArea0");
}
return jTextArea0;
} private JLabel getJLabel0() {
if (jLabel0 == null) {
jLabel0 = new JLabel();
jLabel0.setText("dialog");
}
return jLabel0;
} private JButton getJButton0() {
if (jButton0 == null) {
jButton0 = new JButton();
jButton0.setText("jButton0");
jButton0.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
jButton0MouseMouseClicked(event);
}
});
}
return jButton0;
} private static void installLnF() {
try {
String lnfClassname = PREFERRED_LOOK_AND_FEEL;
if (lnfClassname == null)
lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lnfClassname);
} catch (Exception e) {
System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
+ " on this platform:" + e.getMessage());
}
} /**
* Main entry of the class.
* Note: This class is only created so that you can easily preview the result at runtime.
* It is not expected to be managed by the designer.
* You can modify it as you like.
*/
public static void main(String[] args) {
installLnF();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
test dialog = new test();
dialog.setDefaultCloseOperation(test.DISPOSE_ON_CLOSE);
dialog.setTitle("test");
dialog.setLocationRelativeTo(null);
dialog.getContentPane().setPreferredSize(dialog.getSize());
dialog.pack();
dialog.setVisible(true);
}
});
}
//close
private void windowWindowClosed(WindowEvent event) {
System.out.println("yyyyyy");
//jTextField0.
}} /////父視窗package des;import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;//VS4E -- DO NOT REMOVE THIS LINE!
public class freamtest extends JFrame { private static final long serialVersionUID = 1L;
private JButton jButton0;
private JTextField jTextField0;
String vv;
private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
public JTextField getjTextField0() {
return jTextField0;
} public void setjTextField0(JTextField jTextField0) {
this.jTextField0 = jTextField0;
} public String getVv() {
return vv;
} public void setVv(String vv) {
this.vv = vv;
} public freamtest() {
initComponents();
} private void initComponents() {
setLayout(new GroupLayout());
add(getJButton0(), new Constraints(new Leading(129, 10, 10), new Leading(40, 10, 10)));
add(getJTextField0(), new Constraints(new Leading(31, 254, 12, 12), new Leading(86, 47, 10, 10)));
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
mouseMouseClicked(event);
}
});
setSize(320, 240);
} private JTextField getJTextField0() {
if (jTextField0 == null) {
jTextField0 = new JTextField();
jTextField0.setText("jTextField0");
}
return jTextField0;
} private JButton getJButton0() {
if (jButton0 == null) {
jButton0 = new JButton();
jButton0.setText("tttttt");
jButton0.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event) {
jButton0MouseMouseClicked(event);
}
});
}
return jButton0;
} private static void installLnF() {
try {
String lnfClassname = PREFERRED_LOOK_AND_FEEL;
if (lnfClassname == null)
lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(lnfClassname);
} catch (Exception e) {
System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
+ " on this platform:" + e.getMessage());
}
} /**
* Main entry of the class.
* Note: This class is only created so that you can easily preview the result at runtime.
* It is not expected to be managed by the designer.
* You can modify it as you like.
*/
public static void main(String[] args) {
installLnF();
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
freamtest frame = new freamtest();
frame.setDefaultCloseOperation(freamtest.EXIT_ON_CLOSE);
frame.setTitle("freamtest");
frame.getContentPane().setPreferredSize(frame.getSize());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
} private void mouseMouseClicked(MouseEvent event) {