在JTextArea下插入图像

在JTextArea下插入图像

本文介绍了在JTextArea下插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在JTextArea下面插入图片,但是我没有多少运气,谁能请帮忙?基本上我问的是,如果有人能够帮助创建另一个类或子类来做到这一点。下面是我的代码:

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


公共类t {
private JFrame f; //主框架
private JTextArea t; //文本区域private JScrollPane sbrText; //文本区域的滚动窗格
private JButton btnQuit; //退出程序

public t(){//构造函数
//创建框架
f = new JFrame(Test);
f.getContentPane()。setLayout(new FlowLayout());
String essay =Test;
//在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);
//创建退出按钮
btnQuit = new JButton(Quit);
btnQuit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}});



public void launchFrame(){//创建布局
//添加文本区域和按钮以框架
f.getContentPane()。添加(sbrText);
f.getContentPane()。add(btnQuit);
//单击关闭按钮时关闭
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//显示框架
f.pack(); //将框架调整为组件的大小
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();
}

}

解决方案基本问题是 JTextArea 会绘制它的背景,它的文本位于 paintComponent

最简单的解决方案是使 JTextArea 透明,并接管绘制背景的控制。

这个例子基本上用背景颜色填充背景,绘制图像,然后调用 super.paintComponent 来允许要显示的文本。



  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){
TransparentTextArea();

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

JFrame框架= 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);
尝试{
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
保护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();
}

}

}


so I'm trying to insert an image underneath a JTextArea, but I havent had much luck, could anyone please help? Basically what I'm asking is if anybody could help make another class or subclass that does this. Heres my code:

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

}

解决方案

The basic issue is that JTextArea will paint it's background and it's text within the paintComponent.

The simplest solution is to make the JTextArea transparent and take over the control of painting the background.

This example basically fills the background with the background color, paints the image and then calls super.paintComponent to allow the text to be rendered.

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

    }

}

这篇关于在JTextArea下插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:52