你可以用绝对定位来试试,并对JLabel这类component
指定大小。代码相对于其他布局可能麻烦一点,但是
比较灵活。
指定大小。代码相对于其他布局可能麻烦一点,但是
比较灵活。
解决方案 »
- 懂jfreechart做图的进
- 纯java代码实现从Access数据库查出数据后导入word文档?
- 如何处理swing中各Jpanel间的事件交互?
- <html:img page="<bean:write name="personalInfo" property="headPic"/>"/>就出错,改成<img src>就不出错,怎么办
- TableModel不显示列名
- 如何在应用程序中,设置选种字体的颜色
- 关于自定义类引用的问题
- 如何快速算出当天是星期几? 能不能提供较为快速的算法,不是函数。
- 各位师兄师姐好:我是应届生,软件公司应聘一般都问写什么,还有笔试会考些什么,谢谢大家!高分求赐教。
- 线程的疑惑~~~~~~~~
- 怎么样将字符串写到一个文本文件里?
- 可能是classpath的问题
如果想精确,那就用绝对定位.
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.io.*;public class Ttt {
JFrame frame;
JTextPane textPane;
File file;
Icon image; public Ttt(){
frame = new JFrame("JTextPane");
textPane = new JTextPane();
//file = new File("./classes/test/icon.gif"); //可加入一个小图标试验
//image = new ImageIcon(file.getAbsoluteFile().toString());
JLabel ll = new JLabel("标签");
StyledDocument doc = textPane.getStyledDocument();
try {
SimpleAttributeSet attrSet = new SimpleAttributeSet(); //文本所采用的样式
StyleConstants.setForeground(attrSet, new Color(255, 0, 0)); //文本颜色
StyleConstants.setFontSize(attrSet, 14); //文本字体Size
StyleConstants.setSubscript(attrSet, true); //将文本设成下标
doc.insertString(doc.getLength(), "我们都有一个家", attrSet); //插入有样式的文本
ll.setFont(new Font("宋体", 0, 12));
textPane.setCaretPosition(doc.getLength()); //将光标位置设置到最后
textPane.insertComponent(ll); //插入标签
//textPane.setCaretPosition(doc.getLength());//将光标位置设置到最后
//textPane.insertIcon(image); //插入图标
doc.insertString(doc.getLength(), "名字叫中国", attrSet); //插入有样式的文本
}
catch (BadLocationException ex) {
}
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) {
new Ttt();
}
}