在 GroupLayout 中设置 JPanel 的大小
Posted
技术标签:
【中文标题】在 GroupLayout 中设置 JPanel 的大小【英文标题】:Setting size of a JPanel in GroupLayout 【发布时间】:2013-08-15 01:39:57 【问题描述】:我有一个 JFrame,我将布局设置为 GroupLayout。
我正在添加两个 Jpanel,即 workingPanel(red) , backgroundPanel(green) 。
我希望绿色面板的高度更小,比如 50 或 60。我已将 backgroundPanel 的大小设置为 50,但将其添加到 Jframe 时,backgroundPanel 的高度与 workingPanel 相同。
代码是`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);
请帮帮我。
【问题讨论】:
【参考方案1】:尝试使用 prefferedSize 属性。
backgroundPanel.setPrefferedSize(maximumWidth,60);
【讨论】:
backgroundPanel.setPreferredSize(new Dimension(maximumWidth,10));但没有变化..问题仍然存在。【参考方案2】:为了调整大小,我查看了类属性,找不到任何东西,可以触发。最好的解决方案是这样的。
package groupleyaut;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class gfg
`enter code here`private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args)
EventQueue.invokeLater(new Runnable()
public void run()
try
gfg window = new gfg();
window.frame.setVisible(true);
catch (Exception e)
e.printStackTrace();
);
/**
* Create the application.
*/
public gfg()
initialize();
/**
* Initialize the contents of the frame.
*/
private void initialize()
frame = new JFrame();
frame.setBounds(100, 100, 807, 325);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
frame.getContentPane().add(panel, BorderLayout.CENTER);
JPanel panel_1 = new JPanel();
panel_1.setPreferredSize(new Dimension(10, 100));
JPanel panel_2 = new JPanel();
panel_2.setPreferredSize(new Dimension(200, 50));
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, panel_1.getPreferredSize().width, GroupLayout.PREFERRED_SIZE)
.addGap(64)
.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 492, GroupLayout.PREFERRED_SIZE)
.addContainerGap(166, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, panel_1.getPreferredSize().height, GroupLayout.PREFERRED_SIZE))
.addGap(66))
);
panel_2.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
panel_1.setPreferredSize(new Dimension(10, 200));
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, panel_1.getPreferredSize().width, GroupLayout.PREFERRED_SIZE)
.addGap(64)
.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 492, GroupLayout.PREFERRED_SIZE)
.addContainerGap(166, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE)
.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, panel_1.getPreferredSize().height, GroupLayout.PREFERRED_SIZE))
.addGap(66))
);
panel.setLayout(gl_panel);
);
btnNewButton.setBounds(147, 42, 89, 23);
panel_2.add(btnNewButton);
panel.setLayout(gl_panel);
【讨论】:
【参考方案3】:尝试使用“setMaximumSize()”方法。这将防止组件高度超出所需范围。如果希望保持组件宽度等于框架的宽度,则可以使用“getMaximumSize().width”来指定组件宽度。这将允许组件在框架被手动调整大小的情况下调整为全帧宽度。
backgroundPanel.setMaximumSize(new Dimension(backgroundPanel.getMaximumSize().width, 60));
完整代码:
import javax.swing.*;
import java.awt.*;
public class Home extends JFrame
JButton b1;
JPanel workingPanel,backgroundPanel;
public Home()
new JFrame("Restaurant Billing");
setPreferredSize(new Dimension(500,500));
setSize(new Dimension(500,500));
b1=new JButton("Hello");
workingPanel=new JPanel();
backgroundPanel=new JPanel();
int maximumWidth = backgroundPanel.getMaximumSize().width;
backgroundPanel.setMaximumSize(new Dimension(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);
public static void main(String args[])
Home home = new Home();
home.launchFrame();
【讨论】:
以上是关于在 GroupLayout 中设置 JPanel 的大小的主要内容,如果未能解决你的问题,请参考以下文章