2

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. Thanks

4

3 回答 3

4

You're making a new instance of the GUI class, so all the fields will be empty/reset.

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);
    }
});
于 2013-08-09T02:09:09.783 回答
3

You are creating a new instance 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.

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

于 2013-08-09T02:13:52.867 回答
0

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. 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();
}
于 2013-10-15T08:31:41.277 回答