漫步青青流年 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
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能