JAVA如何实现图文混合显示
2007-05-29 20:20
423 查看
用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("ok.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);
//字体大小
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();
}
}
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("ok.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);
//字体大小
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();
}
}
相关文章推荐
- java中如何实现表格的多表头显示
- java中如何实现二级界面显示
- 在页面中,我们经常看到,一个button按钮,如果属标点击,就会触发一个窗口的显示,如果二次点击并可以隐藏,那么如何通过JAVA配合html来实现这一功能呢?
- 如何实现将java中Double类型的科学计数法转换为指定格式显示?
- 阿里云 > 教程中心 > java教程 > 详细的图文教程来实现 eclipse环境下如何配置tomcat,并且把项目部署到Tomcat服务器上 详细的图文教程来实现 eclipse
- asp.net中chart中如何插入table实现混合显示
- Java如何实现长图文生成
- java程序如何实现右下角系统托盘图标显示
- 使用Jquery+Ajax+Json如何实现分页显示附JAVA+JQuery实现异步分页
- Java如何实现长图文生成的示例代码
- Java如何实现按指定行读取文件
- web开发 java如何连接数据库并取得数据,实现 增,删,改,查
- java如何快速实现深copy
- Java中如何动态创建接口的实现
- 如何实现一个循环显示超长图片的控件
- JAVAWEB项目如何实现验证码
- Java写到.txt文件,如何实现换行
- Android中继承AppCompatActivity后如何实现无标题全屏显示
- 如何用html5实现在网页上显示本地摄像头
- ireport5.6使用table组件,如何用table显示javaBean数据源