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

📄 exercise14_19.java

📁 Introduction to java programming 一书中所有编程练习部分的源码
💻 JAVA
字号:
// Exercise14_19.java: Display a clock and set alarmimport javax.swing.*;import javax.swing.border.*;import java.applet.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.net.URL;public class Exercise14_19 extends JApplet implements Runnable, ActionListener {  private Thread thread;  private DisplayPanel hourPanel, minutePanel, secondPanel;  private JButton jbtSetAlarm;  private JCheckBox jchkAlarm;  private int hourAlarm, minuteAlarm, secondAlarm;  private AudioClip alarmSound;  public void init() {    // Panel p to hold the digital clock    JPanel p = new JPanel();    hourPanel = new DisplayPanel();    hourPanel.setTitle("Hour");    minutePanel = new DisplayPanel();    minutePanel.setTitle("Minute");    secondPanel = new DisplayPanel();    secondPanel.setTitle("Second");    p.setBorder(new LineBorder(Color.red, 1));    p.setLayout(new GridLayout(1, 3, 5, 5));    p.add(hourPanel);    p.add(minutePanel);    p.add(secondPanel);    Panel p1 = new Panel();    p1.setLayout(new FlowLayout(FlowLayout.CENTER));    p1.add(jchkAlarm = new JCheckBox("Alarm"));    p1.add(jbtSetAlarm = new JButton("Set alarm"));    getContentPane().setLayout(new BorderLayout());    getContentPane().add(p, BorderLayout.CENTER);    getContentPane().add(p1, BorderLayout.SOUTH);    // Play a sound    URL url = this.getClass().getResource("Ticker.au");    alarmSound = Applet.newAudioClip(url);    jbtSetAlarm.addActionListener(this);    thread = new Thread(this);    thread.start();  }  // Handle the button action  public void actionPerformed(ActionEvent e) {    JFrame frame = new SetAlarmFrame();    frame.pack();    frame.setVisible(true);  }  public void run() {    while (true) {      GregorianCalendar cal = new GregorianCalendar();      int hour = cal.get(Calendar.HOUR);      int minute = cal.get(Calendar.MINUTE);      int second = cal.get(Calendar.SECOND);      hourPanel.setContent(hour+"");      minutePanel.setContent(minute+"");      secondPanel.setContent(second+"");      checkAlarm(hour, minute, second);      try {        Thread.sleep(1000);      }      catch (InterruptedException ex) {      }    }  }  public void checkAlarm(int h, int m, int s) {    if (jchkAlarm.isSelected() && (h == hourAlarm) && (m == minuteAlarm)      && (s == secondAlarm))      alarmSound.play();  }  // Main method  public static void main(String[] args) {    // Create a frame    JFrame frame = new JFrame("Exercise14_19: Alarm Clock");    // Create an instance of the applet    Exercise14_19 applet = new Exercise14_19();    // Add the applet instance to the frame    frame.getContentPane().add(applet, BorderLayout.CENTER);    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    // Invoke init() and start()    applet.init();    applet.start();    // Display the frame    frame.setSize(500, 200);    frame.setVisible(true);  }  // Inner class  class DisplayPanel extends JPanel {    private String title = "default";    private String content = "default";    private JLabel jlblTitle;    private DisplayCenteredOnPanel panel;    DisplayPanel() {      setLayout(new BorderLayout());      add(jlblTitle = new JLabel(title, JLabel.CENTER),        BorderLayout.NORTH);      add(panel = new DisplayCenteredOnPanel(),        BorderLayout.CENTER);      jlblTitle.setBackground(Color.white);      panel.setBackground(Color.white);    }    public void setTitle(String s) {      jlblTitle.setText(s);    }    public void setContent(String s) {      panel.setMessage(s);    }  }// Inner classclass SetAlarmFrame extends JFrame implements ActionListener {  private JTextField jtfHour, jtfMinute, jtfSecond;  private JButton jbtOK, jbtCancel;  public SetAlarmFrame() {    setTitle("Set alarm");    JPanel p = new JPanel();    p.setLayout(new GridLayout(3, 2));    p.add(new JLabel("Hour"));    p.add(jtfHour = new JTextField());    p.add(new JLabel("Minute"));    p.add(jtfMinute = new JTextField());    p.add(new JLabel("Second"));    p.add(jtfSecond = new JTextField());    p.setBorder(new TitledBorder("Enter Hour, Minute, and Second"));    JPanel p1 = new JPanel();    p1.setLayout(new FlowLayout(FlowLayout.RIGHT));    p1.add(jbtOK = new JButton("OK"));    p1.add(jbtCancel = new JButton("Cancel"));    getContentPane().add(p, BorderLayout.CENTER);    getContentPane().add(p1, BorderLayout.SOUTH);    jbtOK.addActionListener(this);    jbtCancel.addActionListener(this);  }  public void actionPerformed(ActionEvent e) {    String actionCommand = e.getActionCommand();    if (actionCommand.equals("OK")) {      hourAlarm =        Integer.parseInt(jtfHour.getText().trim());      minuteAlarm =        Integer.parseInt(jtfMinute.getText().trim());      secondAlarm =        Integer.parseInt(jtfSecond.getText().trim());    }    setVisible(false);    dispose();  }}}class DisplayCenteredOnPanel extends JPanel {  private String message = " ";  public void setMessage(String message) {    this.message = message;    repaint();  }  public void paintComponent(Graphics g) {    super.paintComponent(g);    //create and set font    Font f = new Font("Helvetica", Font.BOLD, 26);    g.setFont(f);    //get font metrics for the font    FontMetrics fm = g.getFontMetrics(f);    //find the center location to display    int w = fm.stringWidth(message);  //get the string width    int h = fm.getAscent();    int x = (getSize().width-w)/2;    int y = (getSize().height+h)/2;    g.drawString(message, x, y);  }  public Dimension getPreferredSize() {    return new Dimension(200, 200);  }}

⌨️ 快捷键说明

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