漫步青青流年 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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)