Java GUI panel size and resizing issues...

From: TR (t_at_r.t)
Date: 10/09/04


Date: Sat, 9 Oct 2004 11:42:08 -0400

Hi All:

I have a simple test GUI that has 2 panels (a left side and a right side),
and each panel is displaying an image. The left panel has a hardcoded size
of (600,400) using the Dimension() function. Likewise, the right panel has a
hardcoded size of 300, 200.

Problem 1: I would think that when the GUI is launched, the image in the
right panel would be twice as small as the image on the left. However, they
are the same size. Does anyone know why this might be? Further, is there a
way to set panel and image widths to be a percentage of the width of the
parent panel? For example, the left side set to 67% of the total parent
panel width, and the right side sett to 33% of the parent panel width.

Problem 2: When the window is resized in the horizontal direction, the
panels and images also resize. However, when the window is resized in the
veritcal direction, the panels and images do not resize, and eventually
become covered up as the window becomes very small. Is there any way to get
the panels and images to resize in both directions as the window is resized?

Thanks in advance for any help. Code follows:

-------------------------------

import java.awt.*;
import javax.swing.*;

public class JPanels extends JFrame {
  public static void main(String[] args) {
    new JPanels();
  }

  public JPanels() {
    super("Test Window");
    Container content = getContentPane();
    content.setBackground(Color.lightGray);

    JPanel bothsides = new JPanel(new GridLayout(1, 2));

    JPanel leftside = new JPanel();
    leftside.setPreferredSize(new Dimension(600, 400));
    ImageIcon icon1 = new ImageIcon ("sun.jpg");
    leftside.add(new JLabel(icon1));

    JPanel rightside = new JPanel();
    rightside.setPreferredSize(new Dimension(300, 200));
    ImageIcon icon2 = new ImageIcon ("venus.jpg");
    rightside.add(new JLabel(icon2));

    bothsides.add(leftside);
    bothsides.add(rightside);

    content.add(bothsides);

    pack();
    setVisible(true);
  }
}



Relevant Pages

  • Re: How to soften panel
    ... panel on the top of the screen, or reach the bottom of the box to resize. ... I can't remember about Breezy, but in Dapper, if you hold the "Alt" key, ... Also, you can resize a window by holding the "Alt" key, putting the ...
    (Ubuntu)
  • Re: Flickering of gradient rectangle
    ... But is still flickers when I resize ... public partial class PanelEx: Panel ... flags and use this control to draw your gradient... ... The problem is that when I resize the window (in my test app the panel ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Resizing a JFrame at runtime
    ... I have a JFrame, that contains a JSplitPane. ... the top panel get's resized and covers the ... So now I do the same with the lower panel, i.e., I resize ...
    (comp.lang.java.gui)
  • Resizing a JFrame at runtime
    ... I have a JFrame, that contains a JSplitPane. ... the top panel get's resized and covers the ... So now I do the same with the lower panel, i.e., I resize ...
    (comp.lang.java.programmer)
  • Re: Center a panel on a screen
    ... > I am using vb.net and need to center a panel on the screen. ... > resize the screen I would like to remain centered. ... If it is MDI it can iterate through its child forms and call the child ... to use .CenterToParent when it finds the form it wants to center. ...
    (comp.programming)