【问题标题】:Wanna Initiate Threaded Clock without clicking something first and give alarm想要启动线程时钟而不先点击某些东西并发出警报
【发布时间】:2016-07-15 12:26:03
【问题描述】:

您好,我想创建一个线程来显示时钟,但是使用此代码我只能在设置输入闹钟后让时钟运行,尽管闹钟也没有工作。想让闹钟和时钟分开

这是时钟(正在工作)

public class ClockAlarm extends Thread{

private String txt;  
private JLabel Clock;
ClockAlarm(JLabel lbl) {
    this.Clock = lbl;
}

ClockAlarm(){
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public void run()
    {
        while(true)
        {
            try
            {
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                Date date = new Date();
                String s = sdf.format(date);
                Thread.sleep(1000);

                Clock.setText(s);
            }
            catch(Exception e){}

        }
}

}

这是 GUI,当它与时钟相等时,我不知道在哪里放置警报验证,我不知道在哪里放置 myThread.start 或在其运行时可以启动它的程序

public class GUI extends javax.swing.JFrame {
        ClockAlarm myThread1;
        String Alarm;

        /**
         * Creates new form GUI
         */
        public GUI() {
            initComponents();
        }
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            if(jButton1.getText().equalsIgnoreCase("Set Alarm (hh:mm:ss)")){
                Alarm = jTextField1.getText();
            }
            myThread1 = new ClockAlarm(jLabel1);
            myThread1.start();

        }                                        

        private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:

        }                                           

    public static void main(String args[]) {

            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new GUI().setVisible(true);

                }

            });
        }

        // Variables declaration - do not modify                     
        private javax.swing.JButton jButton1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration                   
    }

【问题讨论】:

    标签: java multithreading user-interface clock alarm


    【解决方案1】:
    public class GUI extends JFrame implements ActionListener{
    
        public GUI() {
             jLabel1=new JLabel();
        }
    
    
    
        @Override
        public void actionPerformed(ActionEvent e) {
    
            if(e.getSource()==jButton1)
            {
                   new ClockThread(jLabel1).execute();
            }
    
        }
    
    public static void main(String args[]) {
            GUI gu=new GUI();
            gu.setVisible(true);
        }
    
    }
    
    public class ClockThread extends SwingWorker<String, String> {
    
    private JLabel clock;
    private String msg;
    
    public ClockThread(JLabel clock) {
        this.clock = clock;
    }
    
    @Override
    protected void done() {
        try {
            msg = get();
            if (SwingUtilities.isEventDispatchThread()) {
                clock.setText(msg);
            }
        } catch (Exception e) {
        }
    }
    
    @Override
    protected void process(List<String> res) {
        super.process(res);
        for (String result : res) {
            if (SwingUtilities.isEventDispatchThread()) {
                clock.setText(result);
                clock.revalidate();
            }
        }
    }
    
    @Override
    public String doInBackground() {
        String s = "";
        while (true) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
                Date date = new Date();
                s = sdf.format(date);
                Thread.sleep(1000);
                publish(s);
            } catch (Exception e) {
                return s;
            }
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      相关资源
      最近更新 更多