mport java.awt.Color;
import java.awt.Container;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Gui extends JFrame{
JLabel labelName =new JLabel("姓名");
JLabel labelAge =new JLabel("年龄");
public JTextField textName = new JTextField();
public JTextField textAge = new JTextField();
JButton button = new JButton("确定");
public Gui(){
Container con = this.getContentPane();
con.setLayout(null);
labelName.setBounds(30,30,30,20);
labelName.setForeground(Color.BLUE);
labelAge.setBounds(30,60,30,20);
labelAge.setForeground(Color.BLUE);
textName.setBounds(80,30,100,20);
textAge.setBounds(80,60,100,20);
button.setBounds(120,100,60,30);
button.setMargin(new Insets(1,1,1,1));
con.add(button);
con.add(textAge);
con.add(labelName);
con.add(labelAge);
con.add(textName);
button.addActionListener(new Action(this));
this.setSize(300,200);
this.setTitle("信息录入系统");
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Gui();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Action implements ActionListener{
private String name;
private String age;
Gui g;
public Action(Gui g) {
this.g=g;
this.age=g.textAge.getText();
this.name=g.textName.getText();
}
public void actionPerformed(ActionEvent e) {
System.out.println(age);
System.out.println(name);
System.out.println("测试");
}
public static void main(String[] args) {
}
我想在Action类中获取Gui类中的文本框输入信息,怎么打印出来是 什么也没有啊,也不是null,哪错了,好久没写GUI了忘了,以前我记得这样获取啊
----------------解决方案--------------------------------------------------------
晕了, this.age=g.textAge.getText();
this.name=g.textName.getText();
这个我把写在构造方法里面了,应该写在public void actionPerformed(ActionEvent e){}
里,小小的问题,弄怎么久
----------------解决方案--------------------------------------------------------