我想知道如何在一个 JPanel 上使用两个 JPanel,并且都使用 GridBagLayout。
基本上,您拥有 Top Most JPanel,然后按照以下要求使用 2 个 JPanel:
--------------------------------------
| | |
| | |
| | |
| 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.BOTH
正如我所做的那样。由于不知道将成为其一部分的此 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);
}
}
关于java - GridBagLayout 2 JPanels - 比其他宽度小一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18332267/