Home > Back-end >  Java Swing: do a digital clock with alarm clock function
Java Swing: do a digital clock with alarm clock function

Time:09-21

Bother everybody to help me see, I think another window click on the Settings button to set the alarm clock time, but running like this shows nothing



 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

Public class Clock extends JFrame {

Public Graphics mg;
JButton set, ok;
JLabel label, label1;
JFrame frame1 frame2;
JTextField time1, time2;
Int a=0, b=0;//get string in the input box

Public Clock () {
SetDefaultCloseOperation (EXIT_ON_CLOSE);
Frame1=new JFrame (" clock ");
Frame1. SetSize (300300);
Frame1. SetLocation (600250);
The set=new JButton (" set ");
Mg=this. GetGraphics ();
Add (set);
Set. SetLocation (10, 5);//set button location, size,
Frame1. SetVisible (true);
Set. AddActionListener (new Alarm ());
}

The class Alarm implements ActionListener {
Public void actionPerformed (an ActionEvent e) {
CloseThis (frame1);
Frame2=new JFrame (" set ");
Frame2. SetSize (200200);
Frame2. SetLocation (600250);
Ok=new JButton (" ok ");
Label=new JLabel (" set the alarm clock time: ");
Label1=new JLabel (" : ");
Time1=time2=new JTextField ();
Add (label); Add (label1);
Add (time1); Add (time2); Add (ok);
Label. SetLocation (10, 10);
Time1. SetBounds (20, 10, 10, 5);
Time2. SetBounds (20, 30, 10, 5);
Label1. SetLocation (20, 20);
Ok. SetLocation (40, 30);
Ok. AddActionListener (new Back ());
Frame2. SetVisible (true);
A=Integer. ParseInt (time1. GetText ());
B=Integer. ParseInt (time2. GetText ());
}
}

The class Back implements ActionListener {
Public void actionPerformed (an ActionEvent e) {
CloseThis (frame2);
Frame1. SetVisible (true);
}
}

Public void paint (Graphics mg) {
=30 int hh=16, mm, ss=0;//test, temporarily didn't write the code for current time
If (a==hh& & B==mm& & Ss==0)//setting time is the same as the current time
JOptionPane. ShowMessageDialog (this, "time's up!" "Alarm clock", JOptionPane. INFORMATION_MESSAGE);
}

Public void closeThis (JFrame frame) {
Frame. The dispose ();
}

Public static void main (String [] args) {
The new Clock ();
}

}
  • Related