如何通过单击按钮设置textarea的背景图像?

时间:2012-01-20 15:37:44

标签: java swing applet

我想将textarea的图像设置为背景图像,单击按钮。这怎么可能?

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.GrayFilter;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class BackgroundSample {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Background Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final ImageIcon imageIcon = new ImageIcon("draft.gif");
    JTextArea textArea = new JTextArea() {
      Image image = imageIcon.getImage();

      Image grayImage = GrayFilter.createDisabledImage(image);
      {
        setOpaque(false);
      }

  public void paint(Graphics g) {
    g.drawImage(grayImage, 0, 0, this);
    super.paint(g);
  }
};
JScrollPane scrollPane = new JScrollPane(textArea);
Container content = frame.getContentPane();
content.add(scrollPane, BorderLayout.CENTER);
frame.setSize(250, 250);
frame.setVisible(true);
  }
}

这就是我所说的。如何做同样的事情,但使用actionlistener(点击按钮)

2 个答案:

答案 0 :(得分:4)

您需要扩展JTextPane类并创建setImage(Image image)方法。此方法将保存图像的引用,然后调用repaint()。

此外,您应该重写paintComponent()方法,而不是paint()方法。忽略任何其他原因,因为它已经过时了10年。

答案 1 :(得分:0)

试试这个:

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

然后在你的动作监听器中:

@Override
public void actionPerformed(ActionEvent arg0) {
    Image image = ImageIO.read(..);
    if (image != null)
        textArea.setBackgroundImage(image);     
}

以下是一个例子:

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.*;

public class BackgroundDemo {
    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 MyTextArea textArea = new MyTextArea();
        textArea.setText("Some text");

        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)
                            textArea.setBackgroundImage(image);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

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

        frame.add(content);
        frame.setSize(new Dimension(300, 300));
        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();
            }
        });
    }
}

enter image description here