我有一个框架(600X500),一个JPanel(50X100)和另一个JPanel(200X150),并且
我试图获得以下结果:
我的代码是:
public class BtnsPanel extends JPanel{
public BtnsPanel()
{
setSize(50,100);
setBackground(Color.RED);
}
}
public class DialogPanel extends JPanel{
public DialogPanel() {
setSize(150,150);
setBackground(Color.BLUE);
}
}
public class MainFrame extends JFrame{
public MainFrame()
{
setSize(600,500);
setLayout(new BorderLayout());
add(new BtnsPanel(), BorderLayout.CENTER);
add(new DialogPanel(), BorderLayout.CENTER);
}
public static void main(String[] args){
new MainFrame().setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
代码结果:
那不是预期的结果。
小智 5
您的图片未按比例绘制,但是如果您未尝试达到所提供尺寸的确切比例,建议您使用GridBagLayout而不是BorderLayout:
public class MainFrame extends JFrame {
private JPanel btnsPanel;
private JPanel dialogPanel;
public MainFrame() {
getContentPane().setBackground(Color.BLUE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setPreferredSize(new Dimension(600,500));
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.2, 0.1, 0.5, 0.2};
gridBagLayout.rowWeights = new double[]{0.3, 0.3, 0.15, 0.25};
getContentPane().setLayout(gridBagLayout);
btnsPanel = new JPanel();
btnsPanel.setBackground(Color.RED);
GridBagConstraints gbc_btnsPanel = new GridBagConstraints();
gbc_btnsPanel.insets = new Insets(0, 0, 5, 5);
gbc_btnsPanel.fill = GridBagConstraints.BOTH;
gbc_btnsPanel.gridx = 0;
gbc_btnsPanel.gridy = 0;
gbc_btnsPanel.gridheight = 2;
getContentPane().add(btnsPanel, gbc_btnsPanel);
dialogPanel = new JPanel();
dialogPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
dialogPanel.setBackground(Color.BLUE);
GridBagConstraints gbc_dialogPanel = new GridBagConstraints();
gbc_dialogPanel.insets = new Insets(0, 0, 0, 5);
gbc_dialogPanel.fill = GridBagConstraints.BOTH;
gbc_dialogPanel.gridx = 2;
gbc_dialogPanel.gridy = 1;
gbc_dialogPanel.gridheight = 2;
getContentPane().add(dialogPanel, gbc_dialogPanel);
pack();
}
}
Run Code Online (Sandbox Code Playgroud)
代码结果:

| 归档时间: |
|
| 查看次数: |
268 次 |
| 最近记录: |