【求助】Java GUI 事件监听器的参数传递

tianranhe 2013-03-07 04:37:35
问题:
有两个Frame:Frame1 和Frame2 。我想通过Frame2 修改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 {
public static void main(String []args){
JButton 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);
}
}

class Frame2{
public Frame2(JButton button1, JFrame frame1){
JTextField 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);

}
}

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 taskNameNew, JFrame frame){
this.frame = frame;

this.taskNameOld = button.getText();
this.taskNameNew = taskNameNew;
}
public void actionPerformed(ActionEvent e){
if(taskNameNew != taskNameOld)
{
button.setText(taskNameNew);//运行时Eclipse报错
frame.repaint();
}
}
}



运行时倒数第5行报错

我的思路错了吗?或者是我的代码有问题?

求大侠指点。
...全文
447 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianranhe 2013-03-07
  • 打赏
  • 举报
回复
引用 11 楼 jlu_lamp_lamp 的回复:
很简单 你少了this.button=button;所以button是空,所以是空指针异常 然后 你获取tf内容应该是监听方法里面获取,如在actionPerformed里面 像你button2.addActionListener(new Listener2(button1, tf.getText(),frame1));这样的话 在初始化的时候就已经获取了,这时……
这个改的最好,问题指的也有道理,受教了!!谢谢你
jlu_lamp_lamp 2013-03-07
  • 打赏
  • 举报
回复
很简单 你少了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方法,不是用==或者!=,这样是判断地址,而不是内容
tianranhe 2013-03-07
  • 打赏
  • 举报
回复
引用 6 楼 miaowhehe 的回复:
button空指针了。因为在Listener2的构造方法中少写了一句 Java code?1this.button = button;
可能我忘写了。不知道为什么,改完按钮变得超级小
tianranhe 2013-03-07
  • 打赏
  • 举报
回复
引用 7 楼 yyw6637 的回复:
还有就是你整那么多类干什么,简简单单三四十行代码就可以实现的,你的思路麻烦不?
我把button1,从Frame1传到Frame2, 等于已经初始化了啊 我做一个小东西,这只是一个调试用的demo
tianranhe 2013-03-07
  • 打赏
  • 举报
回复
引用 4 楼 yyw6637 的回复:
我做了如下修改: Java code?12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import java.awt.Fl……
好了,原来要return一下。 太感谢你了
yyw6637 2013-03-07
  • 打赏
  • 举报
回复
还有就是你整那么多类干什么,简简单单三四十行代码就可以实现的,你的思路麻烦不?
miaowhehe 2013-03-07
  • 打赏
  • 举报
回复
button空指针了。因为在Listener2的构造方法中少写了一句
this.button = button;
yyw6637 2013-03-07
  • 打赏
  • 举报
回复
你之前的button.setText(taskNameNew);这行中的button是什么?没有被初始化的按钮
yyw6637 2013-03-07
  • 打赏
  • 举报
回复
我做了如下修改:



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();
		}
	}
}

tianranhe 2013-03-07
  • 打赏
  • 举报
回复
引用 1 楼 yyw6637 的回复:
this.taskNameNew = taskNameNew; 你这是干什么,根本就没有得到文本框里的值啊;在修改按钮事件里: frame.getbutton().setText(jtext.getText()),然后刷新下fram1
有啊,在 Frame2第5行:tf.getText(),调用的时候赋给参数了 button2.addActionListener(new Listener2(button1, tf.getText(),frame1));
tianranhe 2013-03-07
  • 打赏
  • 举报
回复
有啊,在Frame2第5行:tf.getText() button2.addActionListener(new Listener2(button1, tf.getText(),frame1));
yyw6637 2013-03-07
  • 打赏
  • 举报
回复
this.taskNameNew = taskNameNew; 你这是干什么,根本就没有得到文本框里的值啊;在修改按钮事件里: frame.getbutton().setText(jtext.getText()),然后刷新下fram1

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