gpt4 book ai didi

java - 看起来像 JTextArea 的 JTextPane

转载 作者:行者123 更新时间:2023-12-01 23:30:15 25 4
gpt4 key购买 nike

由于我们一开始就走错了路,我再问一遍,之前的问题被删除了。请检查一下,JTextPane 的边框与 JTextArea 的边框不一样,不是默认的:

所以我需要一个看起来与 JTextArea 完全一样的 JTextPane。

我将 JTextPane 的边框设置为 new JTextArea().getBorder();。看起来应该是这样,但是焦点没有正确绘制...我该如何修复它?

我在这里使用 Nimbus,如果有帮助的话......

SSCCE:

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.UIManager.LookAndFeelInfo;


public class Main
{
public static void main(String[] args)
{
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
{
if ("Nimbus".equals(info.getName()))
{
try
{
UIManager.setLookAndFeel(info.getClassName());
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e)

{
System.out.println("No Nimbus!");
}

break;
}
}

JFrame a = new JFrame("Test");
a.setSize(200, 400);
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
a.getContentPane().setLayout(new BoxLayout(a.getContentPane(), BoxLayout.Y_AXIS));

JTextPane[] b = new JTextPane[5];

for (int i = 0; i < 5; i++)
{
b[i] = new JTextPane();
b[i].setBorder(new JTextArea().getBorder());
b[i].setText(Integer.toString(i));
a.getContentPane().add(b[i]);
}

a.setVisible(true);
}
}

我已将边框设置为与 JTextArea 上的边框相同,但焦点未正确绘制或移动。如果您注释掉该行,则不会有任何边框。

最佳答案

如果您添加一个强制重新绘制的焦点监听器,那么奇怪的行为就会消失。

示例:

for (int i = 0; i < 5; i++) {
final JTextPane b = new JTextPane();
b.setBorder(new JTextArea().getBorder());
b.addFocusListener(new FocusListener() {

@Override
public void focusGained(FocusEvent arg0) {
b.repaint();
}

@Override
public void focusLost(FocusEvent arg0) {
b.repaint();
}

});
b.setText(Integer.toString(i));
a.getContentPane().add(b);
}

这看起来像是一个黑客修复,但我不确定为什么会发生这种情况。

关于java - 看起来像 JTextArea 的 JTextPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19456073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com