1

使用 BorderLayout 我放置了两个不同的 JButton,一个在左侧(西),一个在右侧(东),一个水平的 JSeparator 在中心。我想要做的是将分隔符与中心对齐,而不是像现在这样对齐到顶部。我已经尝试在分隔符上使用以下方法

setAlignmentY(CENTER_ALIGNMENT);

但它绝对没有效果。我错过了什么?如果不可能,有没有其他方法可以在不使用外部库的情况下做到这一点?

这就是我得到的:

问题的图像

这就是我想要实现的目标:

解决方案的图像

这是我正在使用的示例代码(为了清楚起见,添加了顶部和底部的 JPanel):

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

public class SeparatorTest extends JFrame{

    JButton btn1 = new JButton("button1");
    JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
    JButton btn2 = new JButton("button2");

    public SeparatorTest() {
        getContentPane().add(BorderLayout.NORTH, new JPanel());
        getContentPane().add(BorderLayout.WEST, btn1);
        getContentPane().add(BorderLayout.CENTER, sep);
        getContentPane().add(BorderLayout.EAST, btn2);
        getContentPane().add(BorderLayout.SOUTH, new JPanel());

        setSize(300, 85);
    }

    public static void main(String[] args){
        new SeparatorTest().setVisible(true);
    }
}

编辑 1:我不介意布局,只要它看起来相同,我在这里使用 BorderLayout 因为它的简单性。

4

1 回答 1

3

这与JSeparatorUI 委托如何决定如何绘制组件有关,根据您的测试,组件似乎总是希望y0.

相反,您可能需要将其包装JSeparator在另一个面板中,该面板可以使用满足您需求的不同布局管理器,GridBagLayout例如(当然您可以GridBagLayout开始使用,但我的目标是尽可能进行最小的更改;))

纽扣

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class SeparatorTest extends JFrame {

    JButton btn1 = new JButton("button1");
    JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
    JButton btn2 = new JButton("button2");

    public SeparatorTest() {

        JPanel pane = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.weightx = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        pane.add(sep, gbc);

        getContentPane().add(BorderLayout.NORTH, new JPanel());
        getContentPane().add(BorderLayout.WEST, btn1);
        getContentPane().add(BorderLayout.CENTER, pane);
        getContentPane().add(BorderLayout.EAST, btn2);
        getContentPane().add(BorderLayout.SOUTH, new JPanel());

        setSize(300, 85);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                SeparatorTest frame = new SeparatorTest();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
于 2014-07-07T01:32:16.037 回答