简体   繁体   English

将GridBagLayout与WeightY函数一起使用可在调整窗口大小时拆分行,而不是调整大小

[英]Using GridBagLayout With WeightY Function Splits Rows Instead of Resizing When Window is Resized

I am wondering (as said in the title) if there is any way to resize rows when resizing the window. 我想知道(如标题中所述)在调整窗口大小时是否有任何方法可以调整行的大小。 What happens to me when I use weighty function in GridBagLayout is that the rows split and there becomes space in between them. 当我在GridBagLayout中使用weighty函数时,发生的事情是行被拆分,行之间也留有空间。 Is there any way to resize the rows instead of the spacing? 有什么方法可以调整行的大小而不是间距?

(Sorry if this is a bit unclear; here is a picture of what I mean ) (抱歉,这还不清楚;这是我的意思的图片)

Here is the important part of my code when I tried coding GridBagLayout: 这是我尝试编码GridBagLayout时代码的重要部分:

JTextField display = new JTextField();//This is the top Text Field as seen in the picture
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.VERTICAL;
c.weightx = 1;
c.weighty = 1;

c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
panel.add(one, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
panel.add(two, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 0;
panel.add(three, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 0;
panel.add(plus, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
panel.add(four, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
panel.add(five, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
panel.add(six, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 1;
panel.add(minus, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
panel.add(seven, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
panel.add(eight, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 2;
panel.add(nine, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 2;
panel.add(multiply, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
panel.add(zero, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 2;
panel.add(equals, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 3;
c.gridwidth = 1;
panel.add(divide, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 4;
panel.add(decimal, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 4;
panel.add(negative, c);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 4;
panel.add(modulus, c);

BorderLayout bl = new BorderLayout();
frame.add(display, bl.NORTH);//This is the top display,not very important.
frame.add(panel, bl.CENTER);

Looking to the image you attached, it seems you're using Java Swing. 查看附加的图像,似乎您正在使用Java Swing。 A little deep dive into the code would be useful(if you can paste the code here). 深入研究代码将很有用(如果您可以在此处粘贴代码)。

I figured it out! 我想到了! My mistake was that I typed c.fill = GridBagConstraints.HORIZONTAL so that it only resized horizontally and not vertically. 我的错误是我键入c.fill = GridBagConstraints.HORIZONTAL以便它只能水平调整大小,而不能垂直调整大小。 What i figured out (it is very obvious but I couldn't think of it when I posted the question) is that I use c.fill = GridBadConstraints.BOTH so that it resizes both vertically and horizontally. 我发现(很明显,但是当我发布问题时我没想到)是我使用c.fill = GridBadConstraints.BOTH以便它在垂直和水平方向上都可以调整大小。

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

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