简体   繁体   English

从另一个类获取值jtextfield到actionperformed方法

[英]get value jtextfield from another class to actionperformed method

I have two class, one is for swing and main class, one is for actionperformed method 我有两个班级,一个是秋千班和主班,一个是动作执行方法

//class swing
package system;
import javax.swing.*;
import java.awt.event.*;

public class GUI{
   JFrame frame = new JFRame("frame");
   JTextField tx = new JTextField(10);
   JButton bt = new JButton("button");

   public void getTx(){
      return tx.getText();
   }

   public void init(){
      frame.setSize(200, 200);
      frame.add(tx);
      frame.add(bt);
      tx.setBounds(20, 20, 140, 50);
      bt.setBounds(20, 100, 120, 40);
      btaddcom.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            control.btclicked(e);
        }
    });
   }

   public GUI(){
      init();
   }

   public static void main(String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable(){
        public void run() {
            new GUI().frame.setVisible(true);
        }
    });
  }
}

below is my other class 下面是我的另一堂课

package system;
import java.awt;
import java.awt.event.*;

public abstract control implements ActionListener{
    public static void btclicked(ActionEvent e){
         GUI gui = new GUI();
         String txf = gui.getTx();
         JOptionPane.showMessageDialog(null, txf);
    }
}

My question is why I cannot get the value from JTextField tx, because it is always blank whenever text I filled it. 我的问题是为什么我无法从JTextField获取值tx,因为每当我填充文本时它总是空白的。 Thanks 谢谢

You're making a new instance of the GUI class, so all the fields will be empty/reset. 您正在创建GUI类的新实例,因此所有字段都将为空/重置。

Here's what you should do: 这是你应该做的:

public abstract control implements ActionListener{
    public static void btclicked(ActionEvent e, GUI gui){
        String txf = gui.getTx();
        JOptionPane.showMessageDialog(null, txf);
    }
}

Usage: 用法:

btaddcom.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e){
        control.btclicked(e, GUI.this);
    }
});

You are creating a new instance GUI . 您正在创建一个新的实例GUI This has no relation ship to what is begin displayed on the screen, so any updates you try to make to it won't be reflected on the screen... 这与屏幕上显示的内容无关,因此您尝试对其进行的任何更新都不会反映在屏幕上...

Instead, you should be passing either some kind of model (preferrabbly) or a reference to the GUI or JTextField . 相反,您应该传递某种模型(首选)或GUIJTextField的引用。

Something more like... 更像是......

public abstract control implements ActionListener{
    public static void btclicked(ActionEvent e, GUI gui){
         String txf = gui.getTx();
         JOptionPane.showMessageDialog(null, txf);
    }
}

For example. 例如。

Personally, I prefer to provide a model of some kind, as it prevent exposing additional content to the caller, as your btclicked method now has full control over your GUI class, which you may not want 就个人而言,我更喜欢提供某种模型,因为它可以防止向调用者公开其他内容,因为您的btclicked方法现在可以完全控制您的GUI类,您可能不希望这样做

It seems as if this was solved if I read the comments correctly. 如果我正确地阅读了这些评论,似乎这已经解决了。 But just in case, there's also a basic syntax error. 但为了以防万一,还有一个基本的语法错误。 Your getTx() method is returning a string but you have it listed as void. 你的getTx()方法返回一个字符串但你把它列为void。 I'm sure this was caught right away but I figure it doesn't hurt to point it out just in case. 我确定这是马上被抓住了但是我认为为了以防万一而指出它并没有什么坏处。 Cheers. 干杯。

public String getTx(){
    return tx.getText();
}

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

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