漫步青青流年 2015-05-04 08:26 采纳率: 0%
浏览 3456

Java程序没有错,但是Console有问题,没运行结果(秒表)

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
//import java.util.TimerTask;
import javax.swing.Timer;

import javax.swing.*;

public class StopwatchFrame extends JFrame implements ActionListener
{
private static final long serialVersionUID = -1947098583246834192L;
JButton startButton; //启动按钮
JButton stopButton; //暂停按钮
JButton countButton; //完成按钮
JButton resetButton; //复位按钮
JLabel timeLabel;
JTextArea resultArea;
Timer myTimer;
int minute; //分
int second; //秒
int donknow; //秒后的单位
int numberFinish; //记录的次数

     StopwatchFrame(String s)
    {

        super(s);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Container contentPane = getContentPane();
        contentPane.setLayout(new CardLayout(4,4));

        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel resultPanel = new JPanel(new CardLayout(4,4));
        JPanel timePanel = new JPanel(new CardLayout(45,4));
        //JPanel timeFlowPanel = new JPanel(new FlowLayout());
        JPanel buttonCardPanel = new JPanel(new CardLayout(4,4));
        JPanel buttonPanel = new JPanel(new GridLayout(4,1,0,3));
        JPanel westPanel = new JPanel(new BorderLayout());

        startButton = new JButton("开始/继续");
        stopButton = new JButton("暂停");
        countButton = new JButton("计数");
        resetButton = new JButton("复位");
        timeLabel = new JLabel("00:00:00");

        timeLabel.setFont(new Font("微软雅黑",Font.PLAIN,35));
        timeLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        timeLabel.setAlignmentY(JLabel.CENTER_ALIGNMENT);
        resultArea = new JTextArea();
        resultArea.setEditable(true);
        resultArea.setLineWrap(false);
        resultArea.setColumns(10);



        buttonPanel.add(startButton);
        buttonPanel.add(stopButton);
        buttonPanel.add(resetButton);
        buttonPanel.add(countButton);
        buttonCardPanel.add(buttonPanel);
        timePanel.add(timeLabel);
        resultPanel.add(new JScrollPane(resultArea));
        westPanel.add(buttonCardPanel,BorderLayout.CENTER);
        mainPanel.add(timePanel,BorderLayout.NORTH);

        mainPanel.add(westPanel,BorderLayout.CENTER);
        mainPanel.add(resultPanel,BorderLayout.WEST);
        contentPane.add(mainPanel);

        myTimer = new Timer(10,this);
        startButton.addActionListener(this);
        stopButton.addActionListener(this);
        countButton.addActionListener(this);
        resetButton.addActionListener(this);

        this.minute = 0;
        this.second = 0;
        this.donknow = 0;
        this.numberFinish=0;

        setSize(250,300);
        setResizable(false);   //禁止改变窗口大小
        setLocationRelativeTo(null); // 在屏幕中心弹出窗口
        setVisible(true);   

    }



    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==startButton){

            myTimer.start();
        }
        else if(e.getSource()==stopButton){
            myTimer.stop();
        }
        else if(e.getSource()==countButton){

            this.numberFinish++;
            DecimalFormat myFormat = new DecimalFormat("00");
            this.resultArea.append(" 第"+this.numberFinish+":   "+myFormat.format(this.minute)+":"+
                    myFormat.format(this.second)+":"+myFormat.format(this.donknow)+"\r\n");
        }
        else if(e.getSource()==resetButton){

            myTimer.stop();
            this.minute = 0;
            this.second = 0;
            this.donknow = 0;
            this.numberFinish=0;
            timeLabel.setText("00:00:00");

            //myTimer.start();
            //resultArea.setText("");
        }
        else if(e.getSource()==myTimer)
        {

            this.donknow++;
            this.second += this.donknow/100;
            this.minute += this.second/60;

            this.donknow = this.donknow%100;
            this.second = this.second%60;
            this.minute =this.minute%60;

            DecimalFormat myFormat = new DecimalFormat("00");
            this.timeLabel.setText(myFormat.format(this.minute)+":"+
                    myFormat.format(this.second)+":"+myFormat.format(this.donknow));

        }

    }
    public static void main(String args[])
    {
        @SuppressWarnings("unused")
        StopwatchFrame myFrame = new StopwatchFrame("秒表");
    }

}

  • 写回答

3条回答

  • 漫步青青流年 2015-05-04 08:28
    关注

    请哪位大师指点一下,感激不尽

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作