gpt4 book ai didi

java - 在 GroupLayout 中设置 JPanel 的大小

转载 作者:行者123 更新时间:2023-11-30 07:14:57 24 4
gpt4 key购买 nike

我有一个 JFrame 并将布局设置为 GroupLayout。

我正在添加两个 Jpanel,即 workingPanel(red) 和 backgroundPanel(green)。

我希望绿色面板的高度小于 50 或 60。我已将 backgroundPanel 的大小设置为 50,但在将其添加到 Jframe 时,backgroundPanel 的高度与 workingPanel 相同。 enter image description here

代码是`import javax.swing.;导入 java.awt.;

public class Home extends JFrame{

JButton b1;
JPanel workingPanel,backgroundPanel;

public Home(){

new JFrame("Restaurant Billing");
b1=new JButton("Hello");
workingPanel=new JPanel();
backgroundPanel=new JPanel();
int maximumWidth=getContentPane().getWidth();
backgroundPanel.setSize(maximumWidth,60);
workingPanel.setBackground(Color.red); //workingpanel backgroundcolor is red
backgroundPanel.setBackground(Color.green);//backgroundPanle backcolor is green
//creating grouplayout and setting to mainframe
GroupLayout layout=new GroupLayout(getContentPane());
getContentPane().setLayout(layout);

layout.setHorizontalGroup(
layout.createParallelGroup()
.addComponent(backgroundPanel)
.addComponent(workingPanel)
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(backgroundPanel)
.addComponent(workingPanel)

);


this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void launchFrame(){

this.setVisible(true);
}
}

请帮助我。

最佳答案

尝试使用 prefferedSize 属性。

backgroundPanel.setPrefferedSize(maximumWidth,60);

关于java - 在 GroupLayout 中设置 JPanel 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18209096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com