public class GUI {
double counter = 1;
double inputA = 5;
int delay = 1000;
//--- Timer Code ---
public void timer(){
Timer timer = new Timer(delay, null);
}
ActionListener counting = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (counter <= inputA) {
timer.stop();
} else {
System.out.println(counter);
counter++;
txtCounter.setText(counter + "");
}
}
//-------------------
private JFrame frmTimer;
private JTextField txtEnterHowLong;
//Launch the Application
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.frmTimer.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//--- Create the Application ---
private JTextField txtCounter;
private JPanel panel;
private JTextField txtCurrentTimer;
private final Checkbox checkbox = new Checkbox("Play sound when time is up?");
public GUI() {
initialize();
}
//--- Initialize contents of frame ---
public void initialize() {
frmTimer = new JFrame();
frmTimer.setTitle("Timer");
frmTimer.setBounds(100, 100, 350, 160);
frmTimer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmTimer.getContentPane().setLayout(null);
//--- Button "Start Timing" created ---
JButton btnStartTiming = new JButton("Start Timing");
btnStartTiming.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent counting) {
new Timer(delay, counting).start();
}
});
btnStartTiming.setRequestFocusEnabled(false);
panel = new JPanel();
panel.setBounds(10, 11, 200, 22);
frmTimer.getContentPane().add(panel);
panel.setLayout(null);
//--- Input field created ---
txtEnterHowLong = new JTextField();
txtEnterHowLong.setEditable(true);
txtEnterHowLong.setBounds(0, 0, 200, 22);
panel.add(txtEnterHowLong);
txtEnterHowLong.setToolTipText("Time in Seconds");
btnStartTiming.setBounds(220, 10, 110, 23);
frmTimer.getContentPane().add(btnStartTiming);
//--- Button "Stop Timing" created ---
JButton btnStopTiming = new JButton("Stop Timing");
btnStopTiming.setBounds(220, 40, 110, 23);
frmTimer.getContentPane().add(btnStopTiming);
//--- Static counter created ---
txtCounter = new JTextField();
txtCounter.setText("Static Timer");
txtCounter.setToolTipText("Shows total time that has passed.");
txtCounter.setBounds(108, 67, 100, 20);
frmTimer.getContentPane().add(txtCounter);
txtCounter.setColumns(10);
//--- Current timer created ---
txtCurrentTimer = new JTextField();
txtCurrentTimer.setToolTipText("Shows current time that has passed.");
txtCurrentTimer.setText("Current Timer");
txtCurrentTimer.setBounds(10, 67, 86, 20);
frmTimer.getContentPane().add(txtCurrentTimer);
txtCurrentTimer.setColumns(10);
//--- Checkbox ---
checkbox.setFont(new Font("Dialog", Font.BOLD, 12));
checkbox.setBounds(10, 32, 200, 31);
frmTimer.getContentPane().add(checkbox);
checkbox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent arg0) {
if (counter == inputA) {
sound.play("test.wav");
} else {
//Do nothing
}
}
});
//------------------------------
JMenuBar menuBar = new JMenuBar();
frmTimer.setJMenuBar(menuBar);
JMenu mnView = new JMenu("View");
menuBar.add(mnView);
JMenu mnAbout = new JMenu("About");
menuBar.add(mnAbout);
}
};
}