用到父子窗口传值,去网上搜了一堆废话,感觉很不爽,自己研究了半天,原来很简单。这个例子还用到了线程。不用会更清楚。不多说了,上代码,注意是怎样传值的
//对话框
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) { }
private void jButton0MouseMouseClicked(MouseEvent event) { String tt=jTextArea0.getText(); this.parent.getjTextField0().setText(tt); System.out.println("6666666666"); }
private void windowWindowClosing(WindowEvent event) { String tt=jTextArea0.getText(); this.parent.getjTextField0().setText("hhhaayyy"); 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) { }
private void jButton0MouseMouseClicked(MouseEvent event) { //JDialog tt=new test(this,"dialogtest",true); //tt.show(); //System.out.println("sdgf"); ThreadTeat rrr=new ThreadTeat(this); Thread th1=new Thread(rrr); th1.start(); }
}
///线程
package des;
public class ThreadTeat implements Runnable{ freamtest ff; ThreadTeat(){} ThreadTeat(freamtest ff){ this.ff=ff; } public static void main(String[] args) { ThreadTeat rrr=new ThreadTeat(); Thread th1=new Thread(rrr); th1.start(); }
@Override public void run() { // TODO Auto-generated method stub int c=0; while(true){ if(c==5){ test tt=new test(ff); tt.show(); } try { Thread.sleep(1000); c++; System.out.println("c is ---"+c); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
}
简单有很清楚,真不明白,这么简单的东西怎么网上搜不到,全是没用的废话,所以不明的菜鸟别乱传别人的东西,整的网上有用的东西不多,垃圾导出都是!