2012-01-20 126 views
3

我想设置一个textarea的图像作为背景图像,点击一个按钮。这怎么可能?如何点击一个按钮设置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(点击按钮)

+3

你尝试过这么远吗?你看过事件监听器,特别是动作监听器吗? – mre

+0

我能够在文本区域中显示背景图像,同时调用textarea的构造函数,但我不知道如何使用动作侦听器执行此操作。 –

+0

和信息,我正在处理一个小程序.. –

回答

4

您需要扩展您的JTextPane类,并创建setImage(Image image)方法。该方法将保存图像的引用,然后调用repaint()。另外,你应该重写paintComponent()方法,而不是paint()方法。忽略任何其他教程,因为它已过时10年。在Action监听

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

然后:

+0

我试过一些东西。但它似乎没有工作..你可以告诉我什么是问题。这是我的代码http://dl.dropbox.com/u/44103654/ColorPalette.java –

0

试试这个

@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

+0

-1,1)类名称不应该是TextArea,因为已经有一个名称为AWT的组件可能导致混淆。 2)你不会创建一个ImageIcon来阅读图像。相反,你可能会使用ImageIO。 3)因为这个答案是在2小时前给出的,所以没有必要混淆论坛。勺子喂食代码没有帮助,并且可以引入由点1和2指出的不良习惯。 – camickr

+2

编辑+1(最后解决@camickr点:-) – kleopatra

相关问题