JTextPane的初始大小 如何设置

来源:百度知道 编辑:UC知道 时间:2024/06/23 12:01:41
ublic class ServerInterface extends JFrame { JTextPane jep; public ServerInterface(){ Container cp=getContentPane(); GridBagLayout gbl = new GridBagLayout(); cp.setLayout(gbl); GridBagConstraints c = new GridBagConstraints(); jep=new JTextPane(); jep.setSize(100, 200); jep.enable(false); System.out.println(jep.HEIGHT); System.out.println(jep.WIDTH); JScrollPane jsp=new JScrollPane(jep); c.gridwidth=GridBagConstraints.REMAINDER; gbl.setConstraints(jsp, c); cp.add(jsp); JButton b1=new JButton("启动服务器"); JButton b2=new JButton("停止服务器"); c.gridwidth=1; gbl.setConstraints(b1, c); cp.add(b1); c.gridwidth=GridBagConstraints.REMAINDER; gbl.setConstraints(b2, c); cp.add(b2); }}

二楼错了
JTextPane不支持setPreferredSize(100, 200)
可以改为
jep.setPreferredSize(new Dimension(100,200));
即可

jep.setPreferredSize(100, 200);
对二楼说得对

JTextPane的例子,模拟聊天客户端,演示了为每段文字设置字体、字号、样式、颜色、背景色和插入图片功能

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing