简体   繁体   English

Java如何获取JComponent以根据帧宽度调整大小

[英]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). 现在,我正在使用GridBagLayout来组织我的按钮(在这种情况下,考虑到许多按钮,最好使用GridBag)。 I set the preferredSize to what I would like the size of the buttons to be upon startup. 我将preferredSize设置为我希望启动时按钮的大小。 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. 对于我的按钮尺寸,我正在使用new Dimension(80, 55)然后将preferredSize设置为该尺寸。 For the memory buttons and jlabels I compute some math to make it as wide as the total buttons across. 对于内存按钮和jlabel,我计算了一些数学运算使其与整个按钮一样宽。

Please Note I'm still learning java, as well as the swing library. 请注意,我仍在学习Java和swing库。 END NOTE 结束注

UPDATE I tried setting the minimum and maximum size when I set the preferred size and still no go END UPDATE UPDATE我在设置首选大小时仍然尝试设置最小和最大大小,但仍然没有执行。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 UPDATE 2我尝试将主面板的大小设置为与启动时应用程序的大小相匹配,但这种方法不起作用END UPDATE

EDIT 2 My hierarchy is like this: 编辑2我的层次结构是这样的:

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

END EDIT 2 结束编辑2

I recommend using TableLayout from Oracle (you can download it there), because it is more comfortable. 我建议使用来自Oracle的TableLayout(可以在此处下载),因为它更舒适。 Sure it is possible with the GridBagLayout too, but especially when you're new, the TableLayout is much easier to understand and to configure. 当然也可以使用GridBagLayout,但是尤其是当您是新手时,TableLayout更加易于理解和配置。

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". 因此,每个单元格都有一个id,例如“ 0,0”。 The height and width is adjustable with pixels, percentage, and Layout.FILL, which uses the available space. 高度和宽度可以使用像素,百分比和Layout.FILL(使用可用空间)进行调整。 if you are using multiple Layout.FILL, it will separate the available space between them. 如果您使用多个Layout.FILL,它将在它们之间分隔可用空间。

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. 要添加Button或其他元素,您需要知道单元格坐标,如果它大于一个单元格,则可以向其中添加一个结束单元格,以便可以使用整个可用位置。

// 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 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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