我有一个正在构建的Java Swing应用程序。
在其中,我创建了一个JTextPane(displayPane),可以将其设置为采用HTML格式的文本。
我希望HTML有点动态,以便用户可以单击其中一个嵌入式图像并触发事件(在这种情况下,全屏查看图像)。
因此,我构建了HTML并设置了窗格的Text,它可以正常工作。
然后,我将HyperlinkListener添加到窗格中以侦听所需的事件。
HyperlinkEvent具有一个.getURL(),即使图像出现在JTextPane中,该URL仍返回NULL。
我真的希望getURL()实际起作用,以便将其扔到我创建的“popWindow”中(只是创建一个显示图像的大对话框-但我需要从HyperlinkEvent中获取该图像)
在此先感谢您的帮助。
Java代码:
private static void ShowAirportInfo(Airport airport) throws IOException {
HTMLEditorKit htmlKit = new HTMLEditorKit();
displayPane.setEditorKit(htmlKit);
displayPane.setEditable(false);
displayPane.setText(buildHTMLAirportInfo(airport).toString());
displayPane.setCaretPosition(0); //makes the pane go back to the top of the page after rendering the HTML.
displayPane.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent he) {
EventType eType= he.getEventType();
URL url = he.getURL();
if(eType.equals(EventType.ACTIVATED)){
try {
popWindow.setImage(url.getPath());
} catch (IOException ex) {
Logger.getLogger(Phaero.class.getName()).log(Level.SEVERE, null, ex);
}
popWindow.displayPane();
}
}
});
}
放在JTextPane中的HTML:
<html>
<head>
<style type="text/css">body {font-family:verdana,arial,sans-serif;font-size:10pt;margin:30px;}</style></head>
<body>
<h2>General Info</h2>
<div><Table><tr><th align="right">ICAO:</><th align="left">VHHH</></tr><tr><th align="right">Name:</><th align="left">Hong Kong Intl</></tr><tr><th align="right">Location:</><th align="left"> HK</></tr><tr><th align="right">Elevation:</><th align="left">29</></tr><tr><th align="right">Longest Runway:</><th align="left">12469</></tr><th align="right">Lat:</><th align="left">22.30891944</></tr><tr><th align="right">Lon:</><th align="left">113.91460278</></tr></table></div><h2>AQP Status</h2>
<li>Mountainous terrain.</li>
<tr><td><a href="C:\Working\temp\airportDataDirectory\aqp\VHHH\overview\VHHH_overview.jpg"><img src="file:C:\Working\temp\airportDataDirectory\aqp\VHHH\overview\VHHH_overview.jpg" width=300 height=200></td><td><a href="C:\Working\temp\airportDataDirectory\aqp\VHHH\falconview\VHHH_falconview.jpg"><img src="file:C:\Working\temp\airportDataDirectory\aqp\VHHH\falconview\VHHH_falconview.jpg" width=300 height=200></td></tr><h2>Airport Comms</h2>
<li>TWR - 118.4,118.2,118.7,null,null</li>
<li>DEP - 123.8,124.05,null,null,null</li>
<li>CLD - 129.9,124.65,null,null,null</li>
<li>ATIS - 128.2,127.05,null,null,null</li>
<li>APP - 119.1,119.35,null,null,null</li>
<li>AFIS - 122.4,121,null,null,null</li>
<li>GND - 122.55,121.6,null,null,null</li>
<li>CAC - 120.6,null,null,null,null</li>
<li>RDR - 133.7,null,null,null,null</li>
<h2>Runways</h2>
<li>Runway 07R/25L</li>
<li>Dimensions: 12466 x 197</li>
<li>Surface: PERMANENT</li>
<li>Runway Condition: GOOD.</li>
<li>LCN: 87</li>
<li>Max demstrated Wingspan: null</li>
<table><tr><td>Runway: 07R</td><td>Runway: 25L</td></tr><tr><td>Heading: null</td><td>Heading: null</td></tr><tr><td>Slope: 0</td><td>Slope: 0</td></tr><tr><td>Latitude: 22.296202019999999</td><td>Latitude: 22.30743056</td></tr><tr><td>Longitude: null</td><td>Longitude: null</td></tr><tr><td><img src="file:C:\Working\temp\airportDataDirectory\aqp\VHHH\runway\VHHH_rwy07R.jpg" width=200 height=200></td><td><img src="file:C:\Working\temp\airportDataDirectory\aqp\VHHH\runway\VHHH_rwy25L.jpg" width=200 height=200></td></tr></table><li>A - ALSF-2</li>
<li>VASI Type: null</li><li> </li><li> </li><li>Runway 07L/25R</li>
<li>Dimensions: 12469 x 197</li>
<li>Surface: PERMANENT</li>
<li>Runway Condition: GOOD.</li>
<li>LCN: 87</li>
<li>Max demstrated Wingspan: null</li>
<table><tr><td>Runway: 07L</td><td>Runway: 25R</td></tr><tr><td>Heading: null</td><td>Heading: null</td></tr><tr><td>Slope: 0</td><td>Slope: 0</td></tr><tr><td>Latitude: 22.31040556</td><td>Latitude: 22.32163611</td></tr><tr><td>Longitude: null</td><td>Longitude: null</td></tr><tr><td><img src="file:C:\Working\temp\airportDataDirectory\aqp\VHHH\runway\VHHH_rwy07L.jpg" width=200 height=200></td><td><img src="file:C:\Working\temp\airportDataDirectory\aqp\VHHH\runway\VHHH_rwy25R.jpg" width=200 height=200></td></tr></table><li>A - ALSF-2</li>
<li>VASI Type: null</li><li> </li><li> </li><h2>Remarks</h2>
<li>Bird haz.</li>
<li>Rwy 07R/25L clsd Mon, Wed, Thu fr 1730-2359Z. Rwy 07L/25R clsd Tue, Fri, Sat, Sun fr 1730-2359Z.</li>
<li>All rwy are grooved. Ctc Apron Ctl (121.755) for Gnd Emerg.</li>
<li>IFR TAKE-OFF MINIMUMS AND DEPARTURE PROCEDURES: Diverse departure not authorized.</li>
<li>All oil grades avbl.</li>
<li>A1 (Chevron USA C852-25113-380) SP0600-09-D.</li>
</ul>
</body>
</html>
最佳答案
好吧,我觉得很蠢...
我只是更好地阅读了文档...。
并注意到了这一点:
getDescription
公共字符串getDescription()
Get the description of the link as a string. This may be useful if a URL can't be formed from the description, in which case the associated URL would be null.
参考:
http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/event/HyperlinkEvent.html#HyperlinkEvent%28java.lang.Object,%20javax.swing.event.HyperlinkEvent.EventType,%20java.net.URL,%20java.lang.String%29
因此,除了获取URL,我还可以获取描述...,如下所示:
he.getDescription();