我正在为createLineborder
设置JTextPane
。但是,与JTextpane
的左端和顶端相比,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);
}
如果缩放文本窗格或将其保存在文件中,可以清楚地看到这一点。下图可以更好地解释它。比较左右线。
答案 0 :(得分: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);
}
}
}
答案 1 :(得分:1)
来自JComponent#setBorder()
API:
虽然从技术上讲,您可以在任何继承自
JComponent
的对象上设置边框,但许多标准Swing组件的外观实现与用户设置的边框不兼容。通常,如果要在JPanel
或JLabel
以外的标准Swing组件上设置边框,我们建议您将组件放在JPanel
中并在JPanel
上设置边框{1}}。
答案 2 :(得分:0)
咨询API setBorder方法(new EmptyBorder());
以及使用变量JTextPane varible.setBorder(BorderFactory.createLineBorder(Color.xxx);