我正在构建一个Java Swing应用程序。 在其中我创建了一个JTextPane(displayPane),我将其设置为以HTML格式提取文本。 我希望HTML有点动态,用户可以点击其中一个嵌入的图像并触发事件(在这种情况下,全屏查看图像)。 所以我构建了HTML并设置了窗格的文本,它工作正常。 然后我在窗格中添加了一个HyperlinkListener来收听所需的事件。 HyperlinkEvent有一个.getURL(),即使图像出现在JTextPane中,也会一直返回NULL。
我真的希望getURL()实际工作,这样我就可以把它扔到我创建的“popWindow”(只是创建一个显示图像的大对话框 - 但我需要从HyperlinkEvent获取该图像)
提前感谢您的帮助。
Java CODE:
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>
答案 0 :(得分:3)
好的,我觉得愚蠢...... 我只是更好地阅读文档.... 并注意到这一点: getDescription
public String 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.
REF ::
因此,我没有获取网址,而是获得了描述......就像这样:
he.getDescription();