简体   繁体   中英

Java How to get JComponent to resize based on frame width

So my calculator program looks good unless I resize it. Then things get all out of whack. Right now I'm using a GridBagLayout to organize my buttons (thought it was best to use GridBag for this situation given a number of buttons). I set the preferredSize to what I would like the size of the buttons to be upon startup. How can I get the buttons to change size upon window resizing?

在此处输入图片说明

That's what happens after I resize the app smaller. When I make it bigger, everything just stays the same and I get a grey emptiness on either side to make up space, instead of enlarging the buttons.

For my buttons dimensions i'm using new Dimension(80, 55) Then setting the preferredSize to that dimension. For the memory buttons and jlabels I compute some math to make it as wide as the total buttons across.

Please Note I'm still learning java, as well as the swing library. END NOTE

UPDATE I tried setting the minimum and maximum size when I set the preferred size and still no go END UPDATE

UPDATE 2 I tried setting the size of my main panel to match the size of the app upon start and that didn't work END UPDATE

EDIT 2 My hierarchy is like this:

  FRAME
    MAINPANEL  - Contains all three panels
      TOPPANEL - Contains the display
      MIDDLEPANEL - Contains memory buttons
      BOTTOMPANEL - Contains calculator buttons

END EDIT 2

I recommend using TableLayout from Oracle (you can download it there), because it is more comfortable. Sure it is possible with the GridBagLayout too, but especially when you're new, the TableLayout is much easier to understand and to configure.

Oracle TableLayout

double size[][] =
        {{0.25, 0.25, 0.25, 0.25},  //Horizontal
         {50, TableLayout.FILL, 40, 40, 40}};  //Vertical

frame.setLayout (new TableLayout(size));

It's basically a Grid which is separated into rows and columns. So, each of the cells have an id, for example "0, 0". The height and width is adjustable with pixels, percentage, and Layout.FILL, which uses the available space. if you are using multiple Layout.FILL, it will separate the available space between them.

To add Buttons or other elements, you will need to know the cell coordinates, if it's bigger than one cell, you can add an end cell to, so you can use the whole place available.

// Add buttons
    frame.add (button[0], "1, 1, 4, 1"); // Top
    frame.add (button[1], "1, 4, 4, 4"); // Bottom
    frame.add (button[2], "1, 3      "); // Left
    frame.add (button[3], "4, 3      "); // Right
    frame.add (button[4], "3, 3, c, c"); // Center
    frame.add (button[5], "3, 3, 3, 4"); // Overlap

Here some useful links:

http://www.oracle.com/technetwork/java/tablelayout-141489.html http://www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/Simple.html http://www.clearthought.info/sun/products/jfc/tsc/articles/tablelayout/Preferred.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM