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

时间:2014-10-19 17:35:43

标签: 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);

这张照片显示了我的问题:

Photo inside JTextArea including transparency

有一种简单的方法吗?

2 个答案:

答案 0 :(得分:0)

您需要扩展JTextArea类并创建setBackground(Image image)方法。此方法将为要使用的图像设置一个字段,然后调用重绘方法(this.repaint())。

然后,您应该覆盖paintComponent(Graphics)方法,使用setBackground(Image image)为您设置的图像绘制组件。

答案 1 :(得分:0)

我在网站上找到了这个代码示例,我做了一点改动,但我有一个错误而且不知道为什么它不正确?它显示错误:“方法setBackgroundImage(Image)未定义类型JTextArea”

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();
            }
        });
    }
}