【发布时间】:2018-09-18 07:40:36
【问题描述】:
我正在构建一个闹钟,但我无法设置我的闹钟,以便可以提前设置该功能只有在我将闹钟设置为当前时间时才有效。我尝试使用 Thread.sleep 并且有相同的结果,所以任何帮助都将不胜感激,而且我的理解主要是基本的,所以如果我遗漏了一些简单的东西,我深表歉意。
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.Calendar;
import java.util.Scanner;
import net.aksingh.owmjapis.core.OWM;
import net.aksingh.owmjapis.api.APIException;
import net.aksingh.owmjapis.model.CurrentWeather;
import org.jfugue.player.Player;
public class Main {
private static int hour;
private static int minute;
private static int minutetext;
private static int hourtext;
public static void main(String[] args) throws APIException {
JFrame f = new JFrame();
f.setSize(320,480);
OWM owm = new OWM("");
CurrentWeather cwd = owm.currentWeatherByCityName("");
int tempcorrect = 273;
Double temp =cwd.getMainData().getTemp();
double tempreal = (int) (temp-tempcorrect);
JPanel mp = new JPanel();
JPanel tp = new JPanel();
JPanel bp = new JPanel();
JButton b1 = new JButton("Set Alarm");
JButton b2 = new JButton("Stop");
JLabel l1 = new JLabel("Temperature in " + cwd.getCityName() + " is " + tempreal + " C" );
JLabel rl = new JLabel("There is a " + cwd.getMainData().getHumidity() + "% chance of rain" );
bp.add(b1);
bp.add(b2);
mp.add(l1);
mp.add(rl);
bp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
mp.add(tp, BorderLayout.NORTH);
mp.add(bp, BorderLayout.SOUTH);
DigitalClock myClock = new DigitalClock();
tp.add(myClock);
f.add(mp);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(e -> {
JDialog jd = new JDialog();
JPanel j1 = new JPanel();
jd.setSize(320,240);
j1.setSize(180,200);
j1.setAlignmentY(100);
FlowLayout f1 = new FlowLayout();
jd.setLayout(f1);
jd.add(j1);
JLabel l2 = new JLabel("Alarm Time (24hrs)");
JTextField t1 = new JTextField();
t1.setToolTipText("Hour");
JTextField t2 = new JTextField();
t2.setToolTipText("Minutes");
t1.setColumns(2);
t2.setColumns(2);
JButton b21 = new JButton("Set Alarm");
t1.setLocation(80,300);
j1.add(l2);
j1.add(t1);
j1.add(t2);
j1.add(b21);
b21.addActionListener(e1 -> {
Player player = new Player();
hourtext= Integer.parseInt(t1.getText());
minutetext=Integer.parseInt(t2.getText());
//int v =1;
if ((hourtext == hour) && (minutetext == minute)) {
System.out.println(hour + " : " + minute);
player.play("A A A A A A ");
} else {
System.out.println("alarm not active");
}
});
jd.setVisible(true);
});
}
public static class DigitalClock extends JPanel {
public static String stringTime;
public void setStringTime(String xyz) {
this.stringTime = xyz;
}
public int findMinimumBetweenTwoNumbers(int a, int b) {
return (a <= b) ? a : b;
}
DigitalClock() {
Timer t1 = new Timer(1000, e -> repaint());
t1.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Calendar now = Calendar.getInstance();
hour = now.get(Calendar.HOUR_OF_DAY);
minute = now.get(Calendar.MINUTE);
int second = now.get(Calendar.SECOND);
String correctionHour = (hour < 10) ? "0" : "";
String correctionMinute = (minute < 10) ? "0" : "";
String correctionSecond = (second < 10) ? "0" : "";
setStringTime(correctionHour + hour + ":" + correctionMinute+ minute + ":" + correctionSecond + second);
g.setColor(Color.BLACK);
int length = findMinimumBetweenTwoNumbers(this.getWidth(),this.getHeight());
Font myFont = new Font("SansSerif", Font.PLAIN, length / 5);
g.setFont(myFont);
g.drawString(stringTime, length /6, length/2);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
【问题讨论】:
-
你试过
java.util.Timer吗? -
我会考虑使用
java.timeAPI 而不是Calendar -
findMinimumBetweenTwoNumbers...Math.min -
1) 为了尽快获得更好的帮助,请发帖 minimal reproducible example 或 Short, Self Contained, Correct Example。删除所有第 3 方代码并删除与当前问题无关的所有其他内容(设置字体等)。此外,删除对
static的所有提及。 2) 曾经只需要源代码中的一个空白行。{之后或}之前的空行通常也是多余的。 3) 请参阅Detection/fix for the hanging close bracket of a code block 了解我无法再费心修复的问题。