-1

I want to add a timer button to my main frame but it is in another class and I don't know how to use it in my main class. I need a timer button in my frame but I can't make it without another class. In that class I can't call my main frame. This is my code:

class ButtonTimer extends Thread{

private JButton button = new JButton(" ");
private int count = 1;
public ButtonTimer() {

    Timer time = new Timer(1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            button.setText(String.valueOf(count));
            count++;

        }
    });

    time.start();

    JFrame frame1 = new JFrame();
    frame1.add(button);
    frame1.setBounds(0, 20, 100, 50);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.pack();
    frame1.setVisible(true);
}
}

public class game {
public static void main(String[] args) {
    JFrame frame2 = new JFrame();
    frame2.setBounds(0, 0, 1000, 5000);
    frame2.setVisible(true);

    JLayeredPane jlp = new JLayeredPane();
    jlp.setBounds(0, 0, 1000, 500);

    frame2.add(jlp);

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new ButtonTimer();
        }
    });

}
}

How can I do this?

hichris123
  • 10,145
  • 15
  • 56
  • 70

2 Answers2

0

You Could...

Create a custom JButton that wraps a Timer internally to it.

This allows you to self contain the button and timer into a single unit and reuse it where ever you want...

You Could...

Create a custom Timer which takes a reference to a JButton and updates the text automatically on each trigger...

You Could...

Create a custom ActionListener or even Action that takes a reference to a JButton and updates the text and then pass this into a Timer instance of your choice...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • but how? can you give me an example? – user2851481 Jan 12 '14 at 23:18
  • You take any one of the suggestions and simply add it where you need it...Depending on what you are actually trying to do, you may need a reference to the frame you are trying to effect... – MadProgrammer Jan 12 '14 at 23:19
  • 3
    No, please explain in more details what is you are trying to achieve so I can better ascertain the best solution - This is not a code factory... – MadProgrammer Jan 12 '14 at 23:30
0

Have a try. Here we create a JButton in your main frame and then we set the text on actionPerformed of another class.

public class game1 {
    private static JFrame frame2;
    private static JButton button1=new JButton(" ");
public static void main(String[] args) {
    frame2 = new JFrame();
    frame2.setBounds(0, 0, 1000, 5000);

    JLayeredPane jlp = new JLayeredPane();
    jlp.setBounds(0, 0, 1000, 500);
    jlp.add(button1);
    frame2.add(jlp);

    frame2.add(button1);
    frame2.setVisible(true);
     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
       new ButtonTimer();

     }
    });

}

    private static class ButtonTimer {
private JButton button = new JButton(" ");
private int count = 1;
public ButtonTimer() {

    javax.swing.Timer timer = new javax.swing.Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                     button.setText(String.valueOf(count));
                     button1.setText(String.valueOf(count));
            count++;
                }
            });
    timer.start();

    JFrame frame1 = new JFrame();
    frame1.add(button);
    frame1.setBounds(0, 20, 100, 50);
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.pack();
    frame1.setVisible(true);
}
    }
}
ravibagul91
  • 20,072
  • 5
  • 36
  • 59