eclipse中console使用

本文详细探讨了如何在Eclipse的Console中实现链接输出及字体颜色改变的功能,包括创建自定义Hyperlink类以实现链接激活跳转,并通过调整字体样式和颜色来增强显示效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近对eclipse里的console使用比较感兴趣,时间又有闲,大致研究里了下如何在console里输出链接,改变字体颜色
但是下面这段代码并不完善,link只能在document中使用,consoleStream就没法用
MessageConsole console = new MessageConsole("Console Name", null);
   ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
   MessageConsoleStream consoleStream = console.newMessageStream();
   MessageConsoleStream consoleStream2 = console.newMessageStream();
   ConsolePlugin.getDefault().getConsoleManager().showConsoleView(console);
   String url = "http://www.baidu.com";
   String url2 = "http://www.google.cn";
   String time = new java.util.Date().toString();
   //consoleStream.println(time);
   console.getDocument().set(time+"\n");
   //IDocument doc = console.getDocument();
   try {
    console.addHyperlink(new MyHyperLink(url), 0,3);
    console.addHyperlink(new MyHyperLink(url2), 8,20);
    FontData fontData = new FontData();
    fontData.setStyle(SWT.BOLD);
    Font font = new Font(Display.getDefault(),fontData);
    console.setFont(font);
   
    /*Color color = console.getBackground();
    System.out.println("color="+color.toString());*/
   
   } catch (BadLocationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }catch(Exception e){
    e.printStackTrace();
   }
   consoleStream.println("red");
   consoleStream.setColor(new Color(Display.getDefault(),255,0,0));
   consoleStream2.println("blue");
   consoleStream2.setColor(new Color(Display.getDefault(),0,0,255));
/****************************   MyHyperLink   *******************************************/
public class MyHyperLink implements IHyperlink {
private URL url; 
public MyHyperLink(String urlStr){
   try {
    this.url = new URL(urlStr);
   } catch (MalformedURLException e) {
    e.printStackTrace();
    this.url = null;
   }
}
@Override
public void linkActivated() {
   if(url!=null){
    try {
     PlatformUI.getWorkbench().getBrowserSupport().createBrowser(url.toString()).openURL(url);
    } catch (PartInitException e) {
     e.printStackTrace();
    }
   }
}
@Override
public void linkEntered(){System.out.println("link enter");}
@Override
public void linkExited() {System.out.println("link exit");} 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值