繁体   English   中英

BoxLayout中的JLabel和JButton中心

[英]JLabel and JButton Center in BoxLayout

我是Java编程的新手,无法解决我的Swing问题。 我找不到以JPanels(BoxLayout)为中心的JButton和JLabel。

这是一些照片:

此处将标签ar居中,按钮不 这里什么都不是中心

码:

        PlayerLabel.setHorizontalTextPosition(JLabel.CENTER);
        PlayerLabel.setVerticalTextPosition(JLabel.BOTTOM);
        PlayerLabel.setText("Player Level: " + CarMain.main[5]);

    AccessoriesLVL1Label.setHorizontalTextPosition(JLabel.CENTER);
    AccessoriesLVL1Label.setVerticalTextPosition(JLabel.BOTTOM);
    AccessoriesLVL1Label.setText("<html>Accessories LVL 1<br>" + "Count: " + Part.parts[1]);

JButton jbtnSellAccessoriesLv1 = new JButton("Sell");
jbtnSellAccessoriesLv1.addActionListener(this);

这就是我制作Jlabel的地方:

//升级面板

GridLayout UpgradesLayout = new GridLayout(3,3);
        JPanel UpgradesPanel = new JPanel();
        UpgradesPanel.setLayout(UpgradesLayout);

        JPanel UpgradesPanelSub = new JPanel();
        UpgradesPanelSub.setLayout(new BoxLayout(UpgradesPanelSub, BoxLayout.PAGE_AXIS));
        UpgradesPanelSub.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));

        JPanel UpgradesPanelSub2 = new JPanel();
        UpgradesPanelSub2.setLayout(new BoxLayout(UpgradesPanelSub2, BoxLayout.PAGE_AXIS));
        UpgradesPanelSub2.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));

        JPanel UpgradesPanelSub3 = new JPanel();
        UpgradesPanelSub3.setLayout(new BoxLayout(UpgradesPanelSub3, BoxLayout.PAGE_AXIS));
        UpgradesPanelSub3.setBorder(BorderFactory.createCompoundBorder(raisedbevel, loweredbevel));

        UpgradesPanelSub.add(Labels.PlayerLabel);
        UpgradesPanelSub.add(jbtnUpgradeLevel);
        UpgradesPanelSub2.add(Labels.GarageLabel);
        UpgradesPanelSub2.add(jbtnUpgradeGarageLevel);
        UpgradesPanelSub3.add(Labels.BoxesLVLLabel);
        UpgradesPanelSub3.add(jbtnUpgradeBoxesLevel);

        UpgradesPanel.add(new JLabel(""));
        UpgradesPanel.add(new JLabel(""));
        UpgradesPanel.add(new JLabel(""));
        UpgradesPanel.add(UpgradesPanelSub);
        UpgradesPanel.add(UpgradesPanelSub2);
        UpgradesPanel.add(UpgradesPanelSub3);
        UpgradesPanel.add(new JLabel(""));
        UpgradesPanel.add(new JLabel(""));
        UpgradesPanel.add(new JLabel(""));

至于布局问题:有一个关于如何使用盒子布局的教程(查找组件的对齐方式以使其居中):docs.oracle.com/javase/tutorial/uiswing/layout/box.html。

或者,您可以使用更易于使用IMO的外部布局管理器之一,例如MigLayout。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM