【问题标题】:Unable to vertically align horizontal JSeparator in BorderLayout无法在 BorderLayout 中垂直对齐水平 JSeparator
【发布时间】:2014-07-07 01:10:45
【问题描述】:

使用 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 因为它很简单。

【问题讨论】:

  • “必须”使用BorderLayout 完成吗?
  • 不,只要看起来一样。一开始我虽然想使用GridBagLayout,但是对于这样一个简单的布局我选择了BorderLayout来保持简单。

标签: java swing border-layout jseparator


【解决方案1】:

这与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);
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 2012-10-10
    • 2017-07-01
    • 1970-01-01
    • 2013-11-25
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    相关资源
    最近更新 更多