| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 476 人关注过本帖
标题:[求助]gui中传参问题
只看楼主 加入收藏
yinxuchina
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-2-1
收藏
 问题点数:0 回复次数:1 
[求助]gui中传参问题

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了忘了,以前我记得这样获取啊



搜索更多相关主题的帖子: gui 
2007-04-15 05:32
yinxuchina
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-2-1
收藏
得分:0 
晕了, this.age=g.textAge.getText();
this.name=g.textName.getText();
这个我把写在构造方法里面了,应该写在public void actionPerformed(ActionEvent e){}
里,小小的问题,弄怎么久

2007-04-15 05:43
快速回复:[求助]gui中传参问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014311 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved