2

我正在createLineborder为我的JTextPane. JTextpane但是与 Left-end 和 top of 相比,right-end 和 bottom of 的边界线略有不同TextPane。我在网上搜索,发现这是lineborder. 那么任何人都可以告诉我是否有任何边框在所有 4 个边上给出相同的线条大小?

嗨,我无法输入我的代码,因为它非常庞大。所以在这里放一个示例代码。JPanel面板;JTextPane 窗格;

public BorderedTextPane() {
    // TODO Auto-generated constructor stub
    pane = new JTextPane();
    panel = new JPanel(null);
    JPanel innerPanel = new JPanel(null);
    innerPanel.setBounds(50,50,300,400);
    pane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    pane.setSize(new Dimension(innerPanel.getWidth(),innerPanel.getHeight()));
    innerPanel.add(pane);
    panel.add(innerPanel);
    add(panel);
    setVisible(true);
    setSize(new Dimension(500,500));
    setLocationRelativeTo(null);
}

如果您缩放文本窗格或将其保存在文件中,则可以清楚地看到这一点。下图将更好地解释它。比较左右线。 在此处输入图像描述

4

3 回答 3

2

我会说它更多地与使用null布局有关......

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

public class TestBorder {

    public static void main(String[] args) {
        new TestBorder();
    }

    public TestBorder() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new BorderLayout());
            setBorder(new EmptyBorder(10, 10, 10, 10));
            JTextPane pane = new JTextPane();
            JPanel panel = new JPanel(new BorderLayout());
            JPanel innerPanel = new JPanel(new BorderLayout());

            pane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            innerPanel.add(pane);
            panel.add(innerPanel);
            add(panel);
        }
    }
}
于 2013-04-07T07:33:15.180 回答
1

JComponent#setBorder()API:

尽管从技术上讲,您可以在任何继承自 的对象上设置边框JComponent,但许多标准 Swing 组件的外观实现不能很好地与用户设置的边框配合使用。一般情况下,当您要在除JPanelor之外的标准 Swing 组件上设置边框时JLabel,我们建议您将组件放在 a 中JPanel,并将边框设置在JPanel.

于 2013-04-07T07:17:32.787 回答
0

请教API setBorder 方法(new EmptyBorder());

以及使用变量 JTextPane varible.setBorder (BorderFactory.createLineBorder (Color.xxx);

于 2017-05-24T20:09:48.590 回答