Hello everyone,
I have some problems with my program , hope you can help me to solve it.
1- I need a method for the Clear Time button that remove the counter to be 0.
` private void btclearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
min=0;sec=0;
lblsec.remove(lblsec);
lblsec.setText("00");
sec = Integer.parseInt(lblsec.getText());
lblmin.setText("00");
min = Integer.parseInt(lblmin.getText());
} `
2- When i press the start button, the counter starts , After that i press the stop button and i want to add more time to the counter, but when i press the start button the counter starts with the old time not in new added time.
this is my code:
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class JFrame extends javax.swing.JFrame {
int sec,min,min1,sec1;
Timer timer;
boolean flag = true;
boolean ifstop = false;
public JFrame() {
initComponents();
}
public void start(){
bt10s.setEnabled(false);
bt20s.setEnabled(false);
bt30s.setEnabled(false);
bt1m.setEnabled(false);
bt5m.setEnabled(false);
bt10m.setEnabled(false);
btclear.setEnabled(false);
btOpenDoor.setEnabled(false);
}
public void stop(){
bt10s.setEnabled(true);
bt20s.setEnabled(true);
bt30s.setEnabled(true);
bt1m.setEnabled(true);
bt5m.setEnabled(true);
bt10m.setEnabled(true);
btclear.setEnabled(true);
btOpenDoor.setEnabled(true);
btCloseDoor.setEnabled(true);
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
lblmin = new javax.swing.JLabel();
lblsec = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
bt10s = new javax.swing.JButton();
bt20s = new javax.swing.JButton();
bt30s = new javax.swing.JButton();
bt1m = new javax.swing.JButton();
bt5m = new javax.swing.JButton();
bt10m = new javax.swing.JButton();
btclear = new javax.swing.JButton();
bTstop = new javax.swing.JButton();
bTstart = new javax.swing.JButton();
btOpenDoor = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
lblDoor = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
btCloseDoor = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Microwave_Simulation");
setAlwaysOnTop(true);
setBackground(new java.awt.Color(255, 255, 255));
setResizable(false);
jPanel1.setBackground(new java.awt.Color(255, 255, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
jPanel1.setForeground(new java.awt.Color(255, 255, 255));
lblmin.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
lblmin.setText("00");
lblsec.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
lblsec.setText("00");
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jLabel5.setText(":");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lblmin)
.addGap(18, 18, 18)
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(lblsec)
.addGap(80, 80, 80))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lblmin, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblsec, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel2.setBackground(new java.awt.Color(204, 204, 204));
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Set Time", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.TOP));
bt10s.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bt10s.setText("10Sec");
bt10s.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt10sActionPerformed(evt);
}
});
bt20s.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bt20s.setText("20Sec");
bt20s.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt20sActionPerformed(evt);
}
});
bt30s.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bt30s.setText("30Sec");
bt30s.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt30sActionPerformed(evt);
}
});
bt1m.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bt1m.setText("1Min");
bt1m.setMaximumSize(new java.awt.Dimension(75, 25));
bt1m.setMinimumSize(new java.awt.Dimension(75, 25));
bt1m.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt1mActionPerformed(evt);
}
});
bt5m.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bt5m.setText("5Min");
bt5m.setMaximumSize(new java.awt.Dimension(75, 25));
bt5m.setMinimumSize(new java.awt.Dimension(75, 25));
bt5m.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt5mActionPerformed(evt);
}
});
bt10m.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bt10m.setText("10Min");
bt10m.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bt10mActionPerformed(evt);
}
});
btclear.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btclear.setText("Clear Time");
btclear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btclearActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(btclear, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(bt1m, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bt5m, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(bt10s, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(bt20s, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(bt30s, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(bt10m, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bt10s, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bt20s, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bt30s, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bt5m, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bt1m, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bt10m, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btclear, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
bTstop.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bTstop.setText("Stop");
bTstop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bTstopActionPerformed(evt);
}
});
bTstart.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
bTstart.setText("Start");
bTstart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bTstartActionPerformed(evt);
}
});
btOpenDoor.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btOpenDoor.setText("Open Door");
btOpenDoor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btOpenDoorActionPerformed(evt);
}
});
jPanel3.setLayout(null);
lblDoor.setFont(new java.awt.Font("Trebuchet MS", 1, 24)); // NOI18N
lblDoor.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
lblDoor.setLabelFor(btOpenDoor);
lblDoor.setText("Door Close");
jPanel3.add(lblDoor);
lblDoor.setBounds(90, 30, 170, 60);
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\vip\\Videos\\Captures\\UID_assignment 2(1).pdf - Adobe Acrobat Reader DC 14_08_41 06_47_32 م.png")); // NOI18N
jLabel1.setAlignmentX(1.0F);
jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 5));
jPanel3.add(jLabel1);
jLabel1.setBounds(-30, -10, 380, 380);
btCloseDoor.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btCloseDoor.setText("Close Door");
btCloseDoor.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btCloseDoorActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(bTstart)
.addGap(103, 103, 103)
.addComponent(bTstop)
.addGap(16, 16, 16))
.addGroup(layout.createSequentialGroup()
.addComponent(btOpenDoor)
.addGap(18, 18, 18)
.addComponent(btCloseDoor)
.addGap(17, 17, 17)))
.addGap(20, 20, 20))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(12, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bTstart, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(bTstop, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btOpenDoor, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(btCloseDoor, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
getAccessibleContext().setAccessibleDescription("");
pack();
}// </editor-fold>
private void bt20sActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
lblsec.setText("20");
sec = Integer.parseInt(lblsec.getText());
}
private void bt5mActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
lblmin.setText("05");
min = Integer.parseInt(lblmin.getText());
}
private void btclearActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
min=0;sec=0;
lblsec.remove(lblsec);
lblsec.setText("00");
sec = Integer.parseInt(lblsec.getText());
lblmin.setText("00");
min = Integer.parseInt(lblmin.getText());
}
private void bt10mActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
lblmin.setText("10");
min = Integer.parseInt(lblmin.getText());
}
private void bTstartActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
start();
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if(ifstop){
min = min1;
sec = sec1;
ifstop = false;
}
if (sec==0){
sec=60;
min--;
}
if (min<0){
lblDoor.setText("Stand By");
stop();
min=0;sec=0;
timer.stop();
}else{
lblDoor.setText("Cooking");
btCloseDoor.setEnabled(false);
sec--;
if(sec<10){
lblsec.setText("0"+sec);
flag = false;
}
if(min<10){
lblmin.setText("0"+min);
if(sec<10)
lblsec.setText("0"+sec);
else
lblsec.setText(""+sec);
flag = false;
}
if(flag){
lblsec.setText(""+sec);
lblmin.setText(""+min);
}
}
}
});
timer.start();
}
private void bTstopActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
stop();
lblDoor.setText("Stand By");
min1 = min;
sec1 = sec;
ifstop = true;
timer.stop();
}
private void btOpenDoorActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
bTstart.setEnabled(false);
jLabel1.setForeground(Color.blue);
lblDoor.setText("Door Open");
}
private void bt10sActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
lblsec.setText("10");
sec = Integer.parseInt(lblsec.getText());
}
private void bt30sActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
lblsec.setText("30");
sec = Integer.parseInt(lblsec.getText());
}
private void bt1mActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
lblmin.setText("01");
min = Integer.parseInt(lblmin.getText());
}
private void btCloseDoorActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
bTstart.setEnabled(true);
lblDoor.setText("Door Close");
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bTstart;
private javax.swing.JButton bTstop;
private javax.swing.JButton bt10m;
private javax.swing.JButton bt10s;
private javax.swing.JButton bt1m;
private javax.swing.JButton bt20s;
private javax.swing.JButton bt30s;
private javax.swing.JButton bt5m;
private javax.swing.JButton btCloseDoor;
private javax.swing.JButton btOpenDoor;
private javax.swing.JButton btclear;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JLabel lblDoor;
private javax.swing.JLabel lblmin;
private javax.swing.JLabel lblsec;
// End of variables declaration
}