簡體   English   中英

如何獲得此代碼以使GUI倒數計時器正常工作?

[英]How can I get this code for a GUI Countdown Timer to work properly?

我試圖根據我在本網站上找到的一些其他代碼,為GUI倒數計時器(用於個人項目)自定義編寫程序。

這個想法是,用戶將能夠從組合框中選擇以分鍾為單位的總時間(可變的"remaining" ),然后按"Start" ,然后"Start"倒計時。

但是,在我當前的代碼中(如下所示),從組合框中選擇總時間並按"Start" ,標簽將顯示正確的開始時間,但倒計時不會開始。 如果我在代碼頂部(第23行)手動將"remaining"設置為總時間,例如 61000並刪除行"remaining = convertTime();" 在我的actionPerformed接口下,倒數計時器工作正常。

我不確定出了什么問題。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.NumberFormat;

import javax.swing.*;

public class GUITimer extends JFrame implements ItemListener {

JLabel jltime;
JLabel jl;
JComboBox jcb;
JButton jbt;
JButton jbt2;
NumberFormat format;

public Timer timer;
public long initial;
public long ttime2;
public String ttime;
public long remaining;

GUITimer() {

    jl=new JLabel("TOTAL TIME (minutes)");
    jl.setHorizontalAlignment((int) CENTER_ALIGNMENT);

    jltime=new JLabel("");
    jltime.setHorizontalAlignment((int) CENTER_ALIGNMENT);
    jltime.setForeground(Color.WHITE);
    jltime.setBackground(Color.BLACK);
    jltime.setOpaque(true);
    jltime.setFont(new Font("Arial", Font.BOLD, 450));

    jbt=new JButton("START");
    jbt2=new JButton("RESET");

    jcb=new JComboBox();
    jcb.addItem("15");
    jcb.addItem("14");
    jcb.addItem("13");
    jcb.addItem("12");
    jcb.addItem("11");
    jcb.addItem("10");
    jcb.addItem("9");
    jcb.addItem("8");
    jcb.addItem("7");
    jcb.addItem("6");
    jcb.addItem("5");
    jcb.addItem("4");
    jcb.addItem("3");
    jcb.addItem("2");
    jcb.addItem("1");

    JPanel jp1=new JPanel();
    jp1.setLayout(new GridLayout(1,4,10,0));
    jp1.add(jl);
    jp1.add(jcb);
    jp1.add(jbt);
    jp1.add(jbt2);

    getContentPane().add(jp1,BorderLayout.NORTH);

    JPanel jp2=new JPanel();
    jp2.setLayout(new GridLayout(1,2,10,10));
    jp2.add(jltime);

    getContentPane().add(jp2,BorderLayout.CENTER);

    event e = new event();
    jbt.addActionListener(e);
    jbt2.addActionListener(e);

    jcb.addItemListener(this);
}

