⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 digitalclock1.java

📁 it is a digital clock
💻 JAVA
字号:
/* name:pun ka wai
   student no.:50566685

    This exercise is to help you to get familiarized with the GUI of Java.

    This program template defines the GUI of a digital clock. 
    The time is displayed in 24hr format.
    You can compile and run the program.

    You are required to make the following changes to the program:
    1. The program will be terminated when the user close the frame window.
    2. Define an inner class that implements the ActionListener interface, and
         provide an implementation of the actionPerformed method.
    3. Create an action listener object and register it with the buttons so that
       the user can adjust the clock reading by clicking on the "hour" and "min"
       button.

    Please note that JBuilder may not support JDK 5.0, if you use features of
    JDK 5.0 in your program, then you may need to compile and run your program
    in a DOS command window.

*/

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


public class DigitalClock extends JFrame
{	
        private TimePanel displayPanel;
        private JPanel buttonPanel;  //panel used to hold the buttons
        private JButton hourB, minuteB,modeA,modeB;
        private Thread clockThread;
        private int hour = 0, minute = 0, second = 0;
        private String sent="";



        public static void main(String[] args)
        {	DigitalClock clock = new DigitalClock();
                clock.setTitle("My Digital Clock");
                clock.setSize(350,150);
                clock.setVisible(true);
                clock.time();
        }


        //constructor to create the DigitalClock frame
        public DigitalClock()
        {	Container contentPane = getContentPane();
                displayPanel = new TimePanel();
                contentPane.add(displayPanel, "Center");

                //instantiate the buttons and add them to the button panel
                buttonPanel = new JPanel();
                hourB = new JButton("hour");
                minuteB = new JButton("min");
                modeA = new JButton("12-hour");
                modeB= new JButton("24-hour");

                buttonPanel.add(hourB);
                buttonPanel.add(minuteB);
                buttonPanel.add(modeA);
                buttonPanel.add(modeB);

                contentPane.add(buttonPanel, "South");


            //Your task: set the option to terminate the program when the
            //           user closes the frame window
            this.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            });
                //Your task: create an action listener and register the 
                //           listener object to the buttons
                BottonIncrement Hour=new BottonIncrement(1);
                BottonIncrement Min=new BottonIncrement(2);
                BottonIncrement mode=new BottonIncrement(3);
                BottonIncrement mode1=new BottonIncrement(4);
                
                hourB.addActionListener(Hour);
                minuteB.addActionListener(Min);
                modeA.addActionListener(mode);
                modeB.addActionListener(mode1);

        }


        //The purpose of the TimePanel is to display the clock reading
        private class TimePanel extends JPanel
        {	private Font font = new Font("Monspaced", Font.BOLD, 48);

                public void paintComponent(Graphics g)
                {	super.paintComponent(g);
                        Graphics2D g2 = (Graphics2D)g;		
                        g2.setFont(font);
                        g2.setColor(Color.blue);
                        g2.drawString("" + hour/10 + hour%10 + ":" + minute/10 + minute%10
                                        + ":" + second/10 + second%10+sent, 10, 60);
                }
        }


        //update the clock reading every second
        public void time()
        {	clockThread = new Thread();
                clockThread.start();
                while (true)
                {	increment();
                        displayPanel.repaint(); //refresh the display
                        try {	
                                clockThread.sleep(1000);
                        }
                        catch (InterruptedException e)
                        { }
                }
        }

        private void increment()
        {	second++;
                if (second == 60)
                {	second = 0;
                        minute++;
                        if (minute == 60)
                        {	minute = 0;
                                hour++;
                                if (hour == 24)
                                        hour = 0;
                        }
                }
        }


        //Your task: provide the implementation of the ActionListener to handle
      //           the button events
      private class BottonIncrement implements ActionListener
      {
          int mode;
          public BottonIncrement(int number)
          {
              mode=number;
          }
          
          public void actionPerformed(ActionEvent event)
          {
              if(mode==1)
                  hour++;
              if (hour == 24)
                  hour = 0;             
              if(mode==2)
                  minute++;
              if (minute == 60)
            	  minute = 0;
              if (mode==3)
              {if(hour<12)
            		 sent="am";}            	  
              if(hour==12)
            	  sent="noon";
              if(hour>12)
              {
            	  hour-=12;
            	  sent="pm";
              }
 
              if(mode==4){
            	  
            	  if(sent=="pm")
            	  {
            		  hour+=12;
            		  sent="";
            	  }
            	  if(sent=="am")
            		  sent="";
              }
            
            	 
              }
              
            	  
          }              
             
}





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -