PDA

View Full Version : JAVA: Multiple Frames?


Blixinator
05-04-2009, 09:20 AM
Yes, another thread in which I ask for help.

So I've been doing some stuff with applications and I'm trying to create multiple JFrames.


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.util.*;

public class Application3 extends JFrame implements ActionListener
{
static JButton button = new JButton("Button");

static JPanel p = new JPanel();

static JPanel p2 = new JPanel();

static JFrame test1 = new JFrame();

static JFrame test2 = new JFrame();

static JLabel label1 = new JLabel("TEST");



public Application3()
{
test1.getContentPane().add(p,BorderLayout.CENTER);
p.add(button);
test2.getContentPane().add(p2,BorderLayout.CENTER) ;
p2.add(label1);

}

public static void main(String[] args) throws AWTException, IOException
{
Application3 app = new Application3();
app.test1.show();
app.test1.setSize(500,500);
app.test1.setLocation(100,100);
app.test1.setTitle("Test1");

app.test2.show();
app.test2.setSize(500,500);
app.test2.setLocation(600,100);
app.test2.setTitle("Test2");

}

public void actionPerformed(ActionEvent ae)
{

}
}


Both frames open, but I don't see either the button in Test1 or the label in Test2, and I can't figure out what's up.

eedok
05-04-2009, 10:43 AM
do you see them when you resize the windows?

Just tested and I can see both, but I get a warning that the .show() method for frame has been deprecated

Blixinator
05-04-2009, 10:56 AM
Whoa, yeah. Why does that happen?
I don't want to have to resize the window every time. :(

eedok
05-04-2009, 11:05 AM
you need to .pack() your frames after adding the components

Blixinator
05-15-2009, 10:07 AM
Ok, it's been a while. My school removed TextPad from the computer I used.

Anyways, the code I have up there is supposed to put it in the middle. Using pack() then resize() keeps it on the left side.

What else could I try?