1

Why is my thread is not working?

I want to show a message on JFrame for a short time ..then I want to close the message and start my main JPanel.

Should I put the thread outside my main class?

I searched on Google about more..but I am hopeless.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class SwitchCheck {

    public static void main(String[] args) {

        final JFrame JF = new JFrame("SwitchJpanel");
        JF.setSize(300, 200);
        JF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Thread Cl = new Thread() {
            public void run() {
                try {
                    JPanel P = new JPanel(null);
                    JF.getContentPane().add(P);
                    JButton L = new JButton("Wellcome");
                    L.setBounds(20, 30, 100, 20);
                    P.add(L);
                    sleep(10000000);
                } catch (Exception e) {

                }
            }
        };
        Cl.start();
        JF.getContentPane().removeAll();
        JF.getContentPane().revalidate();
        final JPanel P1 = new JPanel(null);
        JF.getContentPane().add(P1);
        JLabel L1 = new JLabel("Left");
        L1.setBounds(10, 20, 100, 20);
        P1.add(L1);
        JLabel L2 = new JLabel("Right");
        L2.setBounds(120, 20, 100, 20);
        P1.add(L2);
        final JButton B1 = new JButton("Panel1");
        B1.setBounds(20, 60, 100, 20);
        P1.add(B1);
        final JButton B2 = new JButton("Panel2");
        B2.setBounds(120, 60, 100, 20);
        P1.add(B2);
        B1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                JF.getContentPane().removeAll();
                JPanel P1 = new JPanel(null);
                JF.getContentPane().add(P1);
                JF.getContentPane().revalidate();
                JLabel L1 = new JLabel("Left");
                L1.setBounds(10, 20, 100, 20);
                P1.add(L1);
                JLabel L2 = new JLabel("Right");
                L2.setBounds(120, 20, 100, 20);
                P1.add(L2);
                B1.setText("Panel1");
                B1.setBounds(20, 60, 100, 20);
                P1.add(B1);
                B2.setText("Panel2");
                B2.setBounds(120, 60, 100, 20);
                P1.add(B2);
            }

        });
        B2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                JF.getContentPane().removeAll();
                JPanel P2 = new JPanel(null);
                JF.getContentPane().add(P2);
                JF.getContentPane().revalidate();

                JLabel L3 = new JLabel("UP");
                L3.setBounds(10, 20, 100, 20);
                P2.add(L3);
                JLabel L4 = new JLabel("Down");
                L4.setBounds(120, 20, 100, 20);
                P2.add(L4);
                B1.setText("Panel3");
                B1.setBounds(20, 60, 100, 20);
                P2.add(B1);
                B2.setText("Panel4");
                B2.setBounds(120, 60, 100, 20);
                P2.add(B2);

            }

        });
        JF.setResizable(false);
        JF.setVisible(true);
    }

}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Md. Sakib Hossain
  • 237
  • 1
  • 3
  • 8

1 Answers1

4

You have multiple errors in your code:

  1. You're executing all your code in main method.

  2. You're not following the Oracle naming conventions where variable names should be in lowerCamelCase

  3. You're setting all your components bounds manually, while this might seem like the best and easiest way to make a complex GUI when starting to learn Swing, this isn't the correct way, instead make use of a Layout manager or combinations of them as well as Empty borders if needed for spacing, Swing has to work in multiple screen sizes, PLAFs and resolutions.

  4. I want to show a message on JFrame for a short time ..then I want to close the message and start my main JPanel.

    It seems like what you want to do is use a Dialog with an auto close after some time has passed

  5. From the above point, you don't want to use a Thread but a Swing Timer.

    Thread.sleep(...) will make your program to "freeze" until it finishes, while a Timer will execute in its own thread.

  6. You're not placing your program on the Event Dispatch Thread (EDT)

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89