简体   繁体   English

无法在BorderLayout中垂直对齐水平JSeparator

[英]Unable to vertically align horizontal JSeparator in BorderLayout

Using BorderLayout I have put two different JButtons, one on the left (west) and one on the right (east), and a horizontal JSeparator in the center. 使用BorderLayout我放了两个不同的JButton,一个在左边(西边),一个在右边(东边),一个水平JSeparator在中间。 What I want to do is to y-align the separator to the center, instead of to the top as it is now. 我想要做的是将分隔符与中心对齐,而不是像现在一样对齐顶部。 I have already tried to use the following method on the separator 我已经尝试在分隔符上使用以下方法

setAlignmentY(CENTER_ALIGNMENT);

but it has absolutely no effect. 但它绝对没有效果。 What am I missing? 我错过了什么? If it is not possible, is there any other way to do that without using external libraries? 如果不可能,没有使用外部库有没有其他方法可以做到这一点?

This is what I get: 这就是我得到的:

问题的形象

and this is what I want to achieve: 这就是我想要实现的目标:

解决方案的图像

This is the sample code that I am using (JPanels on top and bottom were added just for clarity): 这是我正在使用的示例代码(为了清楚起见,添加了顶部和底部的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);
    }
}

EDIT 1: I don't mind the layout as long as it looks the same, I used BorderLayout here due to its simpleness. 编辑1:我不介意布局,只要它看起来一样,我在这里使用BorderLayout由于它的简单性。

This will have to do with how the JSeparator UI delegate decides how to paint the component, which, based on your tests, seems to want to always paint the separator starting at a y position of 0 . 这将有该怎么办JSeparator UI委托决定如何绘制组件,其中,根据你的测试,似乎要永远画开始在分隔y的位置0

Instead, you might need to wrap the JSeparator in another panel which can use a different layout manager which meets your needs, like GridBagLayout for example (of course you could just use GridBagLayout to start with, but I'm aiming for the smallest change possible ;)) 相反,您可能需要将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);
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM