Reputation: 181
public class Main extends JFrame{
JLabel lb1,lb2,lb3,lb4;
Button b,b1;
public Main()
{
setLayout(null);
Container c=getContentPane();
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setBackground(Color.red);
setVisible(true);
b1=new Button("CONTINUE");
b1.setFont(new Font("Algerian", 1, 20));
b1.setForeground(Color.black);
b1.setBounds(550, 480, 200,40);
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{ setVisible(false);
try {
new Frame();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
c.add(b1);
setSize(1290,900);
}
public static void main(String...s) throws InterruptedException
{
new Main();
}
}
this is the main class..and this the frame class that it calls..
public class Frame extends JFrame{
Frame() throws InterruptedException
{
JFrame f=new JFrame();
f.setVisible(true);
f.setLayout(null);
f.setBounds(0,0,200,200);
Container p=f.getContentPane();
p.setBackground(Color.GREEN);
f.setResizable(false);
Thread.sleep(5000);
setVisible(false);
new Frame1();
}
}
the main class calls the frame class..that needs to hold for a certain time and then move to another frame.. but what happens is that the main class calls it, the frame appears , but there is no content in it..nothing is displayed.then it moves to frame1()//
but if i run like
new Frame();
then it holds ,shows content , and then moves..
so why doesnt it work when Frame() is called by Main() ?
even this code doesnt' work ..instead of Thread.sleep()...this way i am not blocking any even thread..am i ?
for(double i=0;i<30;i++)
{
for(double j=0;j<10000;j++)
{
}
}
new Frame1();
Upvotes: 0
Views: 845
Reputation: 15408
Swing does it's GUI rendering task inside a single thread known as Event Dispatch Thread(EDT)
. Any thing you do which might take a while will block the EDT and your swing application will be frozen. You are doing such thing by one of your statement:
Thread.sleep(5000);
This is always suggested not to work with NullLayout
. Learn proper layout mangers that Swing developer has created for us with their hard work. Let us give some value to their effort.
Put your GUI rendering task including frame
's setVisible(true)
inside EDT by using SwingUtilities.invokeLater
function. For example:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MyWindow().setVisible(true);
}
});
You are working with Multiple Frame. It is also prohibited by the Swing family of Stack Overflow. There are lots of work around shown using Card Layout
by the family which you can easily adopt for avoiding use of multiple frame unnecessarily. Some example are given in the answer of Andrew Thompson
with given link.
Upvotes: 3