想输出一个带有超链接的字符串到jTextarea里,然后点击这个超链接会触发某个事件这个怎么写?
解决方案 »
- 请教一个SQL的除法过程----在线等
- doc数据 Base64 解码
- 一个C/S 模式的考试系统 Socket 通信那段研究不通 希望大家能帮我下!
- 一个有趣的问题
- 高手来帮我看看一个子文件的问题。谢谢!
- 编码问题,大家讨论一下
- 一个按扭,按下后可以发出声音
- 大家知道哪有java1.4.2的中文API和CHM啊,小弟初学JAVA,希望得到大家的支持
- Swing界面滚动条不显示问题
- thinking in java and core java的重载问题认我好纳闷
- 如何去掉查询出的hh-mm-ss
- new DataInputStream(new BufferedInputStream(new FileInputStream(absolutepath)))是否将整个文件载入内存的?
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("./classes/test/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);
//字体大小
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();
}
}
全部4个类:一个HTML文件:
HyperLinksDemo.JAVA
package html;import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;//@author Santhosh Kumar T - [email protected]
public class HyperLinksDemo extends JFrame{
public HyperLinksDemo() throws Exception{
super("Hyperlinks Demo - [email protected]");
JEditorPane editor = new JEditorPane();
editor.setPage(getClass().getResource("index.html"));
editor.setEditable(false);
ActionMap actionMap = new ActionMap();
actionMap.put("selectPeople", new SelectPeopleAction());
editor.addHyperlinkListener(new ActionBasedHyperlinkListener(actionMap));
JPanel contents = (JPanel)getContentPane();
contents.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
contents.add(new JScrollPane(editor));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1000, 600);
}
public static void main(String[] args) throws Exception{
new HyperLinksDemo().setVisible(true);
}
}
ActionBasedHyperlinkListener.JAVA
package html;import java.awt.event.ActionEvent;import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;//@author Santhosh Kumar T - [email protected]
public class ActionBasedHyperlinkListener implements HyperlinkListener{
ActionMap actionMap;
public ActionBasedHyperlinkListener(ActionMap actionMap){
this.actionMap = actionMap;
}
public void hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType()!=HyperlinkEvent.EventType.ACTIVATED)
return;
String href = e.getDescription();
Action action = actionMap.get(href);
if(action!=null)
action.actionPerformed(new ActionEvent(e, ActionEvent.ACTION_PERFORMED, href));
}
} HyperlinkActivator.JAVA
package html;import javax.swing.JEditorPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
public class HyperlinkActivator implements HyperlinkListener{
public void hyperlinkUpdate(HyperlinkEvent e){
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
try{
((JEditorPane)e.getSource()).setPage(e.getURL());
} catch(Exception ex){
ex.printStackTrace();
}
}
}
}
import java.awt.event.ActionEvent;
import java.util.StringTokenizer;import javax.swing.AbstractAction;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;//@author Santhosh Kumar T - [email protected]
public class SelectPeopleAction extends AbstractAction{
private final static String people[] = {"Santhosh", "Rick", "Matt", "Lorimer", "Scott"};
public SelectPeopleAction(){
super("selectPeople");
}
public void actionPerformed(ActionEvent e){
HyperlinkEvent hle = (HyperlinkEvent)e.getSource();
try{
Element elem = hle.getSourceElement();
Document doc = elem.getDocument();
int start = elem.getStartOffset();
int end = elem.getEndOffset();
String link = doc.getText(start, end-start);
link = link.equals("contains people") ? "" : link.substring("contains ".length());
JList list = new JList(people);
StringTokenizer stok = new StringTokenizer(link, ", ");
while(stok.hasMoreTokens()){
String token = stok.nextToken();
for(int i = 0; i<list.getModel().getSize(); i++){
if(list.getModel().getElementAt(i).equals(token))
list.getSelectionModel().addSelectionInterval(i, i);
}
}
int response = JOptionPane.showOptionDialog((Component)hle.getSource(), new JScrollPane(list), "People",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,
null, null, null);
if(response==JOptionPane.OK_OPTION){
String newLink = "";
Object selected[] = list.getSelectedValues();
for(int i = 0; i<selected.length; i++){
if(i!=0)
newLink += ", ";
newLink += selected[i];
}
newLink = newLink.length()==0 ? "contains people" : "contains "+newLink;
elem.getDocument().remove(start, end-start);
elem.getDocument().insertString(start, newLink, elem.getAttributes());
}
} catch(BadLocationException ex){
ex.printStackTrace();
}
}
} INDEX.HTML
<html>
<body>
<p>When the newly arrived message arrives<br>
where the From <a href="selectPeople">
contains people</a><br>
delete it.</p>
</body>
</html>