简体   繁体   中英

Sending Jframe Jtextfield to another class

I have a JFrame that has a textfield and a button . It should become visible at the start of program and when I click on the button, It should become invisible and send the text of textfield to another class. but It send nothing and when I click on the button the IDE goes to the debug mode.

public class JframeFoo extends JFrame {

    private String username = new String();

    public JframeFoo() {
        // --------------------------------------------------------------
        // Making Frame for login

        final JTextField usernameFiled = new JTextField();
        this.add(usernameFiled);

        JButton signinButton = new JButton();
        // ------------------------------------------------------------

        signinButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                username = usernameFiled.getText();
                setVisible(false);
                Main.mainpage.setVisible(true);

            }
        });
        // --------------------------------------------------------------------------

    }

    public String getuserName() {
        return this.username;
    }
}

my another class calls Jframe:

System.out.println(JframeFoo.getusername);

Ignoring for a moment that having multiple JFrames jumping out at the user is not a great user interface design, for one object to communicate with another object, it must have a valid reference to the other object. (sorry interrupted by daughter).

So for one JFrame class to get information from the other, it must have a reference to the first object that gets the text, and I don't see you passing that reference, such as in a constructor or setter method.

So for instance if an object of Class1 has information that an object of Class2 needs, then one way to pass it is to give Class2 a reference to the valid instance of Class1, and then have Class2 get the information from the Class1 instance. eg,

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class ClassMain {

   private static void createAndShowGui() {
      ClassMain mainPanel = new ClassMain();

      JFrame frame = new Class1();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class Class1 extends JFrame {
   private JTextField textfield = new JTextField(10);

   public Class1() {
      JPanel contentPane = (JPanel) getContentPane();
      contentPane.setLayout(new FlowLayout());
      add(textfield);
      add(new JButton(new AbstractAction("Open Window") {

         @Override
         public void actionPerformed(ActionEvent arg0) {
            Class2 class2 = new Class2(Class1.this);
            Class1.this.setVisible(false);
            class2.pack();
            class2.setVisible(true);
            class2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         }
      }));
   }

   public String getTextfieldText() {
      return textfield.getText();
   }
}

class Class2 extends JFrame {
   private Class1 class1;
   private JLabel label = new JLabel("");

   public Class2(Class1 class1) {
      this.class1 = class1;
      label.setText(class1.getTextfieldText());
      add(label);
   }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM