新手想问:JAVA怎么实现在规定时间记录点击按钮的次数
我估计问题应该是main方法里的循环没法实现,不知道怎么解决了。import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyJbuttonTest {
public static void main(String[] args) {
MyFrame mf=new MyFrame();
while(true){
if(!(mf.s.equals("第0次按按钮"))){
for(int i=0;i<=9;i++){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
mf.time++;
mf.jl1.setText(String.valueOf(mf.time));
}
return;
}else{
continue;
}
}
}
}
class MyFrame extends JFrame{
JPanel jp=new JPanel();
static JLabel jl=new JLabel("第0次按按钮");
int count=0;
ImageIcon ic=new ImageIcon(
"C:\\Users\\Administrator\\Desktop\\icon.jpg");
JButton jb=new JButton(ic);
static int time=0;
JLabel jl1=new JLabel("0");
static String s;
public MyFrame(){
jl.setForeground(Color.blue);
jp.add(jb);
jp.add(jl);
jp.add(jl1);
jl1.setLocation(10, 10);
this.add(jp);
this.setTitle("MyJButton使用");
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension d=tk.getScreenSize();
this.setSize(d);
this.setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windwClosing(WindowEvent e){
System.exit(0);
}
});
jb.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
jl.setText("第"+ (++count)+"次点击");
s=jl.getText();
}
});
}
}