Re: panel size vs. frame size



atilagunes@xxxxxxxxx wrote:
Hi,

i'm writing a gui that has a main panel and includes 3 subpanels.. and
has a Menu Bar..

i'm setting size of Frame with
GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds()
function.

My screen Size is 1280 * 800 and because of Start button, it takes 1280
* 770.. its ok so far.
you know the JFrame has a title bar. and suppose that i add a MenuBar
to that JFrame. how can i measure the best size of main Panel and its
bounds. i'm using null layout in main Panel and force the subpanels to
main Panel but the X and Y values are not true. i think it shifts to
down because of title bar and menu bar. i'm trying several boundaries
for subpanels but i could not fit them onto main panel..

i hope this message can be understood..


Use layouts, they make this sort of thing easy.


import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class TriBox {
public static void main(String[] args) {
JFrame frame = new JFrame();

JPanel p1 = new JPanel();
p1.add(new JLabel("Panel 1: height fixed at 200 pixels"));
int width = (int)p1.getPreferredSize().getWidth();
p1.setPreferredSize(new Dimension(width ,200));
p1.setBorder(new TitledBorder("1"));

JPanel p2 = new JPanel();
p2.add(new JLabel("Panel 2: height fixed at 100 pixels"));
width = (int)p2.getPreferredSize().getWidth();
p2.setPreferredSize(new Dimension(width ,100));
p2.setBorder(new TitledBorder("2"));

JPanel p3 = new JPanel();
p3.add(new JLabel("Panel 3: variable height"));
p3.setBorder(new TitledBorder("3"));

JPanel fixedPanel = new JPanel();
fixedPanel.setLayout(
new BoxLayout(fixedPanel, BoxLayout.PAGE_AXIS));
fixedPanel.add(p1);
fixedPanel.add(p2);

frame.setLayout(new BorderLayout());
frame.add(fixedPanel, BorderLayout.NORTH);
frame.add(p3, BorderLayout.CENTER);

frame.pack();
frame.setVisible(true);

}
}
.


Quantcast