Java 是否可以在JTextArea(包括JScrollPane)内设置背景图像而不是所有的框架窗口?

Java 是否可以在JTextArea(包括JScrollPane)内设置背景图像而不是所有的框架窗口?,java,swing,Java,Swing,我想在JTextArea中设置透明的背景,但我不知道。也许这不是很重要,但我只想有一个有特色的程序。请帮忙 这部分代码,我创建了JTextArea notatnik_k = new JTextArea(); JScrollPane scrollPane2 = new JScrollPane(notatnik_k); scrollPane2.setBounds(20,30, 263,200); notatnik_k.setEditable(false); add(scrollPane2);

我想在JTextArea中设置透明的背景,但我不知道。也许这不是很重要,但我只想有一个有特色的程序。请帮忙

这部分代码,我创建了JTextArea

notatnik_k = new JTextArea();
JScrollPane scrollPane2 = new JScrollPane(notatnik_k);
scrollPane2.setBounds(20,30, 263,200);
notatnik_k.setEditable(false);  
add(scrollPane2);
这张照片显示了我的问题:


有简单的方法吗?

您需要扩展JTextArea类并创建一个setBackgroundImage方法。此方法将为要使用的图像设置一个字段,然后调用repain方法This.repaint


然后,您应该重写paintComponentGraphics方法,以使用setBackgroundImage image image设置的图像绘制组件。

我在网站上找到了此代码示例,我做了一些更改,但我有一个错误,不知道为什么不正确?显示错误:类型JTextArea的方法setBackgroundImageImage未定义

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class BackgroundDemo {

    public JTextArea notatnik;

    private static void createAndShowUI() {

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (UnsupportedLookAndFeelException ex) {
        }

        final JFrame frame = new JFrame("BackgroundDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel buttonsPanel = new JPanel();

        final JTextArea notatnik = new JTextArea();
        JScrollPane scrollPane1 = new JScrollPane(notatnik);
        scrollPane1.setBounds(20,270, 380,110);


       JButton loadButton = new JButton("Set background");
        buttonsPanel.add(loadButton);

        loadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
                int returnVal = fc.showOpenDialog(frame);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                        Image image = ImageIO.read(fc.getSelectedFile());
                        if (image != null)
                            notatnik.setBackgroundImage(image);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        JPanel content = new JPanel(new BorderLayout());
        content.add(buttonsPanel, BorderLayout.SOUTH);

        frame.add(scrollPane1);
        frame.add(content);
        frame.setSize(new Dimension(800, 500));
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);      
    }

    static class MyTextArea extends JTextArea {
        private Image backgroundImage;

        public MyTextArea() {
            super();
            setOpaque(false);
        }

        public void setBackgroundImage(Image image) {
            this.backgroundImage = image;
            this.repaint();
        }

        @Override
        protected void paintComponent(Graphics g) {
            g.setColor(getBackground());
            g.fillRect(0, 0, getWidth(), getHeight());

            if (backgroundImage != null) {
                g.drawImage(backgroundImage, 0, 0, this);
            }
            super.paintComponent(g);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

出现错误的原因是,您已将notatnik声明为JTextArea,此类没有名为setBackgroundImage的方法…您也没有创建MyTextArea的实例,因此无论如何它都不会工作..我使用了来自第一个响应主题的代码:,可以在背景中设置图像的透明度吗?:我不知道如何在JTextArea中设置图像,而不是在所有屏幕上。我看过很多例子,但都是关于如何在全屏幕上设置背景的;AlphaComposite是外部库吗?