    public static void main(String[] args) {
    GUITimer frame=new GUITimer();
    frame.setSize(500,500);
    frame.setTitle("LARC Moot Countdown Timer");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

//this method will run when user presses the start button
void updateDisplay() {

    Timeclass tc = new Timeclass();
    timer = new Timer(1000, tc);
    initial = System.currentTimeMillis();
    timer.start();
}

//code for what happens when user presses the start or reset button
public class event implements ActionListener {

    public void actionPerformed(ActionEvent e) {
    String bname=e.getActionCommand();
    if(bname.equals("START"))
    {
        updateDisplay();
    }
    else
    {
        jltime.setText("");
        timer.stop();
        remaining = convertTime();
    }
    }
}

//code that is invoked by swing timer for every second passed
public class Timeclass implements ActionListener {

    public void actionPerformed(ActionEvent e) {

        remaining = convertTime();
        long current = System.currentTimeMillis();
        long elapsed = current - initial; 
        remaining -= elapsed;
        initial = current; 

        format = NumberFormat.getNumberInstance();
        format.setMinimumIntegerDigits(2); 

        if (remaining < 0) remaining = (long)0;
        int minutes = (int)(remaining/60000);
        int seconds = (int)((remaining%60000)/1000);
        jltime.setText(format.format(minutes) + ":" + format.format(seconds));

        if (remaining == 0)
        {
        jltime.setText("Stop");
        timer.stop();
        }
}
}

    //get the number of minutes chosen by the user and activate convertTime method
    public void itemStateChanged(ItemEvent ie) {

        ttime = (String)jcb.getSelectedItem();
        convertTime();
    }

    //first need to convert no. of minutes from string to long. 
    //then need to convert that to milliseconds.        
    public long convertTime() {

        ttime2 = Long.parseLong(ttime);
        long converted = (ttime2*60000)+1000;
        return converted;
    }
}

我不得不重新排列您的GUI,以便對其進行故障排除。

LARC模擬倒數計時器

我所做的主要更改是將時鍾字體降低到96點,並在停止倒計時的Timeclass actionPerformed方法中注釋掉了該行。

我對您的代碼進行了許多小的更改。

package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.NumberFormat;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class GUITimer extends JFrame implements ItemListener {

    private static final long serialVersionUID = 5924880907001755076L;

    JLabel jltime;
    JLabel jl;
    JComboBox<Integer> jcb;
    JButton jbt;
    JButton jbt2;
    NumberFormat format;

    public Timer timer;
    public long initial;
    public long ttime2;
    public String ttime;
    public long remaining;

    public GUITimer() {

        JPanel timePanel = new JPanel();
        timePanel.setForeground(Color.BLACK);

        jltime = new JLabel(" ");
        jltime.setHorizontalAlignment((int) CENTER_ALIGNMENT);
        jltime.setForeground(Color.WHITE);
        jltime.setBackground(Color.BLACK);
        jltime.setOpaque(true);
        jltime.setFont(new Font("Arial", Font.BOLD, 96));

        timePanel.add(jltime);

        JPanel jp1 = new JPanel();
        jp1.setLayout(new FlowLayout());

        jl = new JLabel("TOTAL TIME (minutes):");
        jp1.add(jl);

        jcb = new JComboBox<Integer>();
        for (int i = 15; i > 0; i--) {
            jcb.addItem(Integer.valueOf(i));
        }
        jcb.setSelectedIndex(0);
        ttime = "15";
        jp1.add(jcb);

        jbt = new JButton("START");
        jp1.add(jbt);

        jbt2 = new JButton("RESET");
        jp1.add(jbt2);

        getContentPane().add(jp1, BorderLayout.NORTH);
        getContentPane().add(timePanel, BorderLayout.CENTER);

        Event e = new Event();
        jbt.addActionListener(e);
        jbt2.addActionListener(e);

        jcb.addItemListener(this);

        setBackground(Color.BLACK);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("LARC Moot Countdown Timer");
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GUITimer();
            }

        });
    }

    // this method will run when user presses the start button
    void updateDisplay() {

        Timeclass tc = new Timeclass();
        timer = new Timer(1000, tc);
        initial = System.currentTimeMillis();
        timer.start();
    }

    // code for what happens when user presses the start or reset button
    public class Event implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String bname = e.getActionCommand();
            if (bname.equals("START")) {
                updateDisplay();
            } else {
                jltime.setText(" ");
                timer.stop();
                remaining = convertTime();
            }
        }
    }

    // code that is invoked by swing timer for every second passed
    public class Timeclass implements ActionListener {

        public void actionPerformed(ActionEvent e) {

            remaining = convertTime();
            long current = System.currentTimeMillis();
            long elapsed = current - initial;
            remaining -= elapsed;
            // initial = current;

            format = NumberFormat.getNumberInstance();
            format.setMinimumIntegerDigits(2);

            if (remaining < 0)
                remaining = (long) 0;
            int minutes = (int) (remaining / 60000);
            int seconds = (int) ((remaining % 60000) / 1000);
            jltime.setText(format.format(minutes) + ":"
                    + format.format(seconds));

            if (remaining == 0) {
                jltime.setText("Stop");
                timer.stop();
            }
        }
    }

    // get the number of minutes chosen by the user and activate convertTime
    // method
    public void itemStateChanged(ItemEvent ie) {

        ttime = (String) jcb.getSelectedItem().toString();
        convertTime();
    }

    // first need to convert no. of minutes from string to long.
    // then need to convert that to milliseconds.
    public long convertTime() {

        ttime2 = Long.parseLong(ttime);
        long converted = (ttime2 * 60000) + 1000;
        return converted;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM