Java 在JTextpane的所有4个边上具有相同大小的边框
我正在将Java 在JTextpane的所有4个边上具有相同大小的边框,java,swing,border,jtextpane,Java,Swing,Border,Jtextpane,我正在将createLineborder设置为我的JTextPane。但是与TextPane的左端和顶部相比,JTextpane的右端和底部的边界线差别不大。我在网上搜索,发现这是lineborder的默认行为。有谁能告诉我,有没有任何边界在四条边上都有相同的线 嗨,我不能把我的代码,因为它是非常巨大的。所以在这里放一个示例代码。 JPanel小组; JTextPane窗格 public BorderedTextPane() { // TODO Auto-generated constr
createLineborder
设置为我的JTextPane
。但是与TextPane
的左端和顶部相比,JTextpane
的右端和底部的边界线差别不大。我在网上搜索,发现这是lineborder
的默认行为。有谁能告诉我,有没有任何边界在四条边上都有相同的线
嗨,我不能把我的代码,因为它是非常巨大的。所以在这里放一个示例代码。
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);
}
如果缩放文本窗格或将其保存到文件中,可以清楚地看到这一点。下图将以更好的方式解释它。比较左右两行。
来自API:
虽然从技术上讲,您可以在继承自JComponent
的任何对象上设置边框,但许多标准Swing组件的外观实现在用户设置边框时并不适用。通常,当您要在标准Swing组件上设置边框而不是JPanel
或JLabel
时,建议您将组件放置在JPanel
中,并在JPanel
上设置边框
我想说它更多地与使用
null
layouts有关
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);
}
}
}
请参考API setboorder方法(new EmptyBorder())
以及使用变量JTextPane varible.setboorder(BorderFactory.createLineBorder(Color.xxx)问题是JTextPane放在Jscrollpane中,详细说明mkorbel的评论。线条边框只为边框绘制一个像素。如果看到较粗的线条,它来自父组件。请更改线条边框的颜色以查看差异。