小编n00*_*ter的帖子

"比较方法违反了其总合同!"

有人可以用简单的语言解释我,为什么这个代码会抛出异常,"比较方法违反了它的一般合同!",我该如何修复它?

private int compareParents(Foo s1, Foo s2) {
    if (s1.getParent() == s2) return -1;
    if (s2.getParent() == s1) return 1;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

java comparator

179
推荐指数
6
解决办法
18万
查看次数

JTextPane/JTextField奇怪的行为与稀有字符

我在JTextPane/JTextField中发现了一个奇怪的错误(或者在它们下面的字体渲染中的某个地方).我想知道是否有其他人遇到过这种情况并且可能有解决方案.

我试图在JTextPane中显示一些"特殊"或罕见的字符,并且一旦我更改了JTextField的字体(与JTextPane完全无关!),JTextPane就会"分解",并且不再显示这些字符字符.

这应该更好地解释我的意思:

public class Scrap {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setLayout(new BorderLayout());

    JTextField field = new JTextField();

    // Uncomment this line... and the JTextPane nor the JTextField
    // no longer display the characters
    // field.setFont(new Font("Arial", Font.PLAIN, 14));

    frame.add(field, BorderLayout.SOUTH);

    JTextPane textPane = new JTextPane();
    textPane.setFont(new Font("Arial", Font.PLAIN, 14));

    JScrollPane scroll = new JScrollPane(textPane);
    frame.add(scroll, BorderLayout.CENTER);

    StyledDocument doc = (StyledDocument) textPane.getDocument();

    try {
        String str = "? ?? ?"; …
Run Code Online (Sandbox Code Playgroud)

java swing jtextpane jtextfield

5
推荐指数
1
解决办法
2443
查看次数

合金外观和抗锯齿

这里有人使用合金外观吗?我在抗锯齿和 JTextComponents 方面遇到了一个奇怪的错误。Alloy 默认情况下根本不使用抗锯齿功能,因此我必须通过创建自己的 UI 类来强制使用它。这在大多数情况下都可以正常工作,但某些字符会对抗锯齿造成严重破坏。

\n\n

例如,如果合金设置为外观,并且我将一些希伯来语文本插入到 JTextComponent 中,例如:\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d、\xd7\x9e\xd7\ x94 \xd7\xa9\xd7\x9c\xd7\x95\xd7\x9e\xd7\x9a \xd7\xa9\xd7\x9e\xd7\x99 \xd7\x94\xd7\x95\xd7\x90 \xd7\x94\ xd7\x90\xd7\xa7\xd7\x96\xd7\x99\xd7\x93\',整个 JTextComponent 突然永久失去抗锯齿功能。

\n\n

奇怪的是,我什至没有扩展 AlloyTextPaneUI,而是扩展 BasicTextPaneUI 来进行抗锯齿,所以我很困惑合金在哪里出现(其他外观和感觉似乎工作得很好)。

\n\n

我很难追踪这个错误...有人遇到过同样的问题吗?

\n\n

这是一个演示该问题的简短示例:

\n\n
import com.incors.plaf.alloy.AlloyLookAndFeel;\nimport com.incors.plaf.alloy.themes.glass.GlassTheme;\n\nimport javax.swing.*;\nimport javax.swing.plaf.ComponentUI;\nimport javax.swing.plaf.basic.BasicTextPaneUI;\nimport javax.swing.text.BadLocationException;\nimport javax.swing.text.StyledDocument;\nimport java.awt.*;\n\npublic class Scrap {\n\n    static {\n        // NOTE: You need a license code for Alloy!\n        AlloyLookAndFeel.setProperty("alloy.licenseCode", "your license here");\n\n        UIManager.put("TextPaneUI", MyTextPaneUI.class.getName());\n\n        try {\n            UIManager.setLookAndFeel(new AlloyLookAndFeel(new GlassTheme()));\n        } catch (UnsupportedLookAndFeelException e) {\n            e.printStackTrace();\n        }\n\n        // With system Look and Feel everything works just fine...\n//      try …
Run Code Online (Sandbox Code Playgroud)

java swing jtextpane antialiasing look-and-feel

5
推荐指数
1
解决办法
1246
查看次数

JTextPane 和水平线

我将如何继续在 JTextPane 中创建水平线元素?只是一个可能由 View.paint(Graphics) 绘制的矩形,它覆盖了 JTextPane 的整个宽度,并且具有任意高度。我还需要能够动态删除这些元素。

我试图通过创建一个自定义编辑器套件来解决这个问题,该套件具有一个自定义视图工厂,它返回行元素的“Horizo​​ntalLineView”...但我必须承认这一切都有点超出了我的想象!例如,我如何为此 Horizo​​ntalLineView 创建元素?到目前为止我只使用过 insertString()...而且我在任何地方都没有看到“addElement”方法...任何指向正确方向的指针都会很棒。

我会偷偷提出另一个问题:任何人都可以推荐一本深入介绍 JEditorPane/JTextPane 的好书吗?

java swing jtextpane

1
推荐指数
1
解决办法
1482
查看次数