关于 JTextPane 的问题?
使用JTextPane.setCharacterAttributes(AttributeSet, false);
对JTextPane的属性进行设置。
当在JTextPane中输入文本时,为什么没有反应?
必须要我点击激活。
问题点数:50、回复次数:18Top
1 楼LittleLandlord(小地主)回复于 2004-09-04 14:39:59 得分 20
调用刷新方法试Top
2 楼LittleLandlord(小地主)回复于 2004-09-04 14:40:58 得分 0
或者在初始化JTextPane时设置字符属性Top
3 楼iiisan(學無止境)回复于 2004-09-04 14:43:07 得分 0
谢谢,两个方法都试过了,还是不行。
Top
4 楼vongood((my c++))回复于 2004-09-04 14:45:25 得分 10
按道理应该行的。Top
5 楼iiisan(學無止境)回复于 2004-09-04 14:47:30 得分 0
三个Button,分别对应Bold,Italic,Underline,
点击Button,设置JTextPane的属性。
如果我先在JTextPane里输入文本,在点击,随后输入的文本字体属性改变。
但是如果JTextPane开始时没有内容,点击输入的文本字体不会变。
为什么?Top
6 楼iiisan(學無止境)回复于 2004-09-04 14:48:18 得分 0
我也知道按道理是可以的,不过就是找不到原因!Top
7 楼iiisan(學無止境)回复于 2004-09-04 14:51:27 得分 0
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import java.awt.Toolkit;
import javax.swing.border.Border;
import java.util.Hashtable;
import javax.swing.JScrollPane;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.SimpleAttributeSet;
import java.util.Enumeration;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
public class Chat
{
public JFrame frame;
public JTextPane inputPane;
public JTextPane outputPane;
public JScrollPane input, output;
public JButtonNoFocus boldButton, italicButton, underlineButton, ulistButton, olistButton;
public JButton enterButton;
public JPanel panel, controlPanel;
public JComboBoxNoFocus colorBox, fontBox, sizeBox;
public ExtendedHTMLDocument htmlDocument;
public ExtendedHTMLEditorKit htmlEditorKit;
public boolean bold, italic, underline, ulist, olist;
public Hashtable actionTable = new Hashtable();
public static SimpleAttributeSet tagSet = new SimpleAttributeSet();
public SimpleAttributeSet fontSet = new SimpleAttributeSet();
public Hashtable fontAttribute = new Hashtable();Top
8 楼LittleLandlord(小地主)回复于 2004-09-04 14:55:34 得分 0
If there is a selection, the attributes are applied to the selection range.
你看一下API文档,里面写的是把属性应用到选择的文本区的Top
9 楼iiisan(學無止境)回复于 2004-09-04 14:56:20 得分 0
public Chat()
{
setUI();
}
public void setUI()
{
frame = new JFrame("CHAT UI");
panel = new JPanel()
{
public void reshape(int x,int y,int width,int height)
{
super.reshape(x,y,width,height);
output.setBounds(3, 3, width - 6, height - 100);
controlPanel.setBounds(3, output.getHeight() + 3, width, 35);
input.setBounds(3, output.getHeight() + controlPanel.getHeight() + 3,
width-80, 55);
enterButton.setBounds(width - 68,
8 + output.getHeight() + controlPanel.getHeight(),
60, 45);
}
public Dimension getPreferredSize()
{
return new Dimension(100, 60);
}
};
htmlDocument = new ExtendedHTMLDocument();
htmlEditorKit = new ExtendedHTMLEditorKit();
htmlDocument.addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{
refreshOnUpdate();
}
public void insertUpdate(DocumentEvent e)
{
setAttribute();
}
public void removeUpdate(DocumentEvent e)
{
}
});
outputPane = new JTextPane();
outputPane.setDocument(htmlDocument);
outputPane.setEditorKit(htmlEditorKit);
outputPane.setEditable(false);
output = new JScrollPane(outputPane);
output.setBorder(BorderFactory.createLoweredBevelBorder());
inputPane = new JTextPane();
inputPane.setDocument(htmlDocument);
inputPane.setEditorKit(htmlEditorKit);
inputPane.requestFocus(true);
//inputPane.setCharacterAttributes(tagSet, false);
input = new JScrollPane(inputPane);
input.setBorder(BorderFactory.createLoweredBevelBorder());
controlPanel = new JPanel();
controlPanel.setLayout(null);
setButtonAction();
setComboBoxAction();
controlPanel.add(boldButton);
controlPanel.add(italicButton);
controlPanel.add(underlineButton);
controlPanel.add(ulistButton);
controlPanel.add(olistButton);
controlPanel.add(sizeBox);
controlPanel.add(fontBox);
controlPanel.add(colorBox);
boldButton.setBounds(10, 8, 20, 20);
italicButton.setBounds(38, 8, 20, 20);
underlineButton.setBounds(66, 8, 20, 20);
ulistButton.setBounds(97, 8, 20, 20);
olistButton.setBounds(128, 8, 20, 20);
sizeBox.setBounds(159, 8, 50, 20);
fontBox.setBounds(220, 8, 100, 20);
colorBox.setBounds(331, 8, 60, 20);
enterButton = new JButton("Enter");
enterButton.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
send(inputPane, outputPane);
inputPane.setText(null);
setAttribute();
inputPane.requestFocus(true);
}
});
panel.setLayout(null);
panel.add(output);
panel.add(controlPanel);
panel.add(input);
panel.add(enterButton);
frame.setSize(450, 300);
frame.getContentPane().add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setButtonAction()
{
boldButton = new JButtonNoFocus(getIcon("bold"));
boldButton.setText(null);
boldButton.setToolTipText("Bold");
boldButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
bold = !bold;
setAttribute();
//refreshOnUpdate();
setButtonState(boldButton, bold);
}
});
italicButton = new JButtonNoFocus(getIcon("italic"));
italicButton.setText(null);
italicButton.setToolTipText("Italic");
italicButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
italic = !italic;
setAttribute();
refreshOnUpdate();
setButtonState(italicButton, italic);
}
});
underlineButton = new JButtonNoFocus(getIcon("underline"));
underlineButton.setText(null);
underlineButton.setToolTipText("Underline");
underlineButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
underline = !underline;
setAttribute();
refreshOnUpdate();
setButtonState(underlineButton, underline);
}
});
ulistButton = new JButtonNoFocus(getIcon("ulist"));
ulistButton.setText(null);
ulistButton.setToolTipText("ListUnordered");
olistButton = new JButtonNoFocus(getIcon("olist"));
olistButton.setText(null);
olistButton.setToolTipText("ListOrdered");
}
Top
10 楼iiisan(學無止境)回复于 2004-09-04 14:57:39 得分 0
public void setComboBoxAction()
{
String[] sizes = new String[]
{"1", "2", "3", "4", "5", "6", "7"};
sizeBox = new JComboBoxNoFocus(sizes);
sizeBox.setSelectedIndex(3);
sizeBox.addActionListener(new ComboBoxListener(this));
String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
fontBox = new JComboBoxNoFocus();
for(int i = 0; i < fonts.length; i++)
{
if(!fonts[i].equals("Dialog") && !fonts[i].equals("DialogInput") && !fonts[i].equals("Monospaced") && !fonts[i].equals("SansSerif") && !fonts[i].equals("Serif"))
{
fontBox.addItem(fonts[i]);
}
}
fontBox.setSelectedIndex(27);
fontBox.addActionListener(new ComboBoxListener(this));
String[] colors = {"Black", "Red", "Green", "Blue", "White", "Yellow"};
colorBox = new JComboBoxNoFocus(colors);
colorBox.addActionListener(new ComboBoxListener(this));
}
public void send(JTextPane input, JTextPane output)
{
String inputText = input.getText();
int messageStart = inputText.indexOf("<body>") + 6;
int messageEnd = inputText.lastIndexOf("</body>");
String message = inputText.substring(messageStart, messageEnd);
String insertString =
"<font color=\"Blue\" face=\"Arial\">User:</font>";
int ol = message.indexOf("<ol>");
int ul = message.indexOf("<ul>");
int insertlocation = ol;
if (ol > ul)
{
if (ul != -1)
insertlocation = ul;
}
else
{
if (ol == -1)
insertlocation = ul;
}
if (insertlocation != -1 &&
insertlocation < message.indexOf("<p>"))
{
message = new StringBuffer(message).
insert(insertlocation - 1, insertString).
toString();
}
else
{
message = new StringBuffer(message).
insert(message.indexOf("<p>") + 3, insertString).
toString();
}
String newMessage1 = message.replaceAll("<p>", "");
String newMessage2 = newMessage1.replaceAll("</p>", "");
newMessage2 = new StringBuffer(newMessage2.trim()).append("<br>").toString();
//System.out.println(newMessage2);
StringBuffer temp = new StringBuffer(output.getText());
int insert = temp.lastIndexOf("</p>");
temp.insert(insert, newMessage2);
//System.out.println(temp);
output.setText(temp.toString());
}
public void setAttribute()
{
if (bold)
tagSet.addAttribute(Tag.B, "bold");
else
tagSet.removeAttribute(Tag.B);
if (italic)
tagSet.addAttribute(Tag.I, "italic");
else
tagSet.removeAttribute(Tag.I);
if (underline)
tagSet.addAttribute(Tag.U, "underline");
else
tagSet.removeAttribute(Tag.U);
fontAttribute.put("size", sizeBox.getSelectedItem().toString());
fontAttribute.put("face", fontBox.getSelectedItem().toString());
fontAttribute.put("color", colorBox.getSelectedItem().toString());
System.out.println(fontAttribute);
if (fontAttribute.size() > 0)
{
Enumeration attribEntries = fontAttribute.keys();
while (attribEntries.hasMoreElements())
{
Object entryKey = attribEntries.nextElement();
Object entryValue = fontAttribute.get(entryKey);
fontSet.addAttribute(entryKey, entryValue);
}
}
System.out.println(fontSet);
tagSet.addAttribute(Tag.FONT, fontSet);
System.out.println(tagSet);
inputPane.setCharacterAttributes(tagSet, false);
//refreshOnUpdate();
System.out.println();
}Top
11 楼iiisan(學無止境)回复于 2004-09-04 14:57:54 得分 0
public void setButtonState(JButton button, boolean state)
{
Border temp = new JButton().getBorder();
if (state)
button.setBorder(BorderFactory.createLoweredBevelBorder());
else
button.setBorder(temp);
}
public ImageIcon getIcon(String iconName)
{
return new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("icons/" + iconName + ".gif")));
}
public JTextPane getTextPane()
{
return inputPane;
}
public ExtendedHTMLDocument getExtendedHtmlDoc()
{
return htmlDocument;
}
public int getCaretPosition()
{
return inputPane.getCaretPosition();
}
public void setCaretPosition(int newPositon)
{
boolean end = true;
do
{
end = true;
try
{
inputPane.setCaretPosition(newPositon);
}
catch (IllegalArgumentException iae)
{
end = false;
newPositon--;
}
}
while(!end && newPositon >= 0);
}
public void refreshOnUpdate()
{
inputPane.setText(inputPane.getText());
}
public static void main(String[] args)
{
try
{
javax.swing.LookAndFeel lnf = new com.sun.java.swing.plaf.windows.WindowsLookAndFeel();
javax.swing.UIManager.setLookAndFeel(lnf);
}
catch (javax.swing.UnsupportedLookAndFeelException ex)
{
}
new Chat();
}
}
class ComboBoxListener implements ActionListener
{
Chat parentChat;
public ComboBoxListener(Chat chat)
{
parentChat = chat;
}
public void actionPerformed(ActionEvent e)
{
parentChat.setAttribute();
parentChat.refreshOnUpdate();
}
}Top
12 楼mq612(五斗米)回复于 2004-09-04 15:14:47 得分 20
textPane.setParagraphAttributes(attrSet, false);Top
13 楼iiisan(學無止境)回复于 2004-09-04 15:20:46 得分 0
不行啊。Top
14 楼mq612(五斗米)回复于 2004-09-04 15:27:55 得分 0
贴简化代码出来,这么长,没时间看Top
15 楼iiisan(學無止境)回复于 2004-09-04 15:32:04 得分 0
你运行一下就行了。Top
16 楼iiisan(學無止境)回复于 2004-09-07 09:09:37 得分 0
没人知道么?Top
17 楼mq612(五斗米)回复于 2004-09-07 13:08:36 得分 0
太长,懒的看,你又不愿意写个简化代码Top
18 楼iiisan(學無止境)回复于 2004-09-07 14:44:58 得分 0
已解决!结贴!Top




