JTextArea中不可选的,但仍然呈现出“鬼”光标(JTextArea not selectabl

2019-07-18 05:47发布

我把一个JTextArea在一个JPanel。 此JPanel对背景图片,并且JTextArea是半透明(半透明红色)通过以显示背景。 我不希望用户能够编辑或选择文本,我希望它采取行动,就像一个JL​​abel(但多行,易换行并调整屏幕大小调整)。

我尝试了所有这些选项:

text.setEditable(false);
text.setFocusable(false);
text.setEnabled(false);
text.setHighlighter(null);

但还是彩色的一些变化发生,因为用户拖动鼠标悬停在JTextArea中。 任何人都知道是怎么回事?

Answer 1:

你不能简单地设置一个组件为“透明”的背景颜色和期望的Swing来对付它。 您需要标记组件为透明( setOpaque(false) ),只有这样,Swing的重绘经理知道它下设更新的组件。

这就导致你如何绘制背景(如摇摆只有完全不透明或完全透明的概念)的问题。

要做到这一点,你需要提供你自己的油漆例程(重写paintComponent ,填充背景,更新组件)......实际上,这就是罗布Camick的解决方案是干什么的,它只是提供了一个很好的包装部件为您...

下面是使用的例子JLabel使用文本包裹在HTML和JTextArea ,既更新,以支持“半透明” ...

使用JLabel

使用JTextArea

现在,这将是一个更容易使用Rob的包装类来实现的,但是这为您提供了什么错误的想法,哪些是你需要做修复它。

public class MultiLineLabel {

    public static void main(String[] args) {
        new MultiLineLabel();
    }

    public MultiLineLabel() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new BackgroundPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TransclucentLabel extends JLabel {

        public TransclucentLabel(String text) {
            super(text);
            setVerticalAlignment(TOP);
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            Insets insets = getInsets();
            int x = insets.left;
            int y = insets.top;
            int width = getWidth() - (insets.left + insets.right);
            int height = getHeight() - (insets.top + insets.bottom);
            g2d.setColor(new Color(255, 0, 0, 128));
            g2d.fillRect(x, y, width, height);
            super.paintComponent(g);
        }
    }

    public class TransclucentTextArea extends JTextArea {

        public TransclucentTextArea(String text) {
            super(text);
            setOpaque(false);
            setLineWrap(true);
            setWrapStyleWord(true);
        }

        @Override
        protected void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            Insets insets = getInsets();
            int x = insets.left;
            int y = insets.top;
            int width = getWidth() - (insets.left + insets.right);
            int height = getHeight() - (insets.top + insets.bottom);
            g2d.setColor(new Color(255, 0, 0, 128));
            g2d.fillRect(x, y, width, height);
            super.paintComponent(g);
        }
    }

    public class BackgroundPane extends JPanel {

        private BufferedImage background;

        public BackgroundPane() {
            setLayout(new BorderLayout());
//            addLabel();
            addTextArea();
            setBorder(new EmptyBorder(24, 24, 24, 24));

            try {
                background = ImageIO.read(new File("/path/to/your/image"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        protected void addTextArea() {
            StringBuilder sb = new StringBuilder(128);
            sb.append("I put a JTextArea in a JPanel. This JPanel has a picture on the background, and the JTextArea is translucent (translucid red) to show the background through. I don't want the user to be able to edit or select the text, I want it to act just as a JLabel (but with multiple lines and easy to word wrap and adjust to screen resize).\n\n");
            sb.append("I tried all these options:\n\n");
            sb.append("text.setEditable(false);\n");
            sb.append("text.setFocusable(false);\n");
            sb.append("text.setEnabled(false);\n");
            sb.append("text.setHighlighter(null);\n\n");
            sb.append("but still some change of color happens as the user drags the mouse over the JTextArea. Anyone knows what is going on?\n");
            add(new TransclucentTextArea(sb.toString()));
        }

        protected void addLabel() {
            StringBuilder sb = new StringBuilder(128);
            sb.append("<html>");
            sb.append("<p>I put a JTextArea in a JPanel. This JPanel has a picture on the background, and the JTextArea is translucent (translucid red) to show the background through. I don't want the user to be able to edit or select the text, I want it to act just as a JLabel (but with multiple lines and easy to word wrap and adjust to screen resize).</p><br>");
            sb.append("<p>I tried all these options:</p><br>");
            sb.append("<p>text.setEditable(false);<br>");
            sb.append("text.setFocusable(false);<br>");
            sb.append("text.setEnabled(false);<br>");
            sb.append("text.setHighlighter(null);</p><br>");
            sb.append("<p>but still some change of color happens as the user drags the mouse over the JTextArea. Anyone knows what is going on?</p>");
            add(new TransclucentLabel(sb.toString()));

        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? super.getPreferredSize() : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g.drawImage(background, x, y, this);
            }
        }
    }
}


文章来源: JTextArea not selectable, but still showing a “ghost” cursor