关于GUI事件的疑问。

syj247 2014-08-14 09:33:47
有一个按钮,点击按钮后,按钮的内容变成001,002,003,004
我的代码如下
button1.addActionListener(new addActionListener(){
public void actionPerformed(ActionEvent e){
do("001");do("002");do("003")
}
});


public void do(String str){
Thread thread = new Thread();
try{
button1.setText(str);
thread.sleep(500);
}catch(Interrupt..... e1){
e1.printStackTrace;
}
}


公司上网机上敲的。效果不好。
现在运行结果是 按钮最后一个值,中间设置都未生效,求大侠指点。
...全文
334 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
syj247 2014-08-14
  • 打赏
  • 举报
回复
引用 8 楼 fangmingshijie 的回复:
别用Thread类的sleep,用Timer类,在构造函数里定义timer= new Timer(500, this); 在action里用e.getSource()==button1的时候,timer.start(),然后else if (e.getSource() == timer) 的时候,再去设置内容就可以了。(把内容放到一个数组里,然后定义一个全局变量,让它递增,再到数组获取对应下标的内容即可。)
还是未达到我想要的效果 我想要的效果是 在点击某个按钮后,执行多个方法,每个方法都返回一个字符串,把这些方法的返回值显示依次显示到某个文本框上。
  • 打赏
  • 举报
回复
别用Thread类的sleep,用Timer类,在构造函数里定义timer= new Timer(500, this); 在action里用e.getSource()==button1的时候,timer.start(),然后else if (e.getSource() == timer) 的时候,再去设置内容就可以了。(把内容放到一个数组里,然后定义一个全局变量,让它递增,再到数组获取对应下标的内容即可。)
  • 打赏
  • 举报
回复
引用 6 楼 syj247 的回复:
[quote=引用 4 楼 u012367513 的回复:] do("003") 貌似这里少了个分号
引用 4 楼 u012367513 的回复:
do("003") 貌似这里少了个分号
伪码的 哥。[/quote] 这样子啊,惭愧
syj247 2014-08-14
  • 打赏
  • 举报
回复
引用 4 楼 u012367513 的回复:
do("003") 貌似这里少了个分号
引用 4 楼 u012367513 的回复:
do("003") 貌似这里少了个分号
伪码的 哥。
syj247 2014-08-14
  • 打赏
  • 举报
回复
引用 3 楼 shixitong 的回复:
package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Test1 {

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JButton button = new JButton("测试");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {

					((JButton) e.getSource()).setText("001,002,003,004");
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		frame.add(button);
		frame.setSize(400, 300);
		frame.setVisible(true);

	}

}
w我的意思是,按钮默认为“确定“,点了后,发生多次变化(如:”文件校验“,”执行升级“等等)。而不是一次把多有都显示出来。
  • 打赏
  • 举报
回复
do("003") 貌似这里少了个分号
shixitong 2014-08-14
  • 打赏
  • 举报
回复
package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Test1 {

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JButton button = new JButton("测试");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {

					((JButton) e.getSource()).setText("001,002,003,004");
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		frame.add(button);
		frame.setSize(400, 300);
		frame.setVisible(true);

	}

}
syj247 2014-08-14
  • 打赏
  • 举报
回复
引用 1 楼 shixitong 的回复:
你要把按钮变为001,002,003,004
button1.addActionListener(new addActionListener(){
    public void actionPerformed(ActionEvent e){
     do("001,002,003,004");
     }
});
没效果。
shixitong 2014-08-14
  • 打赏
  • 举报
回复
你要把按钮变为001,002,003,004
button1.addActionListener(new addActionListener(){
    public void actionPerformed(ActionEvent e){
     do("001,002,003,004");
     }
});
nj_dobetter 2014-08-14
  • 打赏
  • 举报
回复
你起了三个线程 并发同时去改Button上的字符串,原则上说,最终Button上是哪个字符串是 不确定的。 do("001");do("002");do("003"); //起了三个线程并发改button字符串
  • 打赏
  • 举报
回复
我也只能说,呵呵。简单的逻辑,还要别人给你处理。
syj247 2014-08-14
  • 打赏
  • 举报
回复
引用 19 楼 syj247 的回复:
[quote=引用 18 楼 fangmingshijie 的回复:]
String [] strs=new String[]{method1(),method2(),method3()};
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b) {
			secondTime.start();
		} else if (e.getSource() == secondTime) {
			int i = n++;
			if (i < 3) {
				jt.setText(strs[i]);
			}
		}

	}
这样可行?早前面就告诉你了。实现方式很多,我这只是一种而已。
我只能说 呵呵 我要的是方法实时反馈的结果,而不是在一开始就把待执行的代码都执行完。 [/quote] 并且是顺序执行。
syj247 2014-08-14
  • 打赏
  • 举报
回复
引用 18 楼 fangmingshijie 的回复:
String [] strs=new String[]{method1(),method2(),method3()};
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b) {
			secondTime.start();
		} else if (e.getSource() == secondTime) {
			int i = n++;
			if (i < 3) {
				jt.setText(strs[i]);
			}
		}

	}
这样可行?早前面就告诉你了。实现方式很多,我这只是一种而已。
我只能说 呵呵 我要的是方法实时反馈的结果,而不是在一开始就把待执行的代码都执行完。
  • 打赏
  • 举报
回复
String [] strs=new String[]{method1(),method2(),method3()};
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b) {
			secondTime.start();
		} else if (e.getSource() == secondTime) {
			int i = n++;
			if (i < 3) {
				jt.setText(strs[i]);
			}
		}

	}
这样可行?早前面就告诉你了。实现方式很多,我这只是一种而已。
syj247 2014-08-14
  • 打赏
  • 举报
