java swing 彈出對話方塊與父視窗傳值執行個體

來源:互聯網
上載者:User
用到父子視窗傳值,去網上搜了一堆廢話,感覺很不爽,自己研究了半天,原來很簡單。這個例子還用到了線程。不用會更清楚。不多說了,上代碼,注意是怎樣傳值的 //////對話方塊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();
   }
  }
  
 }}
  簡單有很清楚,真不明白,這麼簡單的東西怎麼網上搜不到,全是沒用的廢話,所以不明的菜鳥別亂傳別人的東西,整的網上有用的東西不多,垃圾匯出都是!         

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.