Java 想要启动线程时钟而不首先点击某个东西并发出警报吗
你好,我想做一个线程来显示时钟,但有了这个代码,我只能让时钟运行后设置输入闹钟,虽然闹钟也没有工作。想把闹钟和钟分开吗 这是钟(正在工作) 这是GUI,Idk,当它与时钟相等时,在哪里放置报警验证,我不知道在哪里放置myThread.start或在运行时启动它的过程Java 想要启动线程时钟而不首先点击某个东西并发出警报吗,java,multithreading,user-interface,clock,alarm,Java,Multithreading,User Interface,Clock,Alarm,你好,我想做一个线程来显示时钟,但有了这个代码,我只能让时钟运行后设置输入闹钟,虽然闹钟也没有工作。想把闹钟和钟分开吗 这是钟(正在工作) 这是GUI,Idk,当它与时钟相等时,在哪里放置报警验证,我不知道在哪里放置myThread.start或在运行时启动它的过程 public class GUI extends javax.swing.JFrame { ClockAlarm myThread1; String Alarm; /**
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
}
公共类GUI扩展JFrame实现ActionListener{
公共图形用户界面(){
jLabel1=新的JLabel();
}
@凌驾
已执行的公共无效操作(操作事件e){
如果(例如getSource()==jButton1)
{
新的时钟线程(jLabel1.execute();
}
}
公共静态void main(字符串参数[]){
GUI gu=新GUI();
gu.setVisible(true);
}
}
公共类ClockThread扩展SwingWorker{
专用JLabel时钟;
私有字符串msg;
公共时钟线程(JLabel时钟){
这个时钟=时钟;
}
@凌驾
受保护的void done(){
试一试{
msg=get();
if(SwingUtilities.isEventDispatchThread()){
clock.setText(msg);
}
}捕获(例外e){
}
}
@凌驾
受保护的无效进程(列表资源){
超级工艺(res);
for(字符串结果:res){
if(SwingUtilities.isEventDispatchThread()){
clock.setText(结果);
clock.revalidate();
}
}
}
@凌驾
公共字符串doInBackground(){
字符串s=“”;
while(true){
试一试{
SimpleDataFormat sdf=新的SimpleDataFormat(“HH:mm:ss”);
日期=新日期();
s=sdf.格式(日期);
睡眠(1000);
出版;
}捕获(例外e){
返回s;
}
}
}
}
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
}
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;
}
}
}
}