2012-01-20 57 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

相關問題