0

For a few days now I have been working on a Timer program in Java. How it works is, you open the program and the JLabel counts up. For some reason it doesn't repeat itself and only works once.

Here's my code.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.SwingConstants;
import java.awt.Font;

public class TimerC extends JFrame implements ActionListener{

Timer t = new Timer(5, this);
public int intClicked;
public String stringClicked;
public JLabel clicked;
public TimerC() {
    t.start();
    JPanel p1 = new JPanel();
    getContentPane().add(p1);
    p1.setLayout(null);

    JLabel clicked = new JLabel();
    clicked.setFont(new Font("Tahoma", Font.PLAIN, 64));
    clicked.setHorizontalAlignment(SwingConstants.CENTER);
    clicked.setText("0");

    int intClicked = 1;
    String stringClicked = String.valueOf(intClicked);

    clicked.setText(stringClicked);

p1.add(clicked);

clicked.setSize(42, 100);
clicked.setLocation(191, 97);
     }

@Override
public void actionPerformed(ActionEvent e) {
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! – Andrew Thompson Oct 10 '15 at 18:31
  • `Timer t = new Timer(5, this);` The basic problem here is that the `actionPerformed` method of `this` does ..nothing! Change that first, to print something to `System.out`.. – Andrew Thompson Oct 10 '15 at 18:34
  • `p1.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 10 '15 at 18:35

3 Answers3

1

that line inside TimerC(), JLabel clicked = new JLabel(); remove JLabel infront clicked, because you have already declared clicked outside, and with this way your are declaring again as a local variable. Read about variable scope.

I don't know if you have noticed, but the size of the font that you use is big, and your JLabel size is not big enough, try change it's size:

clicked.setSize(200, 100);

Define some behavior to actionPerformed

   @Override
    public void actionPerformed(ActionEvent e) {
        clicked.setText(intClicked++ + " ");
       // this.repaint(); it's not necessary
    }
Johnny Willer
  • 3,717
  • 3
  • 27
  • 51
  • 1
    @AndrewThompson indeed – Johnny Willer Oct 10 '15 at 19:34
  • @RegularMoments that line inside `TimerC()`, `JLabel clicked = new JLabel();` remove `JLabel` infront `clicked`, because you have already declared clicked outside, and with this way your are declaring again as a local variable – Johnny Willer Oct 10 '15 at 19:37
1

As a good programming practice, it is always recommended to pass the self reference this to other objects, only after the construction of the object is complete, in the code flow. In the code example,

    public class TimerC extends JFrame implements ActionListener{

        Timer t = null;
        public JLabel clicked;

        public TimerC() {
            ...
            clicked = new JLabel();
            ...
            intClicked = 1;
            ...
            clicked.setSize(42, 100);
            clicked.setLocation(191, 97);

            t = new Timer(5, this);
            t.start();
        }

         @Override
         public void actionPerformed(ActionEvent e) {
             intClicked = intClicked + 1;
             clicked.setText(String.valueOf(intClicked));
             clicked.repaint();
         }
    }
deepak marathe
  • 409
  • 3
  • 10
0

For each second update, you would need Timer t = new Timer(1000, this); and use the actionPeformed as below:

public void actionPerformed(ActionEvent e) {
        System.out.println(e);
        clicked.setText(String.valueOf(intClicked++));
}
James Jithin
  • 10,183
  • 5
  • 36
  • 51