[英]Adjusting the height of a JPanel
I have a top panel with a flow layout that has a textfield and label.
我有一个顶部面板,面板的流布局具有文本字段和标签。 I need to add more text fields and labels below the first line.
我需要在第一行下方添加更多文本字段和标签。 I realized theres no real way of forcing a new line in flowlayout so I was just going to make a new panel and put it underneath the top one.
我意识到没有真正的方法可以强制在flowlayout中添加新行,所以我只是要制作一个新面板并将其放在顶部面板的下面。 The problem is theres a gap now because the top panels height is too tall.
问题是现在存在间隙,因为顶板的高度太高。 Is there a way to resize the panel so its width adjusts to screens automatically but I want to adjust the height of the new panel, so the gap is smaller.
有没有一种方法可以调整面板的大小,使其宽度自动适应屏幕,但我想调整新面板的高度,因此间隙较小。 The two textfields up top are in the top panel with a flowlayout, The 3 textfields below are in a new panel with a flowlayout, is there a way to make the 3 textfields go right under the 2 textfields up top?
顶部的两个文本字段在顶部具有Flowlayout,下面的3个文本字段在新面板中具有FlowLayout,是否有办法使3个文本字段在顶部的2个文本字段的正下方? Also, the whole frame is in a gridlayout.
而且,整个框架都处于网格布局中。
frame.setLayout(new GridLayout(0,1));
Static JPanel topPanel = new JPanel(new flowlayout);
Static JPanel searchPanel = new JPanel(new flowlayout);
Static JPanel statusPanel = new JPanel(new gridLayout(2,1));
Static JPanel categoryPanel = new JPanel(new GridLayout(0.2));
Static JPanel btnPanel = new JPanel(new GridLayout(0,3));
frame.pack();
topPanel.add(searchPanel);
JPanel container2 = new JPanel(new BorderLayout()):
container2.add(topPanel, BorderLayout.North);
frame.getContentPane().add(container2);
frame.getContentPane().add(statusPanel);
frame.getContentPane().add(catergoryPanel);
frame.getContentPane().add(btnPanel);
The JPanel
with the GridLayout
is the problem - It's probably added to BorderLayout.CENTER
of the container and therefore it stretches across the whole available space. 带有
GridLayout
的JPanel
是问题-可能已添加到容器的BorderLayout.CENTER
中,因此它遍及整个可用空间。 It also has equal cell sizes. 它还具有相等的像元大小。 Either wrap the grid into a
BorderLayout
and add it to BorderLayout.NORTH
or use a GridBagLayout
for the panels. 将网格包装到
BorderLayout
并将其添加到BorderLayout.NORTH
或者将GridBagLayout
用于面板。
Here's an example that demonstrates how the panels are laid out in your example and how the are laid out in other layouts. 这是一个示例,演示了如何在示例中布置面板以及如何在其他布局中布置面板。 I think this will make it clear why your
GridLayout
behaved in that way. 我认为这将使您清楚为什么
GridLayout
以这种方式表现。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Example {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Example();
}
});
}
public Example() {
// GridLayout
JPanel container1 = new JPanel(new GridLayout(2, 1));
container1.add(createExamplePanel());
container1.add(createExamplePanel());
// GridLayout inside BorderLayut (NORTH)
JPanel panel = new JPanel(new GridLayout(2, 1, 0, 5 /* margin */)); // You could also use other layouts here, e.g. a BoxLayout
panel.add(createExamplePanel());
panel.add(createExamplePanel());
JPanel container2 = new JPanel(new BorderLayout());
container2.add(panel, BorderLayout.NORTH);
// GridBagLayout
JPanel container3 = new JPanel(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.PAGE_START;
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(0, 0, 5 /* margin */, 0);
constraints.weightx = 1.0;
container3.add(createExamplePanel(), constraints);
constraints.gridy = 1;
constraints.weighty = 1.0;
constraints.insets = new Insets(0, 0, 0, 0);
container3.add(createExamplePanel(), constraints);
JPanel contentPane = new JPanel(new GridLayout(1, 3, 5, 5));
contentPane.add(createTitledPanel(container1, "GridLayout"));
contentPane.add(createTitledPanel(container2, "GridLayout inside BorderLayout (NORTH)"));
contentPane.add(createTitledPanel(container3, "GridBagLayout"));
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(contentPane);
frame.setSize(800, 600);
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createTitledPanel(JComponent content, String title) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(content);
panel.setBorder(BorderFactory.createTitledBorder(title));
return panel;
}
private JPanel createExamplePanel() {
JPanel panel = new JPanel();
panel.add(new JLabel("Panel"));
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
return panel;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.