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

setAlignmentY(CENTER_ALIGNMENT);


但这绝对没有效果。我想念什么?如果不可能,是否有其他方法可以不使用外部库呢?

这是我得到的:



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



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

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。

最佳答案

这与JSeparator UI委托决定如何绘制组件有关,根据您的测试,该组件似乎总是要绘制从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);
            }
        });
    }
}

07-26 09:27
查看更多