我想找出我将如何在一个JPanel上拥有两个JPanell,所有这些都使用GridBagLayout。
基本上,您拥有最顶级的JPanel,然后有2个JPanell,要求如下:
--------------------------------------
| | |
| | |
| | |
| 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
不更改大小,则可以将GridBagConstraint.NONE传递给函数,而不是像我所做的那样将GridBagConstraint.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);
}
}