Java GridBagLayout 2 JPanel-一个比另一个宽度小
我想知道我将如何在一个JPanel上使用两个JPanel,所有这些都使用GridBagLayout 基本上,您拥有最顶级的JPanel,然后有2个JPanel,如下所示:Java GridBagLayout 2 JPanel-一个比另一个宽度小,java,swing,layout,jpanel,gridbaglayout,Java,Swing,Layout,Jpanel,Gridbaglayout,我想知道我将如何在一个JPanel上使用两个JPanel,所有这些都使用GridBagLayout 基本上,您拥有最顶级的JPanel,然后有2个JPanel,如下所示: -------------------------------------- | | | | | | | | | | JP
--------------------------------------
| | |
| | |
| | |
| JPanel 1 | JPanel |
| | 2 |
| | |
| | |
--------------------------------------
基本上我需要JPanel 1比JPanel 2大。但是JPanel 2也不能调整大小,JPanel 1应该只在主面板调整大小时调整大小
有什么想法吗?看看这个代码示例,看看这是否是您想要的:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GridBagExample {
private JPanel leftPanel;
private JPanel rightPanel;
private GridBagConstraints gbc;
private Random random;
public GridBagExample() {
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(5, 5, 5, 5);
random = new Random();
}
private void displayGUI() {
JFrame frame = new JFrame("Swing Worker Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = getPanel();
contentPane.setLayout(new GridBagLayout());
leftPanel = getPanel();
rightPanel = getPanel();
addComp(contentPane, leftPanel, 0, 0, 1, 1,
GridBagConstraints.BOTH, 0.7, 1.0);
addComp(contentPane, rightPanel, 1, 0, 1, 1,
GridBagConstraints.BOTH, 0.3, 1.0);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp, int gridx,
int gridy, int gridwidth, int gridheight,
int fill, double weightx, double weighty) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.fill = fill;
gbc.weightx = weightx;
gbc.weighty = weighty;
panel.add(comp, gbc);
}
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
return panel;
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new GridBagExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
虽然如果您希望右侧的JPanel
不改变大小,您可以将GridBagConstraints.NONE
传递给函数,而不是GridBagConstraints。这两个都是我做的。由于没有该JPanel
的实际内容(将成为其一部分),因此很难显示其真实大小
输出:
编辑:
更新我的代码,以便更好地解释我在说什么,尽管我也从评论中使用了user2699405(OP)的想法
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class GridBagExample {
private JPanel leftPanel;
private JPanel rightPanel;
private GridBagConstraints gbc;
private Random random;
public GridBagExample() {
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.insets = new Insets(5, 5, 5, 5);
random = new Random();
}
private void displayGUI() {
JFrame frame = new JFrame("Swing Worker Example");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel contentPane = getPanel();
contentPane.setLayout(new GridBagLayout());
leftPanel = getPanel();
rightPanel = new JPanel() {
@Override
public Dimension getPreferredSize() {
return (new Dimension(100, 100));
}
};
addComp(contentPane, leftPanel, 0, 0, 1, 1,
GridBagConstraints.BOTH, 1.0, 1.0);
addComp(contentPane, rightPanel, 1, 0, 1, 1,
GridBagConstraints.NONE, 0.0, 1.0);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void addComp(JPanel panel, JComponent comp, int gridx,
int gridy, int gridwidth, int gridheight,
int fill, double weightx, double weighty) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.fill = fill;
gbc.weightx = weightx;
gbc.weighty = weighty;
panel.add(comp, gbc);
}
private JPanel getPanel() {
JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
return panel;
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
new GridBagExample().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
谢谢,我也做了一点修改。如前所述,第二个面板不应重新调整大小,权重x为0.3仍使其可重新调整大小。将其更改为0,然后在第二个面板上设置最小和首选大小,使其显示第二个面板。现在,当您调整大小时,它只会使左侧面板变大,而右侧面板保持原样。如果您没有设置最小和首选大小,则第二个面板将不会显示。@user2699405:但是,我希望有问题的第二个JPanel
应该与第一个面板垂直对齐,否则您甚至不希望这种情况发生!!!虽然我更喜欢覆盖JComponent
的getPreferredSize()
,而不是在同一个组件上显式调用setPreferredSize()
,因为前者在任何情况下都有效,但后者可能在某些情况下不起作用:-)是的,根据发布的图片,这正是它应该看起来的样子。上面的蓝色面(第二个面板)永远不应该调整大小,这是必需的。@user2699405:我想这个更新的代码会有所帮助,尽管您也可以覆盖getMinimumSize()
,以返回与getPreferredSize()
:-)+1 Previously相同的值(或您自己的值集),但实际上不可能在两个调整大小坐标中的一个停止调整大小(模拟BorderLayout),听起来您最好使用BorderLayout
。把你的JPanel 1放在中心
,把JPanel 2放在东部
。试试看。JPanel 1不重新调整大小,但JPanel 2重新调整大小。。。下面的示例与我添加的注释完美结合。这根本不可能。如果JPanel 1位于中间,它将占用额外的垂直空间和水平空间,而东部的面板将只垂直调整大小,而不会水平调整大小。啊,是的,对不起,我刚刚想起,我将第一个面板设置为LINE_START,将第二个面板设置为居中,然后调整了第二个面板的大小