回复
引用 15 楼 syj247 的回复:
[quote=引用 14 楼 fangmingshijie 的回复:]
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;
import javax.swing.Timer;

public class Setting extends JFrame implements ActionListener {
	private static final long serialVersionUID = -1490406134768395542L;

	JButton b = new JButton("设置");

	JTextField jt = new JTextField(null,10);
	private Timer secondTime;
	int n = 1;
	public Setting() {
		setLayout(new FlowLayout(FlowLayout.LEFT));
		add(jt);
		add(b);
		pack();
		setVisible(true);
		secondTime = new Timer(500, this);
		b.addActionListener(this);
	}

	public static void main(String[] args) {
		new Setting();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b) {
			secondTime.start();
		} else if (e.getSource() == secondTime) {
			int i = n++;
			if (i < 11) {
				jt.setText(""+i);
			}
		}
		
	}

}
非常感谢

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b) {
            secondTime.start();
        } else if (e.getSource() == secondTime) {
                jt.setText(method1());
                 jt.setText(method2());
                 jt.setText(method3());
        }
         
    }

private string method1(){
 //todo something
System.out.println("方法1被执行");
return "方法一被执行";
} 
private string method2(){
 //todo something
System.out.println("方法2被执行");
return "方法二被执行";
} 
private string method3(){
 //todo something
System.out.println("方法3被执行");
return "方法三被执行";
} 

方法一直被重复调用。求破[/quote] 我在每个返回前加了 secondTime.stop() 3个方法各被调用一次 但是前2个方法的值,未显示到文本框中。求解。
  • 打赏
  • 举报
回复
例子已经给你了,照着做还不会。
syj247 2014-08-14
  • 打赏
  • 举报
回复
引用 14 楼 fangmingshijie 的回复:
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;
import javax.swing.Timer;

public class Setting extends JFrame implements ActionListener {
	private static final long serialVersionUID = -1490406134768395542L;

	JButton b = new JButton("设置");

	JTextField jt = new JTextField(null,10);
	private Timer secondTime;
	int n = 1;
	public Setting() {
		setLayout(new FlowLayout(FlowLayout.LEFT));
		add(jt);
		add(b);
		pack();
		setVisible(true);
		secondTime = new Timer(500, this);
		b.addActionListener(this);
	}

	public static void main(String[] args) {
		new Setting();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b) {
			secondTime.start();
		} else if (e.getSource() == secondTime) {
			int i = n++;
			if (i < 11) {
				jt.setText(""+i);
			}
		}
		
	}

}
非常感谢

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b) {
            secondTime.start();
        } else if (e.getSource() == secondTime) {
                jt.setText(method1());
                 jt.setText(method2());
                 jt.setText(method3());
        }
         
    }

private string method1(){
 //todo something
System.out.println("方法1被执行");
return "方法一被执行";
} 
private string method2(){
 //todo something
System.out.println("方法2被执行");
return "方法二被执行";
} 
private string method3(){
 //todo something
System.out.println("方法3被执行");
return "方法三被执行";
} 

方法一直被重复调用。求破
  • 打赏
  • 举报
回复
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;
import javax.swing.Timer;

public class Setting extends JFrame implements ActionListener {
	private static final long serialVersionUID = -1490406134768395542L;

	JButton b = new JButton("设置");

	JTextField jt = new JTextField(null,10);
	private Timer secondTime;
	int n = 1;
	public Setting() {
		setLayout(new FlowLayout(FlowLayout.LEFT));
		add(jt);
		add(b);
		pack();
		setVisible(true);
		secondTime = new Timer(500, this);
		b.addActionListener(this);
	}

	public static void main(String[] args) {
		new Setting();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b) {
			secondTime.start();
		} else if (e.getSource() == secondTime) {
			int i = n++;
			if (i < 11) {
				jt.setText(""+i);
			}
		}
		
	}

}
syj247 2014-08-14
  • 打赏
  • 举报
回复
引用 11 楼 fangmingshijie 的回复:
具体执行那些方法是你自己的逻辑判断的,给你的解决方法是你依据你的描述。自己都没有表述清楚,别人更不可能给你彻底的解决方案。
主要是对事件机制不了解。我想问的是,有一个按钮和一个文本框。 点一次按钮后,文本框的内容从1显示到10(依次)。求实现。
syj247 2014-08-14
  • 打赏
  • 举报
回复
引用 10 楼 shixitong 的回复:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class Test1 {

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setLayout(new BorderLayout());
		JButton button = new JButton("确定");
		final javax.swing.JTextField field = new JTextField();
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					String str1=do1();
					String str2=do2();
					String str3=do3();
					field.setText(str1+","+str2+","+str3);
					//((JButton) e.getSource()).setText("001,002,003,004");
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		frame.add(button,BorderLayout.NORTH);
		frame.add(field,BorderLayout.CENTER);
		frame.setSize(400, 300);
		frame.setVisible(true);
	}
	
	public static String do1(){
		return "aaa";
	}
	public static String do2(){
		return "bbb";
	}
	public static String do3(){
		return "ccc";
	}

}
field文本框的内容期望值是aaa--->bbb--->ccc (--->变成)

try {
                    String str1=do1();
                    String str2=do2();
                    String str3=do3();
                    field.setText(str1+","+str2+","+str3);
                    //((JButton) e.getSource()).setText("001,002,003,004");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
我把改成

try {
                    field.setText(do1());
                     field.setText(do2());
                     field.setText(do3());
                    //field.setText(str1+","+str2+","+str3);
                    //((JButton) e.getSource()).setText("001,002,003,004");
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
效果后,发现文本框的内容从来没有被设置成aaa ,bbb 过 你可能会说程序执行太快了。
加载更多回复(2)

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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