我想用java做一个闹钟,程序如下:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;public class MyClock {
public static void main(String [] args){
JFrame jf = new JFrame("MyClock");
JButton button = new JButton("设置闹钟");
button.addActionListener(new MyThread(null));
JLabel clock = new JLabel("clock");
clock.setHorizontalAlignment(JLabel.LEFT); //设置JLabel显示格式
clock.setFont(new Font("",1,20));//字体大小
clock.setForeground(Color.BLUE);//字体颜色
jf.add(button,"East");
jf.add(clock,"Center");
jf.setSize(150,100);
jf.setLocation(500,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
jf.setVisible(true);
Thread t = new MyThread(clock);
t.start();
}
}class MyThread extends Thread implements ActionListener {
private JLabel clock;
String StringTime ;
String inputTime =null ;
public MyThread(JLabel clock){
this.clock = clock;
}
public void run(){
while(true){
clock.setText(this.getTime()); //调用
if(inputTime.equals(clock.getText())){
System.out.println("TimeOver");
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public void actionPerformed (ActionEvent e) {
inputTime = JOptionPane.showInputDialog("Input Time:");
}
public String getTime(){
Calendar c = new GregorianCalendar();
String time = c.get(Calendar.YEAR) +"-"+(c.get(Calendar.MONTH)+1)+
"-"+ c.get(Calendar.DATE)+" " ;
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
// String h1 = String.valueOf(h);
// String m1 = String.valueOf(m);
// String s1 = String.valueOf(s);
// String STime = h1.concat(m1);
// StringTime = STime.concat(s1);
time = h+"-"+m+"-"+s; //显示时间
return time;
}
}程序运行没错,我输入的时间格式也是跟显示的一样,可是结果就是不出来!求解!!!
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;public class MyClock {
public static void main(String [] args){
JFrame jf = new JFrame("MyClock");
JButton button = new JButton("设置闹钟");
button.addActionListener(new MyThread(null));
JLabel clock = new JLabel("clock");
clock.setHorizontalAlignment(JLabel.LEFT); //设置JLabel显示格式
clock.setFont(new Font("",1,20));//字体大小
clock.setForeground(Color.BLUE);//字体颜色
jf.add(button,"East");
jf.add(clock,"Center");
jf.setSize(150,100);
jf.setLocation(500,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
jf.setVisible(true);
Thread t = new MyThread(clock);
t.start();
}
}class MyThread extends Thread implements ActionListener {
private JLabel clock;
String StringTime ;
String inputTime =null ;
public MyThread(JLabel clock){
this.clock = clock;
}
public void run(){
while(true){
clock.setText(this.getTime()); //调用
if(inputTime.equals(clock.getText())){
System.out.println("TimeOver");
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public void actionPerformed (ActionEvent e) {
inputTime = JOptionPane.showInputDialog("Input Time:");
}
public String getTime(){
Calendar c = new GregorianCalendar();
String time = c.get(Calendar.YEAR) +"-"+(c.get(Calendar.MONTH)+1)+
"-"+ c.get(Calendar.DATE)+" " ;
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);
// String h1 = String.valueOf(h);
// String m1 = String.valueOf(m);
// String s1 = String.valueOf(s);
// String STime = h1.concat(m1);
// StringTime = STime.concat(s1);
time = h+"-"+m+"-"+s; //显示时间
return time;
}
}程序运行没错,我输入的时间格式也是跟显示的一样,可是结果就是不出来!求解!!!
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;public class MyClock { public static void main(String[] args) {
JFrame jf = new JFrame("MyClock");
JButton button = new JButton("设置闹钟");
MyThread mt=new MyThread();
button.addActionListener(mt);
JLabel clock = new JLabel("clock");
clock.setHorizontalAlignment(JLabel.LEFT); //设置JLabel显示格式
clock.setFont(new Font("", 1, 20));//字体大小
clock.setForeground(Color.BLUE);//字体颜色
jf.add(button, "East");
jf.add(clock, "Center");
jf.setSize(200, 100);
jf.setLocation(500, 300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
jf.setVisible(true);
clock.setText(mt.getTime());
MyThread.setClock(clock);
}
}class MyThread extends Thread implements ActionListener { private static JLabel clock;
static String StringTime;
static String inputTime = null; public static void setClock(JLabel clock) {
MyThread.clock = clock;
} public MyThread() {
} public void run() {
while (true) {
clock.setText(this.getTime()); //调用
if (inputTime.equals(clock.getText())) {
System.out.println("TimeOver");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} public void actionPerformed(ActionEvent e) {
inputTime = JOptionPane.showInputDialog("Input Time:");
Thread t = new MyThread();
t.start();
} public String getTime() {
Calendar c = new GregorianCalendar();
String time = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) +
"-" + c.get(Calendar.DATE) + " ";
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);// String h1 = String.valueOf(h);
// String m1 = String.valueOf(m);
// String s1 = String.valueOf(s);
// String STime = h1.concat(m1);
// StringTime = STime.concat(s1);
time = h + "-" + m + "-" + s; //显示时间 return time; }
}
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;public class MyClock {
public static void main(String [] args){
JFrame jf = new JFrame("MyClock");
JButton button = new JButton("设置闹钟");
//button.addActionListener(new MyThread(null));
JLabel clock = new JLabel("clock");
clock.setHorizontalAlignment(JLabel.LEFT); //设置JLabel显示格式
clock.setFont(new Font("",1,20));//字体大小
clock.setForeground(Color.BLUE);//字体颜色
jf.add(button,"East");
jf.add(clock,"Center");
jf.setSize(150,100);
jf.setLocation(500,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //
jf.setVisible(true);
Thread t = new MyThread(clock);
//---------------------------------(1)
button.addActionListener((MyThread)t);
t.start();
}
}class MyThread extends Thread implements ActionListener {
private JLabel clock;
String StringTime ;
//-------------------------------------(2)
String inputTime ="" ;
public MyThread(JLabel clock){
this.clock = clock;
}
public void run(){
while(true){
clock.setText(this.getTime()); //调用 if(inputTime.equals(clock.getText())){
System.out.println("TimeOver");
}
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
}
}public void actionPerformed (ActionEvent e) {
inputTime = JOptionPane.showInputDialog("Input Time:");
}public String getTime(){
Calendar c = new GregorianCalendar();
String time = c.get(Calendar.YEAR) +"-"+(c.get(Calendar.MONTH)+1)+
"-"+ c.get(Calendar.DATE)+" " ;
int h = c.get(Calendar.HOUR_OF_DAY);
int m = c.get(Calendar.MINUTE);
int s = c.get(Calendar.SECOND);// String h1 = String.valueOf(h);
// String m1 = String.valueOf(m);
// String s1 = String.valueOf(s);
// String STime = h1.concat(m1);
// StringTime = STime.concat(s1);
time = h+"-"+m+"-"+s; //显示时间return time;}
}
at com.zph.myclock.MyThread.run(MyClock.java:46)