2013-04-13 302 views
0

所以我試圖在JTextArea下插入一個圖像,但我沒有很多運氣,任何人都可以請幫忙嗎?基本上我問的是,如果有人能夠幫助創建另一個類或子類來做到這一點。我的繼承人代碼:在JTextArea下插入圖像

import java.awt.*; 
import javax.swing.*; 


public class t{ 
    private JFrame f; //Main frame 
    private JTextArea t; // Text area private JScrollPane sbrText; // Scroll pane for text area 
    private JButton btnQuit; // Quit Program 

    public t(){ //Constructor 
     // Create Frame 
     f = new JFrame("Test");   
     f.getContentPane().setLayout(new FlowLayout());   
     String essay = "Test"; 
     // Create Scrolling Text Area in Swing 
     t = new JTextArea(essay, 25, 35); 
     t.setEditable(false); 
     Font f = new Font("Verdana", Font.BOLD, 12); 
     t.setFont(f);   
     t.setLineWrap(true);   
     sbrText = new JScrollPane(t); 
     sbrText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
       // Create Quit Button 
     btnQuit = new JButton("Quit"); 
     btnQuit.addActionListener(new ActionListener(){ 
       public void actionPerformed(ActionEvent e){ 
        System.exit(0);   
       }   }  ); 
    } 


    public void launchFrame(){ // Create Layout 
     // Add text area and button to frame 
     f.getContentPane().add(sbrText); 
     f.getContentPane().add(btnQuit); 
       // Close when the close button is clicked 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Display Frame 
     f.pack(); // Adjusts frame to size of components 
     f.setSize(450,480); 
     f.setResizable(false); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    } 

    public static void main(String args[]){ 
     t gui = new t();  
     gui.launchFrame(); 
    } 

}

+2

你可以看看[這](http://stackoverflow.com/questions/14152491/swing-graphics-on-jframe/14152568#14152568),這表明下paining JTextArea的文本 – MadProgrammer

回答

1

退房的Background Panel。向面板添加滾動窗格時,它將使滾動窗格,視口和文本區域都不透明,以便您可以看到圖像。

1

基本問題是,JTextArea將繪製它的背景,它的文本在paintComponent內。

最簡單的解決方案是使JTextArea透明並接管繪製背景的控制。

本示例基本上用背景色填充背景,繪製圖像,然後調用super.paintComponent以允許呈現文本。

enter image description here

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TransparentTextArea { 

    public static void main(String[] args) { 
     new TransparentTextArea(); 
    } 

    public TransparentTextArea() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new JScrollPane(new CustomTextArea())); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 

     }); 
    } 

    public class CustomTextArea extends JTextArea { 

     private BufferedImage image; 

     public CustomTextArea() { 
      super(20, 20); 
      try { 
       image = ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/Miho_Small_02.png")); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 

     @Override 
     public boolean isOpaque() { 
      return false; 
     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.setColor(getBackground()); 
      g2d.fillRect(0, 0, getWidth(), getHeight()); 
      if (image != null) { 
       int x = getWidth() - image.getWidth(); 
       int y = getHeight() - image.getHeight(); 
       g2d.drawImage(image, x, y, this);  
      } 
      super.paintComponent(g2d); 
      g2d.dispose(); 
     } 

    } 

} 
+0

對不起,這不是我正在尋找的。當我向後滾動時,圖像停留在底部,即時尋找要在JTextArea下方的圖像。 – user2278319

+0

定義「底部」 – MadProgrammer

+0

如Photoshop圖層或堆疊紙張。底部是JFrame,接下來是圖片,然後頂部作爲jtextarea – user2278319