比如记事本中的查询Frame,和JTextArea的主窗体的参数如何传递传递?????希望能说的详细点,先谢了!

解决方案 »

  1.   

    例如
    class NoteFrame extends JFrame implements ActionListener//主窗体
    {
       .......
       public void find()
      {
          SearchFrame sf=new SearchFrame();
          sf.setVisible(true);
       }
     .........
    }
    class SearchFrame extends JFrame implements ActionListener//查找窗体
    {
            ....................
          SearchFrame();
            ...................
    }如何将查找窗体的参数传入主窗体??
    如果通过构造函数传递,应该怎么实现,请简单举个例子!!谢谢了
      

  2.   

    class SearchFrame extends JFrame implements ActionListener//查找窗体 

            .................... 
          SearchFrame(); 
            ................... 
         // 打开主窗体
         NoteFrame frame = new NoteFrame ("Hello","World");
    } class NoteFrame extends JFrame implements ActionListener//主窗体 

       // 在主窗体声明两个变量用来接住传过来的参数
       String a;
       String b;
       // 给主窗体加个构造方法,s1,s2就是子窗体穿过来的值
       public NoteFrame(String s1,String s2){
         a=s1;
         b=s2;
        // 这样就可以在主窗口调用 a,b的值了
      }
      ....... 
      public void find() 
      { 
          SearchFrame sf=new SearchFrame(); 
          sf.setVisible(true); 
      } 
    ......... 
      

  3.   

    貌似可以将查询窗体searchFrame做成内部类将其放在NoteFrame里面也不知道对不对,请各位大侠指正!!
      

  4.   

    参考下吧:
    package com.test;import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;public class NoteFrame extends JFrame { private JTextField txt1; private JButton btn; public NoteFrame() {
    txt1 = new JTextField(10);
    btn = new JButton("open subFrame");
    btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
    new SearchFrame(txt1);
    } }); add(txt1, BorderLayout.NORTH);
    add(btn, BorderLayout.CENTER); setTitle("NoteFrame");
    setSize(400, 200);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } public static void main(String[] args) {
    new NoteFrame();
    }
    }class SearchFrame extends JFrame { private JButton btn;
    private JTextField txt; public SearchFrame(JTextField txt) {
    this.txt = txt;
    btn = new JButton("ok");
    btn.addActionListener(new BtnAction(this.txt)); this.add(this.btn, BorderLayout.CENTER); setTitle("SearchFrame");
    setSize(400, 200);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } class BtnAction implements ActionListener { private JTextField txt; public BtnAction(JTextField txt) {
    this.txt = txt;
    } public void actionPerformed(ActionEvent e) {
    this.txt.setText("hello world");
    } }
    }
      

  5.   

    最简单的方法是你的主类包含两个Frame作为成员变量,再定义第三个成员变量传递数据,使用主类的方法来操作成员变量。