java gui 窗口 传值_[求助]gui中传参问题

这篇博客探讨了在Java GUI应用程序中如何从窗口组件正确获取用户输入值的问题。作者在ActionListener类中初始化文本框值时遇到了困扰,错误地将`this.age=g.textAge.getText();`和`this.name=g.textName.getText();`放入构造函数,而非`actionPerformed`方法。通过调整代码,将这两行移到事件处理方法内,成功解决了无法获取文本框输入的问题。
摘要由CSDN通过智能技术生成

[求助]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

----------------解决方案--------------------------------------------------------

晕了, this.age=g.textAge.getText();

this.name=g.textName.getText();

这个我把写在构造方法里面了,应该写在public void actionPerformed(ActionEvent e){}

里,小小的问题,弄怎么久

----------------解决方案--------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值