我试图根据我在本网站上找到的其他一些代码,为GUI倒数计时器(个人项目)自定义编写程序。
我们的想法是,用户可以从组合框中选择分钟的总时间(变量"remaining"
),按"Start"
,倒计时就会开始。
但是,在我当前的代码(下方)中,从组合框中选择总时间并按"Start"
后,标签将显示正确的开始时间,但倒计时将不会开始。如果我手动将代码顶部的"remaining"
(第23行)设置为总时间,例如。 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;
}
}
答案 0 :(得分:0)
我不得不重新安排你的GUI,以便对其进行故障排除。
我做的主要更改是将时钟字体降低到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;
}
}