Código
package base; import gui.Gui; import javax.swing.SwingUtilities; public class Main { public void run(){ new Gui(); } }); } }
Código
package gui; import java.awt.Component; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Time; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import base.CheckTime; private int width = 370,height = 150; private int maxHour = 24,maxMin = 60,maxSec = 60; private JComboBox<Integer> hour = new JComboBox<Integer>(); private JComboBox<Integer> min = new JComboBox<Integer>(); private JComboBox<Integer> sec = new JComboBox<Integer>(); private boolean isStarted = false; public Gui(){ setSize(width,height); setTitle(windowTitle); setLocationRelativeTo(null); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); configureJComboBoxes(); configureJButton(); setVisible(true); } GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0); container.add(component, gbc); } public void configureJComboBoxes(){ for(int i = 0; i < maxHour; i++) hour.addItem(i); for(int i = 0; i < maxMin; i++) min.addItem(i); for(int i = 0; i < maxSec; i++) sec.addItem(i); addComponentGBL(this,hour,0,0,1,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 0, 90, 40, 0); addComponentGBL(this,min,1,0,1,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 0, 0, 40, 0); addComponentGBL(this,sec,2,0,1,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 0, 0, 40, 90); } public void configureJButton(){ addComponentGBL(this,programShutdown,0,0,0,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 40, 0, 0, 150); addComponentGBL(this,cancelShutdown,0,0,0,0,GridBagConstraints.CENTER, GridBagConstraints.CENTER, 40, 150, 0, 0); programShutdown.addActionListener(this); cancelShutdown.addActionListener(this); } CheckTime time = new CheckTime(hour.getSelectedIndex(), min.getSelectedIndex(), sec.getSelectedIndex()); if(evt.getSource() == programShutdown && isStarted == false){ time.execute(); isStarted = true; } else if(evt.getSource() == cancelShutdown && isStarted == true){ time.stopThread(); isStarted = false; } } }
Código
package base; import java.util.Calendar; import javax.swing.SwingWorker; public class CheckTime extends SwingWorker<Object, Object> { private int copyHour; private int copyMin; private int copySec; private boolean running; public CheckTime(int hour, int min, int sec) { copyHour = hour; copyMin = min; copySec = sec; } Calendar calendario; int thisHour; int thisMin; int thisSec; this.running = true; do { if (copyHour == thisHour && copyMin == thisMin && copySec == thisSec) { this.running = false; } try { } } while (this.running); return null; } public void stopThread() { this.running = false; } }
Un saludo.