JavaSwing中的标题分隔符

JavaSwing中的标题分隔符,java,swing,border,separator,Java,Swing,Border,Separator,我想在JavaSwing应用程序中显示标题分隔符。差不多 -------正文------- 我发现一些第三方库提供了此功能: 但我对一种不使用任何第三方api的方式感兴趣。我们可以通过扩展JSepator来做到这一点吗?我们还能怎么做 使用标题边框。例如: import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtili

我想在JavaSwing应用程序中显示标题分隔符。差不多

-------正文-------

我发现一些第三方库提供了此功能:


但我对一种不使用任何第三方api的方式感兴趣。我们可以通过扩展JSepator来做到这一点吗?我们还能怎么做

使用标题边框。例如:

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

public class TestBorder {

    protected void initUI() {
        JFrame frame = new JFrame(TestBorder.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        TitledBorder titledBorder = BorderFactory.createTitledBorder("Some title");
        titledBorder.setTitleJustification(TitledBorder.CENTER);
        panel.setBorder(titledBorder);
        frame.add(panel);
        frame.setSize(400, 300);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestBorder().initUI();
            }
        });
    }

}

我认为您可能可以使用MatteBorder和TitleBorder的组合

MatteBorder mb = new MatteBorder(1, 0, 0, 0, Color.BLACK);
TitledBorder tb = new TitledBorder(mb, "Some Long Text", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION);

这很简单,而且是小写的

import javax.swing.border.*;
import javax.swing.border.*;
然后

-----------------下一个边框类型

然后


请看我的答案:。它使用
标题边框
@SoboLAN是的,它很有用。谢谢。它在左边显示文本,我希望文本位于行分隔符的中心。@mark更新了我的代码。只需设置边框的对齐方式。标题边框(又名:GroupBox)会引入视觉噪音,因此不要使用它们-现在,即使是win ux指南也警告不要使用它们@标记:为自己实现一个titledSeparator非常简单,或者从开源项目(如SwingX或任何其他您已经完成的项目)中提取代码found@mark确切地说:-)看看卡斯滕(JGoodies)的文章/talksGreat.)而且应该是
标题边框
,而不是
标题顺序
import javax.swing.border.*;
TitledBorder tedge=new TitledBorder(TitledBorder.CENTER);
lbl.setBorder(tedge);