67,513
社区成员
发帖
与我相关
我的任务
分享
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Window {
JFrame aq;
JButton a1, a2;
public static void main(String args[]) {
Window ll = new Window();
}
public Window() {
JFrame aq = new JFrame("ButtonTest");
aq.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a1 = new JButton("0");
a2 = new JButton("1");
Container af = aq.getContentPane();
af.setLayout(new GridLayout(2, 1));
af.add(a1);
af.add(a2);
ActionListener ajj = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String rn = e.getActionCommand();
System.out.println(rn);
if (rn == "0") {
System.out.println("0 clicked!");
Runnable r = new Runnable() {
@Override
public void run() {
int i = 0;
while (i < 10) {
i++;
System.out.println("0 : " + new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Thread t = new Thread(r);
t.start();
}
if (rn == "1") {
System.out.println("1 clicked!");
Runnable r = new Runnable() {
@Override
public void run() {
int i = 0;
while (i < 10) {
i++;
System.out.println("1 : " + new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread t = new Thread(r);
t.start();
}
}
};
a1.addActionListener(ajj);
a2.addActionListener(ajj);
aq.pack();
aq.setVisible(true);
}
};