看看这个代码示例,看看这是否是你想要的:
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);
}
}