999.......紧急事件....请问在TextArea里面显示图片... 拜托...各位哥哥姐姐...告诉我在在TextArea里面显示图片咯...谢谢....谢谢..... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 拜托....告诉我在在TextArea里面怎么显示图片咯... ImageIcon icon = new ImageIcon("Calv.gif");JLabel calv_label = new JLabel("这是 Calvin", icon, SwingConstants.LEFT); 在JLable里面我知道如何添加ICON,我要在Jtextarea里面添加图片哦 。 JTextArea使用的Document只支持纯文本类型,建议使用JTextPane,除了添加图片外,还能给文字设置颜色、字体效果等。 给你个例子:import javax.swing.*;import java.awt.*;import java.awt.event.*;import javax.swing.text.*;import java.io.*;public class Test { JFrame frame; JTextPane textPane; File file; Icon image; public Test(){ frame = new JFrame("JTextPane"); textPane = new JTextPane(); file = new File("icon.gif"); image = new ImageIcon(file.getAbsoluteFile().toString()); } public void insert(String str, AttributeSet attrSet) { Document doc = textPane.getDocument(); str ="\n" + str ; try { doc.insertString(doc.getLength(), str, attrSet); } catch (BadLocationException e) { System.out.println("BadLocationException: " + e); } } public void setDocs(String str,Color col,boolean bold,int fontSize) { SimpleAttributeSet attrSet = new SimpleAttributeSet(); StyleConstants.setForeground(attrSet, col); //颜色 if(bold==true){ StyleConstants.setBold(attrSet, true); }//字体类型 StyleConstants.setFontSize(attrSet, fontSize); //字体大小 //StyleConstants.setFontFamily(attrSet, "黑体"); //设置字体 insert(str, attrSet); } public void gui() { textPane.insertIcon(image); setDocs("第一行的文字",Color.red,false,20); setDocs("第二行的文字",Color.BLACK,true,25); setDocs("第三行的文字",Color.BLUE,false,20); frame.getContentPane().add(textPane, BorderLayout.CENTER); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); }}); frame.setSize(200,300); frame.setVisible(true); } public static void main(String[] args) { Test test = new Test(); test.gui(); }} JTextArea jPanel4 = new JTextArea (){ public void paintChildren(Graphics g) { if (backgroundImg != null) { g.drawImage(backgroundImg.getImage(), 0, 0, backgroundImg.getImage().getWidth(null), backgroundImg.getImage().getHeight(null), null); } super.paintChildren(g); } }; 没装sysbase,jdbc怎么直接连它的db数据库? Swing 窗体面板上控件无法正常显示? 求教大虾 IO流? 线程所在的线程组是如何确定的? 想让一个java程序在一台机器上只能执行一个,该怎么办? 如何让javaApplet进行socket通信 在jbuilt中的这个错误是什么意思?? 如何使用java访问SQL Server数据库,如何配置?谢谢,我用的UE编辑java,急急 如何判断一个字符是中文字符? 怎样实现文件对话框的文件过滤功能………………急!!! 我在深圳见到的题
告诉我在在TextArea里面怎么显示图片咯...
JLabel calv_label = new JLabel("这是 Calvin", icon, SwingConstants.LEFT);
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.io.*;public class Test {
JFrame frame;
JTextPane textPane;
File file;
Icon image; public Test(){
frame = new JFrame("JTextPane");
textPane = new JTextPane();
file = new File("icon.gif");
image = new ImageIcon(file.getAbsoluteFile().toString());
} public void insert(String str, AttributeSet attrSet) {
Document doc = textPane.getDocument();
str ="\n" + str ;
try {
doc.insertString(doc.getLength(), str, attrSet);
}
catch (BadLocationException e) {
System.out.println("BadLocationException: " + e);
}
} public void setDocs(String str,Color col,boolean bold,int fontSize) {
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setForeground(attrSet, col);
//颜色
if(bold==true){
StyleConstants.setBold(attrSet, true);
}//字体类型
StyleConstants.setFontSize(attrSet, fontSize);
//字体大小
//StyleConstants.setFontFamily(attrSet, "黑体");
//设置字体
insert(str, attrSet);
} public void gui() {
textPane.insertIcon(image);
setDocs("第一行的文字",Color.red,false,20);
setDocs("第二行的文字",Color.BLACK,true,25);
setDocs("第三行的文字",Color.BLUE,false,20);
frame.getContentPane().add(textPane, BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
frame.setSize(200,300);
frame.setVisible(true);
}
public static void main(String[] args) {
Test test = new Test();
test.gui();
}
}
public void paintChildren(Graphics g) {
if (backgroundImg != null) {
g.drawImage(backgroundImg.getImage(), 0, 0,
backgroundImg.getImage().getWidth(null),
backgroundImg.getImage().getHeight(null), null);
}
super.paintChildren(g);
} };