不一定呀,可以传递主窗口的reference(作为参数)给被弹开的窗口,不就可以实现对其的引用了么?

解决方案 »

  1.   

    你在确定的按钮里面打开这个组窗口就可以...如果么传递参数就声明变量为static的,然后就可以传递参数给这个static的,,这个和c++不同的.!!!!!!
      

  2.   

    no, use the main frame as the parameters of the second one...
      

  3.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Frame1 extends JFrame {
      JPanel contentPane;
      BorderLayout borderLayout1 = new BorderLayout();
      JPanel jPanel1 = new JPanel();
      JButton jButton1 = new JButton();
      JPanel jPanel2 = new JPanel();
      GridLayout gridLayout1 = new GridLayout();
      JPanel jPanel3 = new JPanel();
      JPanel jPanel4 = new JPanel();
      JTextField jTextField2 = new JTextField();
      JTextField jTextField1 = new JTextField();
      JLabel jLabel1 = new JLabel();
      JLabel jLabel2 = new JLabel();
      FlowLayout flowLayout1 = new FlowLayout();
      FlowLayout flowLayout2 = new FlowLayout();  /**Construct the frame*/
      public Frame1() {
        //enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      /**Component initialization*/
      private void jbInit() throws Exception  {
        this.setSize(new Dimension(405, 163));
        this.setTitle("Frame Title");
        this.getContentPane().setBackground(new Color(245, 246, 247));
        //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
        contentPane = (JPanel) this.getContentPane();
        contentPane.setLayout(borderLayout1);
        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            jButton1_actionPerformed(e);
          }
        });
        jPanel2.setBackground(new Color(245, 246, 247));
        jPanel2.setLayout(gridLayout1);
        gridLayout1.setRows(2);
        jLabel1.setFont(new java.awt.Font("Dialog", 1, 16));
        jLabel1.setText("用户名:");
        jLabel2.setFont(new java.awt.Font("Dialog", 1, 16));
        jLabel2.setText("密  码:");
        jPanel4.setLayout(flowLayout1);
        flowLayout1.setHgap(20);
        jPanel3.setLayout(flowLayout2);
        flowLayout2.setHgap(20);
        jPanel3.setBackground(new Color(245, 246, 247));
        jPanel4.setBackground(new Color(245, 246, 247));
        jTextField1.setMinimumSize(new Dimension(65, 24));
        jTextField1.setPreferredSize(new Dimension(65, 24));
        jTextField2.setPreferredSize(new Dimension(65, 24));
        jPanel1.setBackground(new Color(245, 246, 247));
        contentPane.add(jPanel1, BorderLayout.SOUTH);
        jPanel1.add(jButton1, null);
        contentPane.add(jPanel2, BorderLayout.CENTER);
        jPanel2.add(jPanel3, null);
        jPanel3.add(jLabel1, null);
        jPanel3.add(jTextField2, null);
        jPanel2.add(jPanel4, null);
        jPanel4.add(jLabel2, null);
        jPanel4.add(jTextField1, null);
      }
      /**Overridden so we can exit when window is closed*/
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }class Frame2 extends JFrame {
      GridLayout gridLayout1 = new GridLayout();
      JPanel jPanel1 = new JPanel();
      JPanel jPanel2 = new JPanel();
      JLabel jLabel1 = new JLabel();
      JLabel jLabel2 = new JLabel();  public Frame2() {
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      private void jbInit() throws Exception {
        this.setSize(new Dimension(100,80));
        this.getContentPane().setLayout(gridLayout1);
        gridLayout1.setRows(2);
        jLabel1.setText(jTextField1.getText());
        jLabel2.setText(jTextField2.getText());
        this.getContentPane().add(jPanel1, null);
        jPanel1.add(jLabel1, null);
        this.getContentPane().add(jPanel2, null);
        jPanel2.add(jLabel2, null);
      }
    }  void jButton1_actionPerformed(ActionEvent e) {
        new Frame2().show();
      }
    }
      

  4.   

    不能直接对主窗口的JTextFiled获得吗?
    假如主窗口protected JTextFiled为InputName和InputPass
    不能直接在TestDialog里获得InputName的值吗????
    类似javascript里的,弹开窗口可以对父窗口用opener.document.xxxx.value……获得某个input框的值类为MainFrame.java
    调用
        TestBut.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(ActionEvent e) {
            TestBut_actionPerformed(e);
          }
        });  void TestBut_actionPerformed(ActionEvent e) {
        TestDialog td=new TestDialog(this,"测试……",true);
        Dimension tdSize=td.getPreferredSize();
        Dimension frmSize=getSize();
        ctd.setSize(300,150);
        Point loc=getLocation();
        ctd.setLocation((frmSize.width-300)/2+loc.x,(frmSize.height-200)/2+loc.y);
        ctd.show();
      }TestDialog.java 构造
      public TestDialog(Frame frame, String title, boolean modal) {
        super(frame, title, modal);
        try {
          jbInit();
          pack();
        }
        catch(Exception ex) {
          ex.printStackTrace();
        }
      }
      

  5.   

    crazyboy123(狂徒) 
    如果frame2不是frame1的内部类,    jLabel1.setText(jTextField1.getText());
        jLabel2.setText(jTextField2.getText());
    这里就会有错
      

  6.   

    两种方法:
    ====================A==================================
    // main
    public class MainFrame extends JFrame {
    ....
     static String inputName;
     static String inputPass; public void actionPerformed(...) {
        inputName = txtName.getText();
        inputPass = txtPswd.getText();
        ...
        // show ChildFrame
        ...
     }// in Child
     class ChildFrame extends ...
       MainFrame.inputName
       MainFrame.inputPass====================B==================================
    // main
    public class MainFrame extends JFrame {
      MainFrame handle;
      String inputName = txtName.getText();
      String inputPass = txtPswd.getText();  public MainFrame() {
        handle = this;
      }
      public void actionPerformed(...) {
        inputName = txtName.getText();
        inputPass = txtPswd.getText();
        ChildFrame child = new ChildFrame(handle);
        ...// in Child
     class ChildFrame extends ...
       MainFrame parent;
       public ChildFrame(MainFrame p) {
          parent = p;
       }
       ...
       parent.inputName
       parent.inputPass
      

  7.   

    gamebus() 
    当然了如果要达到你所说的不用给弹出窗口传参数,得到主窗口的输入信息。除了使用内部类或接口,我还没有别的好方法。 另外最好不要引用主窗口,两个窗口是相互独立的在弹出窗口中引用主窗口,会增加两个类之间的偶合度。弹出窗口将降低复用性。最好是主窗口获得输入信息,用弹出窗口的方法将信息传给弹出窗口,再显示。