50,530
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
button1.addActionListener(new addActionListener(){
public void actionPerformed(ActionEvent e){
do("001,002,003,004");
}
});
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]);
}
}
}
这样可行?早前面就告诉你了。实现方式很多,我这只是一种而已。
@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个方法的值,未显示到文本框中。求解。
@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);
}
}
}
}
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 过
你可能会说程序执行太快了。