本文介绍了Nimbus JTextArea默认边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我的程序中有一个使用Nimbus LAF的JTextArea.由于某些功能问题,我需要将其替换为JTextPane.

So, I have a JTextArea in my program which uses Nimbus LAF. I need to swap it for JTextPane because of some functionality issues.

但是,默认情况下,JTextArea具有绘制的边框. JTextPane没有.我不知道将JTextArea的默认边框设置为JTextPane.

However, JTextArea has a painted border by default. JTextPane does not. I do not know which is the JTextArea's default border to set it to JTextPane.

我尝试使用getBorder(),但只返回了"javax.swing.plaf.synth.SynthBorder@455e3f91"

I tried with getBorder(), but that only returned "javax.swing.plaf.synth.SynthBorder@455e3f91"

如何将默认的JTextBoreder设置为JTextPane?

How do I get default JTextBoreder to JTextPane?

推荐答案

在Nimbus与这些 * 画家一起交给我几个月后,我取得了胜利.

After having Nimbus hand it to me for months with these * Painters, I emerge victorious.

请注意,所使用的密钥可以在操作系统之间进行更改(因此不能保证在那里使用,但确实可以在我的操作系统上使用).只要您具有JTextArea边框的有效键,就可以将其转移到您的JTextPane.

Please note that the keys used can change between operating systems (so no guarantee there, but it does work on mine). So long as you have a valid key for the border of JTextArea you can transfer it to your JTextPane.

import java.awt.Insets;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;


public class NimbusBorderPainting extends Box{

    public NimbusBorderPainting(){
        super(BoxLayout.Y_AXIS);
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (UnsupportedLookAndFeelException e2) {
            e2.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //Retrieve the TextArea painter from the defaults 
        Object o = UIManager.get("TextArea[Enabled+NotInScrollPane].borderPainter");        

        //Transfer the Painter to a TextPane key
        UIDefaults paneDefaults = new UIDefaults();
        paneDefaults.put("TextPane.borderPainter",o);

        JTextPane pane = new JTextPane();
        pane.setMargin(new Insets(10, 10, 10, 10));

        //Apply the new UI to your text pane
        pane.putClientProperty("Nimbus.Overrides",paneDefaults);
        pane.putClientProperty("Nimbus.Overrides.InheritDefaults",false);
        pane.setText("Lots of Text\nWell, as much as I'm willing to type\n");
        add(pane);

    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new NimbusBorderPainting());
        frame.validate();
        frame.pack();
        frame.setVisible(true);
    }

}

这篇关于Nimbus JTextArea默认边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 09:57