我有一个正在构建的 Java Swing 应用程序。在其中,我创建了一个 JTextPane (displayPane),我已将其设置为接收 HTML 格式的文本。我希望 HTML 有点动态,用户可以单击其中一个嵌入的图像并触发事件(在这种情况下,全屏查看图像)。所以我构建了 HTML 并设置了窗格的文本,它工作正常。然后我在窗格中添加了一个 HyperlinkListener 来监听所需的事件。HyperlinkEvent 有一个 .getURL(),即使图像出现在 JTextPane 中,它也会不断返回 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>