【问题标题】:Aligning JButton in BoxLayout在 BoxLayout 中对齐 JButton
【发布时间】:2014-04-30 11:15:57
【问题描述】:

当前 BoxLayout 设置 [按钮 1、按钮 2、按钮 3 ||||||||||||||||||||||||||||||||| |||] |表示空白。

如何让它出现 [Button 1, Button 2, ||||||||||||||||||||||按钮3] 这些按钮是 JButton,boxlayout 位于 North 位置的 BorderLayout 内。

private JPanel makeButtonPanel(ObservableMazeGame observableMazeGame) {
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    ButtonPanel buttonPanelMaze = new ButtonPanel(observableMazeGame);
    buttonPanel.add(buttonPanelMaze.newGameButton());
    buttonPanel.add(buttonPanelMaze.helpButton());
    buttonPanel.add(buttonPanelMaze.aboutButton());
    return buttonPanel;
}

这是从

添加的
    ObservableMazeGame observableMazeGame = new ObservableMazeGame();
    JFrame baseFrame = new JFrame();
    baseFrame.setTitle("Maze Game");
    baseFrame.setLayout(new BorderLayout());
    baseFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    baseFrame.add(makeButtonPanel(observableMazeGame), BorderLayout.NORTH);
    baseFrame.pack();
    baseFrame.setVisible(true);

我只希望第三个按钮向右对齐,即使拉伸,它也会跟随拉伸。我试过 setAlignment 但它不起作用。

【问题讨论】:

    标签: java swing layout boxlayout


    【解决方案1】:

    在 button2 和 button3 之间使用水平胶水:

    buttonPanel.add(Box.createHorizontalGlue());
    

    更多详情请见How to Use BoxLayout

    【讨论】:

    • 像魅力一样工作!谢谢!这基本上在按钮之间创建了一个不可见的组件?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 2016-07-02
    相关资源
    最近更新 更多