Reputation: 5041
My Code:
public MyConstructor() {
view = new JPanel(new GridLayout(3, 1));
header = new JPanel(new GridLayout(2, 1));//2 ROWS 1 COLUMN
foot = new JLabel("Copyright...");
content = new JPanel();
info = new JLabel("");
logo = new JLabel() {
BufferedImage img;
@Override
public void paint(Graphics g) {
try {
img = ImageIO.read(new File("logo.jpg"));
} catch (IOException e) {
}
g.drawImage(img, 0, 0, null);
}
};
window.add(view);
header.add(logo);
header.add(info);
view.add(header);
view.add(content);
view.add(foot);
window.setLocation(width / 2, 100);
window.setSize(width, height);
window.setPreferredSize(new Dimension(width, height));
content.setSize(window.getWidth(), height-70);
content.setPreferredSize(new Dimension(window.getWidth(), height-70));
}
"window" is the frame...the class is not extending JFrame My class is going to be a super class to others, the subclasses inherit the public content JPanel. In my super class i'm trying to set the width and height of the 3 sections of my GridLayout, the logo and info components add up to 70 for their height...I've set the other components (view,header,info,logo) private so that subclasses can't access them...
When the application runs a window is shown for the login, this displays and resizes properly. once logged in an instance of one of the subclasses is created, the login window is then hidden site setVisible(false)
when the new window is shown however, the JFrame is the correct size but the header,content and footer are not the currect sizes. I've tried setting the size and preferred size each of the components but still not working...I've also tried calling repaint and validate/revalidate
Any ideas?
Upvotes: 0
Views: 5963
Reputation: 23639
It sounds like your design could be improved. Why do these variables need to be attributes of a superclass? Why not just have a method that constructs the panels that you need and a constructor that adds them so that you get fresh panels for each instance?
In fact, why not just create the header and footer classes and reuse them instead of having to subclass a frame just to get the same header and footer?
Upvotes: 1
Reputation: 324197
Why are you overriding the label to paint an image?
Upvotes: 1
Reputation: 77089
Try setting preferred sizes before adding components to containers, and adding components "bottom-up". Otherwise, try calling pack()
, revalidate()
, repaint()
etc.to adjust things.
Read up on Layout Managers, too, you're not using them correctly.
Also, Swing sucks. Try Netbeans, it makes it a little bit more bearable. It helps a lot when you need to manually place and resize things, too.
Upvotes: 0