繁体   English   中英

将Jframe Jtextfield发送到另一个类

[英]Sending Jframe Jtextfield to another class

我有一个有textfieldbuttonJFrame 它应该在程序开始时可见,当我点击按钮时,它应该变为不可见,并将textfield的文本发送到另一个类。 但它什么也没发送,当我点击按钮时,IDE进入调试模式。

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;
    }
}

我的另一个类调用Jframe:

System.out.println(JframeFoo.getusername);

暂时忽略向用户跳出多个JFrame并不是一个很好的用户界面设计,对于一个对象与另一个对象进行通信,它必须具有对另一个对象的有效引用。 (抱歉被女儿打断了)。

因此,对于一个JFrame类从另一个获取信息,它必须具有对获取文本的第一个对象的引用,并且我没有看到您传递该引用,例如在构造函数或setter方法中。

因此,例如,如果Class1的对象具有Class2对象所需的信息,那么传递它的一种方法是为Class2提供对Class1的有效实例的引用,然后让Class2从Class1实例获取信息。 例如,

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);
   }


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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