Whats wrong with my code ?? Whenever i click start button it will just printout start to the jtextfield it wont start the time .
import java.awt.*; import java.util.Date; import javax.swing.*; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.*; public class MicroWave extends JFrame implements ActionListener { JButton n0,n1,n2,n3,n4; JButton n5,n6,n7,n8,n9; JButton start,stop; JPanel keyButtons; JPanel South; JPanel North; JPanel West; JPanel East; JPanel time; Timer starter; JTextField screen; long cd = 1800000; java.text.SimpleDateFormat tformat = new java.text.SimpleDateFormat(" hh : mm : ss"); JTextField timer = new JTextField(tformat.format(new Date(cd)),JTextField.CENTER); public void actionPerformed(ActionEvent arg0) { if(timer.getText().equals("00:00:00")) { timer.setText(""); JButton source = (JButton)arg0.getSource(); timer.replaceSelection(arg0.getActionCommand()); } else{ JButton source = (JButton)arg0.getSource(); timer.replaceSelection(arg0.getActionCommand()); } if(arg0.getActionCommand()=="Start"){ ActionListener al = new ActionListener(){ long x = cd - 1000; public void actionPerformed(ActionEvent ae){ timer.setText(tformat.format(new Date(x))); x -= 1000;}}; starter = new Timer(1000, al); starter.start(); } } public MicroWave(){ n1 = new JButton("1"); n2 = new JButton("2"); n3 = new JButton("3"); n4 = new JButton("4"); n5 = new JButton("5"); n6 = new JButton("6"); n7 = new JButton("7"); n8 = new JButton("8"); n9 = new JButton("9"); start = new JButton("START"); n0 = new JButton("0"); stop = new JButton("STOP"); North= new JPanel(new BorderLayout(260,0)); South= new JPanel(new FlowLayout(FlowLayout.RIGHT)); West = new JPanel(new FlowLayout(FlowLayout.LEFT)); East = new JPanel(new FlowLayout(FlowLayout.RIGHT)); keyButtons = new JPanel(); time = new JPanel(); screen = new JTextField("SCREEN"); West.add(screen); time.add(timer); North.add(time,BorderLayout.EAST); South = new JPanel(); } public void launchFrame(){ keyButtons.setLayout(new GridLayout(4,3)); South.setPreferredSize(new Dimension(100,100)); screen.setPreferredSize(new Dimension(440,700)); timer.setPreferredSize(new Dimension(227,70)); keyButtons.setPreferredSize(new Dimension(235,250)); screen.add(new JTextField(8)); //INPUT WILL START AT THE CENTER timer.setHorizontalAlignment(JTextField.CENTER); screen.setHorizontalAlignment(JTextField.CENTER); //COLOR FOR JTEXTFIELD BACKGROUND// timer.setBackground(Color.DARK_GRAY); screen.setBackground(Color.DARK_GRAY); //FONT COLOR // screen.setForeground(Color.RED); timer.setForeground(Color.RED); //FONT SIZE// screen.setFont(new Font("Tahoma",1,55)); timer.setFont(new Font("BOLD",1,35)); keyButtons.add(n1); keyButtons.add(n2); keyButtons.add(n3); keyButtons.add(n4); keyButtons.add(n5); keyButtons.add(n6); keyButtons.add(n7); keyButtons.add(n8); keyButtons.add(n9); keyButtons.add(start); keyButtons.add(n0); keyButtons.add(stop); n1.addActionListener(this); n2.addActionListener(this); n3.addActionListener(this); n4.addActionListener(this); n5.addActionListener(this); n6.addActionListener(this); n7.addActionListener(this); n8.addActionListener(this); n9.addActionListener(this); n0.addActionListener(this); start.addActionListener(this); stop.addActionListener(this); JFrame dos = new JFrame(); dos.setSize(700,500); dos.add(keyButtons,BorderLayout.EAST); dos.add(screen,BorderLayout.WEST); dos.add(North,BorderLayout.NORTH); dos.add(South,BorderLayout.SOUTH); dos.setVisible(true); dos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main (String args[]){ MicroWave GUI = new MicroWave(); GUI.launchFrame(); } }
--- Update ---
if i remove IF S TATEMET it will start the countdown even if i click any jbuttons
import java.awt.*; import java.util.Date; import javax.swing.*; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.*; public class MicroWave extends JFrame implements ActionListener { JButton n0,n1,n2,n3,n4; JButton n5,n6,n7,n8,n9; JButton start,stop; JPanel keyButtons; JPanel South; JPanel North; JPanel West; JPanel East; JPanel time; Timer starter; JTextField screen; long cd = 1800000; java.text.SimpleDateFormat tformat = new java.text.SimpleDateFormat(" hh : mm : ss"); JTextField timer = new JTextField(tformat.format(new Date(cd)),JTextField.CENTER); public void actionPerformed(ActionEvent arg0) { if(timer.getText().equals("00:00:00")) { timer.setText(""); JButton source = (JButton)arg0.getSource(); timer.replaceSelection(arg0.getActionCommand()); } else{ JButton source = (JButton)arg0.getSource(); timer.replaceSelection(arg0.getActionCommand()); } ActionListener al = new ActionListener(){ long x = cd - 1000; public void actionPerformed(ActionEvent ae){ timer.setText(tformat.format(new Date(x))); x -= 1000;}}; starter = new Timer(1000, al); starter.start(); } public MicroWave(){ n1 = new JButton("1"); n2 = new JButton("2"); n3 = new JButton("3"); n4 = new JButton("4"); n5 = new JButton("5"); n6 = new JButton("6"); n7 = new JButton("7"); n8 = new JButton("8"); n9 = new JButton("9"); start = new JButton("START"); n0 = new JButton("0"); stop = new JButton("STOP"); North= new JPanel(new BorderLayout(260,0)); South= new JPanel(new FlowLayout(FlowLayout.RIGHT)); West = new JPanel(new FlowLayout(FlowLayout.LEFT)); East = new JPanel(new FlowLayout(FlowLayout.RIGHT)); keyButtons = new JPanel(); time = new JPanel(); screen = new JTextField("SCREEN"); West.add(screen); time.add(timer); North.add(time,BorderLayout.EAST); South = new JPanel(); } public void launchFrame(){ keyButtons.setLayout(new GridLayout(4,3)); South.setPreferredSize(new Dimension(100,100)); screen.setPreferredSize(new Dimension(440,700)); timer.setPreferredSize(new Dimension(227,70)); keyButtons.setPreferredSize(new Dimension(235,250)); screen.add(new JTextField(8)); //INPUT WILL START AT THE CENTER timer.setHorizontalAlignment(JTextField.CENTER); screen.setHorizontalAlignment(JTextField.CENTER); //COLOR FOR JTEXTFIELD BACKGROUND// timer.setBackground(Color.DARK_GRAY); screen.setBackground(Color.DARK_GRAY); //FONT COLOR // screen.setForeground(Color.RED); timer.setForeground(Color.RED); //FONT SIZE// screen.setFont(new Font("Tahoma",1,55)); timer.setFont(new Font("BOLD",1,35)); keyButtons.add(n1); keyButtons.add(n2); keyButtons.add(n3); keyButtons.add(n4); keyButtons.add(n5); keyButtons.add(n6); keyButtons.add(n7); keyButtons.add(n8); keyButtons.add(n9); keyButtons.add(start); keyButtons.add(n0); keyButtons.add(stop); n1.addActionListener(this); n2.addActionListener(this); n3.addActionListener(this); n4.addActionListener(this); n5.addActionListener(this); n6.addActionListener(this); n7.addActionListener(this); n8.addActionListener(this); n9.addActionListener(this); n0.addActionListener(this); start.addActionListener(this); stop.addActionListener(this); JFrame dos = new JFrame(); dos.setSize(700,500); dos.add(keyButtons,BorderLayout.EAST); dos.add(screen,BorderLayout.WEST); dos.add(North,BorderLayout.NORTH); dos.add(South,BorderLayout.SOUTH); dos.setVisible(true); dos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main (String args[]){ MicroWave GUI = new MicroWave(); GUI.launchFrame(); } }