【求助】Java GUI 事件监听器的参数传递 本帖最后由 tianranhe 于 2013-03-07 16:41:39 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 this.taskNameNew = taskNameNew; 你这是干什么,根本就没有得到文本框里的值啊;在修改按钮事件里:frame.getbutton().setText(jtext.getText()),然后刷新下fram1 有啊,在Frame2第5行:tf.getText()button2.addActionListener(new Listener2(button1, tf.getText(),frame1)); 有啊,在 Frame2第5行:tf.getText(),调用的时候赋给参数了button2.addActionListener(new Listener2(button1, tf.getText(),frame1)); 我做了如下修改:import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JTextField;public class Frame1 { static JButton button1; public static void main(String []args){ button1 = new JButton("button1"); JFrame frame1 = new JFrame("frame1"); button1.addActionListener(new Listener1(button1, frame1)); frame1.add(button1); frame1.setLayout(new FlowLayout()); frame1.setBounds(400, 100, 400, 300); frame1.setVisible(true); } static JButton getbun(){ return button1; }}class Frame2{ JButton button2; static JTextField tf; public Frame2(JButton button1, JFrame frame1){ tf = new JTextField(20); JButton button2 = new JButton("修改"); button2.addActionListener(new Listener2(button1, tf.getText(),frame1)); JFrame frame2 = new JFrame("frame2"); frame2.setLayout(new FlowLayout()); frame2.setBounds(450, 150, 300, 200); frame2.add(tf); frame2.add(button2); frame2.setVisible(true); } static JTextField gettext(){ return tf; } }class Listener1 implements ActionListener{ JButton button; JFrame frame; public Listener1(JButton button, JFrame frame){ this.frame = frame; this.button = button; } public void actionPerformed(ActionEvent e){ Frame2 frame2 = new Frame2(button,frame); }}class Listener2 implements ActionListener{ String taskNameOld, taskNameNew; JButton button; JFrame frame; public Listener2(JButton button, String stringNew, JFrame frame){ this.frame = frame; this.taskNameOld = button.getText(); this.taskNameNew = taskNameNew; } public void actionPerformed(ActionEvent e){ if(taskNameNew != taskNameOld) { Frame1.getbun().setText(Frame2.gettext().getText().toString());//运行时Eclipse报错 frame.repaint(); } }} 你之前的button.setText(taskNameNew);这行中的button是什么?没有被初始化的按钮 button空指针了。因为在Listener2的构造方法中少写了一句this.button = button; 好了,原来要return一下。太感谢你了 我把button1,从Frame1传到Frame2, 等于已经初始化了啊我做一个小东西,这只是一个调试用的demo 很简单你少了this.button=button;所以button是空,所以是空指针异常然后你获取tf内容应该是监听方法里面获取,如在actionPerformed里面像你button2.addActionListener(new Listener2(button1, tf.getText(),frame1));这样的话在初始化的时候就已经获取了,这时tf里面是没有内容的,应该写在监听方法里面,这样才是点击按钮时获取改了如下class Listener2 implements ActionListener{ String taskNameOld, taskNameNew; JButton button; JFrame frame; JTextField tf; public Listener2(JButton button,JTextField tf, JFrame frame){ this.frame = frame; this.button = button; this.taskNameOld = button.getText(); this.tf = tf; } public void actionPerformed(ActionEvent e){ if(taskNameNew != taskNameOld) { taskNameNew = tf.getText(); button.setText(taskNameNew);//运行时Eclipse报错 frame.repaint(); } }}然后上面的有一句button2.addActionListener(new Listener2(button1, tf,frame1));改成这样另外还想跟你说,判断字符是不是一样是用equals方法,不是用==或者!=,这样是判断地址,而不是内容 怎么替换字符串 java.util.ResourceBundle 除了能解析.properties 还能解析什么文件? 搜狗测试题,decode怎么写呢 如何在Listener中使用另外一个类的变量?(绝对菜!) 用java实现,把一个文件中的数字读出来,再倒序写进文件,各位前辈帮忙啊,一定给分的,请帮忙啊 打包 关于执行顺序的问题???????? 继续讨论文件目录处理的,高手指点!! 怎么从控制台程序中输入一串字符?? androidsocket服务器方面的问题 snmp4j get 问题 异常处理问题的困惑
frame.getbutton().setText(jtext.getText()),然后刷新下fram1
button2.addActionListener(new Listener2(button1, tf.getText(),frame1));
有啊,在 Frame2第5行:tf.getText(),调用的时候赋给参数了
button2.addActionListener(new Listener2(button1, tf.getText(),frame1));
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;public class Frame1 {
static JButton button1;
public static void main(String []args){
button1 = new JButton("button1");
JFrame frame1 = new JFrame("frame1");
button1.addActionListener(new Listener1(button1, frame1));
frame1.add(button1);
frame1.setLayout(new FlowLayout());
frame1.setBounds(400, 100, 400, 300);
frame1.setVisible(true);
}
static JButton getbun(){
return button1;
}
}class Frame2{
JButton button2;
static JTextField tf;
public Frame2(JButton button1, JFrame frame1){
tf = new JTextField(20);
JButton button2 = new JButton("修改");
button2.addActionListener(new Listener2(button1, tf.getText(),frame1));
JFrame frame2 = new JFrame("frame2");
frame2.setLayout(new FlowLayout());
frame2.setBounds(450, 150, 300, 200);
frame2.add(tf);
frame2.add(button2);
frame2.setVisible(true); }
static JTextField gettext(){
return tf;
}
}class Listener1 implements ActionListener{
JButton button;
JFrame frame;
public Listener1(JButton button, JFrame frame){
this.frame = frame;
this.button = button;
}
public void actionPerformed(ActionEvent e){
Frame2 frame2 = new Frame2(button,frame);
}
}
class Listener2 implements ActionListener{
String taskNameOld, taskNameNew;
JButton button;
JFrame frame;
public Listener2(JButton button, String stringNew, JFrame frame){
this.frame = frame; this.taskNameOld = button.getText();
this.taskNameNew = taskNameNew;
}
public void actionPerformed(ActionEvent e){
if(taskNameNew != taskNameOld)
{
Frame1.getbun().setText(Frame2.gettext().getText().toString());//运行时Eclipse报错
frame.repaint();
}
}
}
this.button = button;
好了,原来要return一下。
太感谢你了
我把button1,从Frame1传到Frame2, 等于已经初始化了啊我做一个小东西,这只是一个调试用的demo
你少了this.button=button;所以button是空,所以是空指针异常
然后
你获取tf内容应该是监听方法里面获取,如在actionPerformed里面
像你button2.addActionListener(new Listener2(button1, tf.getText(),frame1));这样的话
在初始化的时候就已经获取了,这时tf里面是没有内容的,应该写在监听方法里面,这样才是点击按钮时获取
改了如下
class Listener2 implements ActionListener{
String taskNameOld, taskNameNew;
JButton button;
JFrame frame;
JTextField tf;
public Listener2(JButton button,JTextField tf, JFrame frame){
this.frame = frame;
this.button = button;
this.taskNameOld = button.getText();
this.tf = tf;
}
public void actionPerformed(ActionEvent e){
if(taskNameNew != taskNameOld)
{ taskNameNew = tf.getText();
button.setText(taskNameNew);//运行时Eclipse报错
frame.repaint();
}
}
}
然后上面的有一句button2.addActionListener(new Listener2(button1, tf,frame1));改成这样
另外还想跟你说,判断字符是不是一样是用equals方法,不是用==或者!=,这样是判断地址,而不是内容