java中如何在JTextArea中添加图片?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中如何在JTextArea中添加图片?相关的知识,希望对你有一定的参考价值。

方法最好要详细点。

JTextPane 是可以做的,

import java.io.File;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class 文本窗格里的组件和图标 extends JFrame
private JFileChooser chooser = new JFileChooser();
private JTextPane textPane = new JTextPane();

public 文本窗格里的组件和图标()
Container contentPane = getContentPane();
JMenuBar menuBar = new JMenuBar();
JMenu insertMenu = new JMenu("Insert");
JMenuItem imageItem = new JMenuItem("image"),
chooserItem = new JMenuItem("color chooser");

insertMenu.add(imageItem);
insertMenu.add(chooserItem);

menuBar.add(insertMenu);
setJMenuBar(menuBar);

textPane.setFont(new Font("Serif", Font.ITALIC, 24));

contentPane.add(textPane, BorderLayout.CENTER);

chooserItem.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
JColorChooser chooser = new JColorChooser();
chooser.setMaximumSize(
chooser.getPreferredSize());
textPane.insertComponent(chooser);

);
imageItem.addActionListener(new ActionListener()
public void actionPerformed(ActionEvent e)
int option =
chooser.showDialog(文本窗格里的组件和图标.this,"Pick An Image");

if(option == JFileChooser.APPROVE_OPTION)
File file = chooser.getSelectedFile();

if(file != null)
textPane.insertIcon(new ImageIcon(
file.getPath()));




);

public static void main(String args[])
GJApp3.launch(new 文本窗格里的组件和图标(),
"Using JTextPane",300,300,450,300);


class GJApp3 extends WindowAdapter
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");

public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h)
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);

statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);

f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);

f.addWindowListener(new WindowAdapter()
public void windowClosed(WindowEvent e)
System.exit(0);

);

static public JPanel getStatusArea()
return statusArea;

static public void updateStatus(String s)
status.setText(s);

参考技术A

JTextArea 对应于 PlainDocument, 不支持 插入图片之类Rich Text。


想插入图片需要使用JTextPane。

    JTextPane textPane = new JTextPane();
    StyledDocument doc = (StyledDocument) textPane.getDocument();

    Style style = doc.addStyle("StyleName", null);
    StyleConstants.setIcon(style, new ImageIcon("imagefile"));

    doc.insertString(doc.getLength(), "ignored text", style);

如何设置JTextArea的最大大小,如果等于或大于该大小,则可以添加滑块

我目前正在使用JTextArea。我的问题是我应该如何处理JTextArea所以我可以设置一个特定的大小,如果文本太多不适合,那么在JTextArea上添加一个滑块?

First example image enter image description here

这是创建我的JTextArea的方法:

public JPanel create_Output_Panel(){

    //Setup Main Panel of the Chat Application
    JPanel panel = new JPanel();
    title = BorderFactory.createTitledBorder("Server Screen");
    title.setTitleJustification(TitledBorder.CENTER);
    title.setTitleColor(Color.BLACK);
    panel.setBorder(title); //Set title to the Panel

    panel.setLayout(new BorderLayout());

    //Store IP Address in a String variable
    String ip_Address = new ChatServerViewer().getServer_IP_Addres();

    JLabel label = new JLabel("You are connected to Server : " + ip_Address, SwingConstants.CENTER);
    label.setFont(new Font("Serif", Font.PLAIN, 17));
    panel.add(label,BorderLayout.NORTH);

    JLabel label2 = new JLabel("Use .bye to log-out ", SwingConstants.CENTER);
    label2.setFont(new Font("Serif", Font.BOLD, 20));
    panel.add(label2);

    //CREATE TEXT AREA FOR THE USER MESSAGES
    textArea = new JTextArea(12,1);
    textArea.setFont(new Font("Serif", Font.PLAIN, 25));
    textArea.setEditable(false); //Block User from Editing the Text Area

    textArea.setText("

     Server:");
    textArea.append("
          Hello User !");

    panel.add(textArea, BorderLayout.SOUTH);

    return panel;
}
答案

我使用以下方法来解决问题!!

/**
 * Reference : https://stackoverflow.com/questions/10177183/java-add-scroll-into-text-area
 * Reference : https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html#sizing
 * @return a panel of a JTextArea inside an ScrollPane
 */
public JPanel create_Output_Panel(){

    //Setup Main Panel of the Chat Application
    JPanel panel = new JPanel();
    title = BorderFactory.createTitledBorder("Server Screen");
    title.setTitleJustification(TitledBorder.CENTER);
    title.setTitleColor(Color.BLACK);
    panel.setBorder(title); //Set title to the Panel

    panel.setLayout(new BorderLayout());

    //Store IP Address in a String variable
    String ip_Address = new ChatServerViewer().getServer_IP_Addres();

    JLabel label = new JLabel("You are connected to Server : " + ip_Address, SwingConstants.CENTER);
    label.setFont(new Font("Serif", Font.PLAIN, 17));
    panel.add(label,BorderLayout.NORTH);

    JLabel label2 = new JLabel("Use .bye to log-out ", SwingConstants.CENTER);
    label2.setFont(new Font("Serif", Font.BOLD, 20));
    panel.add(label2);

    //CREATE TEXT AREA FOR THE USER MESSAGES
    textArea = new JTextArea(15,0);
    textArea.setFont(new Font("Serif", Font.BOLD, 20));
    textArea.setEditable(false); //Block User from Editing the Text Area

    textArea.setLineWrap(true);

    JScrollPane areaScrollPane = new JScrollPane(textArea);
    areaScrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    System.out.println("textArea height = " + textArea.getSize().width);
    textArea.setText("    Server>>>   ");
    textArea.append("Connection Succesful !");

    panel.add(areaScrollPane, BorderLayout.SOUTH);

    return panel;
}

以上是关于java中如何在JTextArea中添加图片?的主要内容,如果未能解决你的问题,请参考以下文章

在 JTextArea 中添加边距?

在 JtextArea 的末尾添加一个新行

如何设置JTextArea的最大大小,如果等于或大于该大小,则可以添加滑块

Jtextarea 如何添加滚动条

jTextArea如何加滚动条

在 JTextArea 中停止水平滚动