Frame resizing based on screen size

Hi everybody,

I'm trying to make a JFrame and all its contents resize dynamically based on whatever screen size the user has. The text on my JButtons and JLabel won't resize consistently. Screen widths below 2000 and above 3000 pixels cause problems. I tried to scale the fonts based on how big they were when the window was 700 pixels wide.

Can anyone tell me what I'm doing wrong? Here's a self-contained example--

publicclass Test{

publicstaticvoid main( String args[] ){

try{

int height = Toolkit.getDefaultToolkit().getScreenSize().height;

int width = Toolkit.getDefaultToolkit().getScreenSize().width;

JFrame frame =new JFrame("Test" );

frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

frame.setSize( width / 2, height / 2 );

// Create main label

JLabel label =new JLabel("Test" );

label.setBackground(new Color( 179, 0, 0 ) );

label.setForeground(new Color( 235, 191, 16 ) );

label.setHorizontalAlignment( SwingConstants.CENTER );

label.setOpaque(true );

Font font =new Font("Old English Text MT", 0, 48 );

float fontSize = 48 * ( ( width / 2 ) / 700 );

font = font.deriveFont( fontSize );

label.setFont( font );

frame.add( label, BorderLayout.NORTH );

// Create button panel and buttons

JPanel panel =new JPanel(new GridLayout( 0, 2 ) );

for(int i = 0; i < 2; i++ ){

JButton button;

if( i == 0 ){

button =new JButton("Test Button 1" );

}else{

button =new JButton("Test 2" );

}

button.setBackground(new Color( 104, 17, 2 ) );

button.setForeground(new Color( 212, 212, 212 ) );

Font buttonFont =new Font("Wide Latin", 0, 25 );

float buttonFontSize = 25 * (float)( ( width / 4 ) / 350 );

buttonFont = buttonFont.deriveFont( buttonFontSize );

button.setFont( buttonFont );

panel.add( button );

}

panel.setName("Startup Panel" );

frame.add( panel );

frame.setLocationRelativeTo(null );

frame.setVisible(true );

}catch(Exception e){

e.printStackTrace();

System.exit(-1);

}

}

}

Thank you!

Jezzica85

[3878 byte] By [jezzica85a] at [2007-11-27 8:47:52]
# 1
float fontSize = 48 * ( ( width / 2 ) / 700 );System.out.println(fontSize);I get 0.0 on a 1024 x 768 resolution.
camickra at 2007-7-12 20:53:47 > top of Java-index,Desktop,Core GUI APIs...
# 2
I'm not sure what your post means, camickr. I've tested that, and when I give different widths to test for screen size, sometimes it resizes and sometimes it doesn't.Jezzica85
jezzica85a at 2007-7-12 20:53:47 > top of Java-index,Desktop,Core GUI APIs...
# 3

Well, I'm not sure what you example is supposed to show.

A BorderLayout always resizes the components to the width of the frame when added to the North. So setting the font will have no effect on the size of the component. However, I don't even see the label when I run the code because the font is of size zero which means the label is of height 0.

Same with the components added to the center. They will always fill the available width and height remaining in the frame. So, again setting the font will have no effect on the size of the component. Again, when I tested it, the font size was zero, so again no text was displayed.

I was just trying to tell you that in any case using a font size of 0, will surely not give you the results you are looking for, no matter what Layout Manager you are using.

camickra at 2007-7-12 20:53:47 > top of Java-index,Desktop,Core GUI APIs...
# 4

Oh, I see what you meant. That tells me that I wasn't clear with my question. It's not the resizing of the components themselves that's the problem, it's the text on the components. I want the text to be able to be seen in its entirety on the JLabels and the JButtons, no matter what size the components are (so basically the text would resize along with the components).

jezzica85a at 2007-7-12 20:53:47 > top of Java-index,Desktop,Core GUI APIs...