gpt4 book ai didi

java - 我的 Java Swing JPanel 居中功能不起作用。为什么?

转载 作者:行者123 更新时间:2023-12-01 21:15:43 25 4
gpt4 key购买 nike

中间的“JPanel”,我想将对齐操作移动到单独的函数中,但不知何故不起作用。各项工作均在本地顺利开展。

desCont = new DesignController();

JPanel centerBox = desCont.createCenterPanel();
this.add(centerBox);

DesignController类来源:

public class DesignController {
(...)
public JPanel createCenterPanel(){
JPanel centerPanel = new JPanel();
JPanel hbox = new JPanel();
JPanel vbox = new JPanel();

centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS ));
centerPanel.add(Box.createHorizontalGlue());
centerPanel.add(hbox);
centerPanel.add(Box.createHorizontalGlue());

hbox.setLayout(new BoxLayout(hbox, BoxLayout.Y_AXIS ));
hbox.add(Box.createVerticalGlue());
hbox.add(vbox);
hbox.add(Box.createVerticalGlue());

return centerPanel;
}
}

更新:

示例文件中重建问题所需的元素:

public class CenterTest {
private JFrame frame;
private static CardLayout cardLayout = new CardLayout();
private JPanel headPanel;
private static JPanel contentPanel;
private JPanel footerPanel;
private static JPanel mainPanel;

private AppConstants appVars;

-->default main method (generated by IDE)
-->default constructor (generated by IDE)

/**
* Initialize the contents of the frame.
*/
private void initialize() {
appVars = new AppConstants();

frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setBounds(100, 100, appVars.initWidth, appVars.initHeight);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainPanel = new JPanel();
frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
mainPanel.setLayout(new BorderLayout(0, 0));

headPanel = new JPanel();
headPanel.setBackground(Color.LIGHT_GRAY);
headPanel.setPreferredSize(new Dimension( appVars.initHeaderPanelWidth, appVars.headerPanelHeight ));
mainPanel.add(headPanel, BorderLayout.NORTH);

contentPanel = new JPanel();
contentPanel.setPreferredSize(new Dimension(appVars.initContentWidth, appVars.initContentHeight));
mainPanel.add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(cardLayout);

footerPanel = new JPanel();
footerPanel.setBackground(Color.GREEN);
footerPanel.setPreferredSize(new Dimension(appVars.initFooterPanelWidth, appVars.footerPanelHeight));
mainPanel.add(footerPanel, BorderLayout.SOUTH);

addCard(new CenterPanel(), "center-panel");
}

public static void addCard(JPanel panel, String name){
contentPanel.add(panel, name);
cardLayout.show(contentPanel, name);
}

}

class CenterPanel extends JPanel {

private static final long serialVersionUID = 1L;

/**
* Include DesignController class.
*/
private DesignController desCont;

/**
* Include global variables class.
*/
private AppConstants appVars;

public CenterPanel(){
appVars = new AppConstants();
desCont = new DesignController();

/* ------------ THIS SECTION IS NOT CORRECTLY WORKING ----------------- */
JPanel hBox = desCont.createCenterPanel();
hBox.setBackground(Color.YELLOW);
this.add(hBox);
/* ------------ THIS SECTION IS NOT CORRECTLY WORKING ----------------- */

/* ------------ THIS SECTION IS CORRECTLY WORKING ----------------- */

/*JPanel hbox = new JPanel();
JPanel vbox = new JPanel();
vbox.setBackground(new Color(0, 204, 204));
vbox.setPreferredSize(new Dimension(appVars.initContentWidth, appVars.initContentHeight));

setLayout(new BoxLayout(this, BoxLayout.X_AXIS ));
add(Box.createHorizontalGlue());
add(hbox);
add(Box.createHorizontalGlue());

hbox.setLayout(new BoxLayout(hbox, BoxLayout.Y_AXIS ));
hbox.add(Box.createVerticalGlue());
hbox.add(vbox);
hbox.add(Box.createVerticalGlue());*/

/* ------------ THIS SECTION IS CORRECTLY WORKING ----------------- */
}
}



class DesignController {
private AppConstants appVars = new AppConstants();

public JPanel createCenterPanel(){
JPanel centerPanel = new JPanel();
JPanel hbox = new JPanel();
JPanel vbox = new JPanel();

vbox.setBackground(new Color(0, 204, 204));
vbox.setPreferredSize(new Dimension(appVars.initContentWidth, appVars.initContentHeight));

centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.X_AXIS ));
centerPanel.add(Box.createHorizontalGlue());
centerPanel.add(hbox);
centerPanel.add(Box.createHorizontalGlue());

hbox.setLayout(new BoxLayout(hbox, BoxLayout.Y_AXIS ));
hbox.add(Box.createVerticalGlue());
hbox.add(vbox);
hbox.add(Box.createVerticalGlue());

return centerPanel;
}

}

class AppConstants {
/**
* Define initial width of window <br/>
* <b>900</b>
*/
public int initWidth = 900;

/**
* Define initial height of window <br/>
* <b>600</b>
*/
public int initHeight = 600;


/**
* Define initial width of window header, same as window width <br/>
* <b>900</b>
*/
public int initHeaderPanelWidth = initWidth;

/**
* Define height of windows header panel <br/>
* <b>80</b>
*/
public int headerPanelHeight = 80;



/**
* Define initial width of window footer, same as window width <br/>
* <b>900</b>
*/
public int initFooterPanelWidth = initWidth;

/**
* Define height of windows footer panel <br/>
* <b>50</b>
*/
public int footerPanelHeight = 50;



/**
* Define initial width of content panel, same as window width <br/>
* <b>900</b>
*/
public int initContentWidth = 900;

/**
* Define height of content panel <br/>
* <b>window initial height - (header panel height + footer panel height)</b>
*/
public int initContentHeight = initHeight - (headerPanelHeight + footerPanelHeight);
}

最佳答案

你的做法是错误的。您无法归还“中心面板”。

为了使面板居中,您需要这样的结构:

HorizontalPanel
VerticalPanel
CenterPanel

垂直/水平面板是包装器,您需要将水平面板添加到框架中,因为它是组件的父面板。

如果将“CenterPanel”添加到框架中,那么您将丢失垂直/水平包装面板,因为 Swing 组件只能有一个父组件。因此该框架成为“CenterPanel”的新父级。

所以你的方法的设计需要类似于:

public static Component centerPanel(Component panelToBeCentered)
{
JPanel vertical = ...
vertical.add( panelToBeCentered );

JPanel horizontal = ...
horizontal.add( vertical );

return horizontal;
}

要使用代码,您需要执行以下操作:

 JPanel center = new JPanel();
center.add(...);

frame.add( DesignController.centerPanel( center ) );

请注意,使用 BoxLayout 时,您可以使用方便的静态方法:

Box vertical = Box.createVerticalBox();
Box horizontal Box.createHorizontalBox();

创建一个容器来容纳您的组件。

更简单的是,您不需要使用嵌套框。您可以只使用 GridBagLayout。 GridBagLayout 的默认约束将导致组件居中:

JPanel center = new JPanel();
JPanel wrapper = new JPanel( new GridBagLayout() );
wrapper.add(center, new GridBagConstraints());
frame.add( wrapper );

关于java - 我的 Java Swing JPanel 居中功能不起作用。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40196004/

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