首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java中心不工作

Java中心不工作
EN

Stack Overflow用户
提问于 2017-07-04 18:06:09
回答 2查看 1.2K关注 0票数 0

我有一个JFrame,里面有JPanel。在JPanel内部有两个按钮。JPanel有一个BoxLayout。我需要按钮水平显示在窗口的中心。这是我的代码:

我只需创建两个按钮,将它们的对齐设置为中心(尝试了我所知道的所有方法),然后将它们水平地添加到面板中。

代码语言:javascript
复制
public class UserInterface extends JFrame  {
   public UserInterface()  {
                setup();
   }
   private void setup()  {
         ...
         panel=new UserInterfacePanel();
         add(panel);
   }
}

class UserInterfacePanel extends JPanel {
         private JToggleButton startButton;
         private JToggleButton stopButton;

         public UserInterfacePanel()  {
             setup();
         }

         private void setup()  {
            setLayout(new BoxLayout(this, BoxLayout.X_AXIS));

            setupButtons();
            setupButtonsActions();
            add(startButton);
            add(stopButton);
        }

        private void setupButtons()  {
            ...
            startButton.setHorizontalAlignment(JButton.CENTER);
            stopButton.setHorizontalAlignment(JButton.CENTER);
            startButton.setAlignmentX(CENTER_ALIGNMENT);
            stopButton.setAlignmentX(CENTER_ALIGNMENT);
        }
}

但它不起作用。为什么它没有,以及如何修复它?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-04 18:10:56

你在设置按钮的x对齐,但是你的盒子布局沿着x轴排列--没有意义。要么设置y对齐并使用x轴表示盒布局方向,要么设置x对齐并使用y轴表示盒布局方向。

例如,

代码语言:javascript
复制
private void setup() {
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));  // **** change ****

    setupButtons();
    // setupButtonsActions();
    add(startButton);
    add(stopButton);
}

private void setupButtons() {
    // ...
    startButton.setHorizontalAlignment(JButton.CENTER);
    stopButton.setHorizontalAlignment(JButton.CENTER);
    startButton.setAlignmentX(CENTER_ALIGNMENT);
    stopButton.setAlignmentX(CENTER_ALIGNMENT);
}

代码语言:javascript
复制
private void setup() {
    setLayout(new BoxLayout(this, BoxLayout.X_AXIS));  

    setupButtons();
    // setupButtonsActions();
    add(startButton);
    add(stopButton);
}

private void setupButtons() {
    // ...
    startButton.setHorizontalAlignment(JButton.CENTER);
    stopButton.setHorizontalAlignment(JButton.CENTER);
    startButton.setAlignmentY(CENTER_ALIGNMENT);  // **** change ****
    stopButton.setAlignmentY(CENTER_ALIGNMENT);  // **** change ****
}

若要将所有设置为中心,请使用不同的布局,如GridBagLayout:

代码语言:javascript
复制
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;    
import javax.swing.*;

@SuppressWarnings("serial")
public class UserInterfacePanel extends JPanel {
    private static final Insets INSETS = new Insets(3, 3, 3, 3);
    private static final int PREF_W = 800;
    private static final int PREF_H = 650;
    private JToggleButton startButton = new JToggleButton("Start");
    private JToggleButton stopButton = new JToggleButton("Long Texted Title");

    public UserInterfacePanel() {
        setup();
    }

    private void setup() {
        setLayout(new GridBagLayout());  // **** change

        add(startButton, createGbc(0, 0));
        add(stopButton, createGbc(1, 0));
    }


    private GridBagConstraints createGbc(int x, int y) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = x;
        gbc.gridy = y;
        gbc.insets = INSETS;
        return gbc;
    }

    @Override
    public Dimension getPreferredSize() {
        if (isPreferredSizeSet()) {
            return super.getPreferredSize();
        }
        return new Dimension(PREF_W, PREF_H);
    }


    private static void createAndShowGui() {
        UserInterfacePanel mainPanel = new UserInterfacePanel();

        JFrame frame = new JFrame("UserInterfacePanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}
票数 1
EN

Stack Overflow用户

发布于 2017-07-04 19:52:47

按钮仍然位于窗口的左侧。

如果您想让组件水平地集中在线上,则需要使用“胶水”。

代码语言:javascript
复制
add(Box.createHorizontalGlue());
add(startButton);
add(stopButton);
add(Box.createHorizontalGlue());
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44911930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档