从 HTML 链接调用 Java 方法
我目前正在使用 Twitter4J API 用 Java 构建 Twitter 客户端。为了创建 Twitter“时间线”,我目前正在从 Twitter 中提取数据,例如个人资料图像、推文和用户名,然后将它们显示在 JTextPane 中,并使用 HTML 进行格式化。下面的代码示例:
StringBuilder out = new StringBuilder();
try {
List<Status> statuses = HandleEvents.instance().twitter.getHomeTimeline();
out.append("<html>");
for (Status status : statuses)
{
out.append("<img src=\"").append(status.getUser().getProfileImageURL())
.append("\" width=30 height=30><b>").append(status.getUser().getName())
.append(":</b> ").append(status.getText())
.append("<br><br>");
}
out.append("</html>");
tweetsTextPane.setText(out.toString());
这显示了 20 条推文的时间线,由两个换行符分隔。在每条推文下,我想放置一个简单的超链接,称为“Retweet”,它调用我的 Java 方法之一 - HandleEvents.instance().twitter.retweetStatus(status.getId())
我将如何做到这一点?可以直接在标签之间进行调用,还是必须使用 JavaScript 进行调用?
任何帮助将不胜感激。非常感谢。
I'm currently building a Twitter client in Java using the Twitter4J API. To create a Twitter "timeline", I am currently pulling data from Twitter such as profile images, tweets and usernames, then displaying them in a JTextPane, formatted using HTML. Code example below:
StringBuilder out = new StringBuilder();
try {
List<Status> statuses = HandleEvents.instance().twitter.getHomeTimeline();
out.append("<html>");
for (Status status : statuses)
{
out.append("<img src=\"").append(status.getUser().getProfileImageURL())
.append("\" width=30 height=30><b>").append(status.getUser().getName())
.append(":</b> ").append(status.getText())
.append("<br><br>");
}
out.append("</html>");
tweetsTextPane.setText(out.toString());
This displays a timeline of 20 tweets, separated by two line breaks. Under each tweet, I would like to place a simple hyperlink, called "Retweet", which calls one of my Java methods - HandleEvents.instance().twitter.retweetStatus(status.getId())
How would I got about doing this? Can the call be made directly between the tags, or do I have to make the call using JavaScript?
Any help would be appreciated. Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您真的不需要超链接吗?由于它是一个 Swing 应用程序,您只需添加一个
JLabel
,它看起来就像一个超链接(但如果您付出一点努力,它也可以表现得像一个超链接)。在该JLabel
上添加一个鼠标点击监听器,您就可以将当前的处理程序挂接到那里。另一方面,如果您确实需要实际的 HTML 链接,您可以实现自己的 HyperlinkListener。
以下是几个示例:
You don't really need to have a hyperlink do you? Since it's a Swing app you could just add a
JLabel
that only looks like a hyperlink (but if you put in a little effort, it could behave like one as well). Add a listener for mouse clicks on thatJLabel
and you've can hook your current handler there.On the other hand, if you do want actual HTML links, what you can do is implement your own HyperlinkListener.
Here are a couple of examples: