标题:新手想问:JAVA怎么实现在规定时间记录点击按钮的次数
只看楼主
wawuye
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-1-9
 问题点数:0 回复次数:0 
新手想问: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();
            }
            
        });
    }
}
搜索更多相关主题的帖子: public JAVA import 记录 
2017-01-09 18:42



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-473452-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019486 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved