我有一个JFrame,里面有JPanel。在JPanel内部有两个按钮。JPanel有一个BoxLayout。我需要按钮水平显示在窗口的中心。这是我的代码:
我只需创建两个按钮,将它们的对齐设置为中心(尝试了我所知道的所有方法),然后将它们水平地添加到面板中。
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);
}
}但它不起作用。为什么它没有,以及如何修复它?
发布于 2017-07-04 18:10:56
你在设置按钮的x对齐,但是你的盒子布局沿着x轴排列--没有意义。要么设置y对齐并使用x轴表示盒布局方向,要么设置x对齐并使用y轴表示盒布局方向。
例如,
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);
}或
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:
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());
}
}发布于 2017-07-04 19:52:47
按钮仍然位于窗口的左侧。
如果您想让组件水平地集中在线上,则需要使用“胶水”。
add(Box.createHorizontalGlue());
add(startButton);
add(stopButton);
add(Box.createHorizontalGlue());https://stackoverflow.com/questions/44911930
复制相似问题