小弟有一个作业 要求用swing+applet 实现一个闹钟,要具有最基本的报时功能,能不能给点思路

解决方案 »

  1.   

    baobao28(瓜瓜) 
    能发一份到 [email protected] 吗
    谢谢了!
      

  2.   

    我直接贴出来吧,写完才发现代码不是很多,对了,APPLET我不会的,我写了个SWING版的,你如果会APPLET,改起来应该不难的主窗体类
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Timer;public class clockFrame extends JFrame {
      JPanel jPanel1 = new JPanel();
      JTextField jTextField1 = new JTextField();
      JTextField jTextField2 = new JTextField();
      JTextField jTextField3 = new JTextField();
      JButton jButton1 = new JButton();
      JLabel jLabel1 = new JLabel();
      JLabel jLabel2 = new JLabel();
      Timer timer;
      TimeThread tt;
      public clockFrame() {
        try {
          jbInit();
          this.setSize(300,100);
          Dimension nowsize=Toolkit.getDefaultToolkit().getScreenSize();
          this.setLocation((nowsize.width-300)/2,(nowsize.height-100)/2);
          this.show();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        clockFrame clockFrame1 = new clockFrame();
      }
      private void jbInit() throws Exception {
        jTextField1.setText("");
        jTextField1.setColumns(5);
        jTextField2.setText("");
        jTextField2.setColumns(5);
        jTextField3.setText("");
        jTextField3.setColumns(5);
        jButton1.setActionCommand("jButton1");
        jButton1.setText("开始");
        jButton1.addActionListener(new clockFrame_jButton1_actionAdapter(this));
        jLabel1.setText(":");
        jLabel2.setText(":");
        this.addWindowListener(new clockFrame_this_windowAdapter(this));
        this.getContentPane().add(jPanel1,  BorderLayout.CENTER);
        jPanel1.add(jTextField1, null);
        jPanel1.add(jLabel1, null);
        jPanel1.add(jTextField2, null);
        jPanel1.add(jLabel2, null);
        jPanel1.add(jTextField3, null);
        jPanel1.add(jButton1, null);
      }  void this_windowClosing(WindowEvent e) {
        System.exit(0);
      }  void jButton1_actionPerformed(ActionEvent e) {
        int hour=Integer.parseInt(jTextField1.getText());
        int minute=Integer.parseInt(jTextField2.getText());
        int second=Integer.parseInt(jTextField3.getText());    Timer timer=new Timer();
        tt=new TimeThread(timer,hour,minute,second);
        tt.start();
      }
    }class clockFrame_this_windowAdapter extends java.awt.event.WindowAdapter {
      clockFrame adaptee;  clockFrame_this_windowAdapter(clockFrame adaptee) {
        this.adaptee = adaptee;
      }
      public void windowClosing(WindowEvent e) {
        adaptee.this_windowClosing(e);
      }
    }class clockFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {
      clockFrame adaptee;  clockFrame_jButton1_actionAdapter(clockFrame adaptee) {
        this.adaptee = adaptee;
      }
      public void actionPerformed(ActionEvent e) {
        adaptee.jButton1_actionPerformed(e);
      }
    }
      

  3.   

    timer类
    import java.util.Timer;
    import java.text.SimpleDateFormat;
    import java.util.Date;public class TimeThread extends Thread{
      Timer timer;
      long clock;  public TimeThread(Timer timer,int hour,int minute,int second) {
        this.timer=timer;
        long tt=0;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
        Date currentTime = new Date();
        String set_date=formatter2.format(currentTime)+" "+hour+":"+minute+":"+second;
        String now_date=formatter.format(currentTime);
        try {
          tt=formatter.parse(set_date).getTime() - formatter.parse(now_date).getTime();
          if(tt < 0)
          {
            tt=tt+24*60*60*1000;
          }
        }
        catch (Exception ex) {
        }    clock=tt;
      }  public void run(){
        timer.schedule(new MyTask(), clock, 24*60*60*1000);
      }
    }
    mytask类
    import java.util.TimerTask;public class MyTask extends TimerTask{
      public MyTask() {
      }  public void run() {
        new swmsg();
      }
    }
    显示到点信息类
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    public class swmsg extends JFrame{
      public swmsg() {
        JOptionPane.showMessageDialog(this, "到点了", "信息",
                                        JOptionPane.INFORMATION_MESSAGE);
      }
    }
      

  4.   

    要有时钟的界面,我想建立3个类,主类用于显示界面,时钟类继承JPanel,实现Runnable,设置闹钟类用对话框的地形式出现的
    不知道怎么样把JPanel加入到Applet当中,