我如何使用 BoxLayout 来做到这一点?

Jak*_*ger 3 java swing layout-manager boxlayout

我已经完美地设置了菜单(中心框),但我不知道如何放置标签。目前发生的情况是标签位于菜单选项下方,并且菜单选项被推到右侧。

这是我想要发生的事情: 在此输入图像描述

这是正在发生的事情: 在此输入图像描述

目前我的盒子集中在:

this.setAlignmentX(Component.CENTER_ALIGNMENT);
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下方法对我的标签执行相同的操作:

this.setAlignmentX(Component.BOTTOM_ALIGNMENT);
this.setAlignmentY(Component.LEFT_ALIGNMENT);
Run Code Online (Sandbox Code Playgroud)

这什么也没做。

抱歉,这张图太糟糕了,我用 MS Paint 花了大约 20 秒就把它画出来了。

这是标签的重要部分

public Label(String text)
{

    this.setHorizontalTextPosition(JLabel.CENTER);
    this.setVerticalTextPosition(JLabel.CENTER);
    this.setHorizontalAlignment(0);
}
Run Code Online (Sandbox Code Playgroud)

这是我创建盒子布局的地方:

 pnlMain.setLayout(new BoxLayout(pnlMain, BoxLayout.Y_AXIS));
Run Code Online (Sandbox Code Playgroud)

编辑:这是我的 JFrame 扩展类中的主要函数。功能之上只是面板、按钮和标签的创建。

public Window()
{
    //Create the JFrame
    super("Tank Trouble");
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);

    //Changes the frame size to your screen size
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) (dimension.getWidth());
    int y = (int) (dimension.getHeight());
    setSize(x,y);
    setResizable(false);
    //GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this); //Makes the application go fullscreen

    getContentPane().add(pnlMaster);

    pnlMaster.add(pnlMenu, "Main Menu");
    pnlMaster.add(pnlOptions, "Options");
    pnlMaster.add(pnlGame, "Game");
    pnlMaster.add(pnlMenu2);

    switchTo("Main Menu");

    pnlOptions.setLayout(new BoxLayout(pnlOptions, BoxLayout.Y_AXIS));

    Box box = Box.createVerticalBox();
    box.add(Window.playS);
    box.add(Box.createVerticalStrut(20));
    box.add(Window.playM);
    box.add(Box.createVerticalStrut(20));
    box.add(Window.options);
    box.add(Box.createVerticalStrut(20));
    box.add(Window.language);
    box.add(Box.createVerticalStrut(20));
    box.add(Window.exit);
    box.add(Box.createVerticalStrut(20));

    pnlMenu.add(box);
    pnlMenu.add(new JPanel());
    pnlMenu.add(new JPanel());
    pnlMenu.add(new JPanel());
    pnlMenu.add(new JPanel());

    pnlMenu2.add(Window.lblVersion);

    System.out.println("Window class loaded");

}
Run Code Online (Sandbox Code Playgroud)

这是我的菜单类当前的样子(这是之前处理与按钮和标签有关的所有内容(除了它们的创建)的类。

package menu;

import main.Window;

public class Menu
{   
    public Menu()
    {
    Listener listener = new Listener();

    //Add ActionListeners
    Window.exit.addActionListener(listener);
    Window.playS.addActionListener(listener);
    Window.playM.addActionListener(listener);
    Window.options.addActionListener(listener);
    Window.language.addActionListener(listener);
    Window.btnBack.addActionListener(listener);

    System.out.println("Menu class loaded");
}
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*tha 5

您可以使用布局管理器的正确组合获得所需的结果。不要将自己限制于一种。利用组合/嵌套布局的强大功能。

在此输入图像描述

这是我使用的技术

  • ABoxLayout代表中心按钮
  • AGridLayout(1, 5)表示面板,由除左下按钮(独立存在)之外的所有内容组成
  • FlowLayoutLEADING对齐
  • 全部包含在JFrame默认值中BorderLayout

请参阅下面的程序

import java.awt.*;
import javax.swing.*;

public class BoxLayoutDemo {

    JButton button1 = new JButton("Button1");
    public BoxLayoutDemo() {
        JPanel pane = new JPanel(new GridLayout(1, 5));

        Box box = Box.createVerticalBox();
        box.add(new JButton("Button 1"));
        box.add(Box.createVerticalStrut(20));
        box.add(new JButton("Button 2"));
        box.add(Box.createVerticalStrut(20));
        box.add(new JButton("Button 3"));
        box.add(Box.createVerticalStrut(20));
        box.add(new JButton("Button 4"));
        box.add(Box.createVerticalStrut(20));
        box.add(new JButton("Button 5"));
        box.add(Box.createVerticalStrut(20));

        pane.add(new JPanel());
        pane.add(new JPanel());
        pane.add(box);
        pane.add(new JPanel());
        pane.add(new JPanel());

        JPanel pane2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        pane2.add(new JButton("ButtonButton"));

        JFrame frame = new JFrame("GridBag Box");
        frame.add(pane, BorderLayout.CENTER);
        frame.add(pane2, BorderLayout.SOUTH);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }


    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new BoxLayoutDemo();
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

免责声明:请原谅框架的标题。我首先考虑的是GridBagLayout与结合使用Box,但我这样做的方式要容易得多。既然已经注意到了,就懒得改标题了。


对于那些说我上面所做的有点黑客的人(通过添加空面板),也许是这样,您还可以将顶部面板和底部面板添加到具有 aBorderLayout和首选尺寸的包含面板中,它会给您类似的结果结果

    public BoxLayoutDemo() {
        JPanel pane = new JPanel();

        Box box = Box.createVerticalBox();
        box.add(new JButton("Button 1"));
        box.add(Box.createVerticalStrut(20));
        box.add(new JButton("Button 2"));
        box.add(Box.createVerticalStrut(20));
        box.add(new JButton("Button 3"));
        box.add(Box.createVerticalStrut(20));
        box.add(new JButton("Button 4"));
        box.add(Box.createVerticalStrut(20));
        box.add(new JButton("Button 5"));
        box.add(Box.createVerticalStrut(20));

        pane.add(box);

        JPanel pane2 = new JPanel(new FlowLayout(FlowLayout.LEADING));
        pane2.add(new JButton("ButtonButton"));

        JPanel panel = new JPanel(new BorderLayout()){
            public Dimension getPreferredSize() {
                return new Dimension(400, 260);
            }
        };
        panel.add(pane, BorderLayout.CENTER);
        panel.add(pane2, BorderLayout.SOUTH);

        JFrame frame = new JFrame("Slitting using different layouts");
        frame.add(panel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
Run Code Online (Sandbox Code Playgroud)