📄 quickhtmlviewer.java
字号:
/**
* Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package jmt.gui.common.startScreen;
import jmt.gui.common.util.BareBonesBrowserLaunch;
import javax.swing.*;
import javax.swing.plaf.basic.BasicHTML;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.io.IOException;
import java.net.URL;
/**
* Created by IntelliJ IDEA.
* User: OrsotronIII
* Date: 14-dic-2004
* Time: 10.03.08
* This class' purpose is to show a simple html document in a JFrame, given a URL.
* The document to be viewed must not contain java applets, scripts and frames,
* otherwise a correct visualization is not assured.
*
* Modified by Bertoli Marco
*/
public class QuickHTMLViewer extends JFrame{
/**creates a new instance of this viewer, given the document's url, and sets up
* visualization.
* @param url: url of document that must be displayed.
*/
public QuickHTMLViewer(URL url){
prepareWindow();
setSize(new Dimension(640,480));
JEditorPane jepHtml = new JEditorPane();
// Relative path for html documents. This is needed for link inside jars
jepHtml.putClientProperty(BasicHTML.documentBaseKey, getClass().getResource("./"));
jepHtml.setEditable(false);
//jepHtml.setOpaque(false);
jepHtml.setContentType("text/html");
try {
jepHtml.setPage(url);
} catch (IOException e) {
jepHtml.setContentType("text/plain");
jepHtml.setText("Couldn't load page: "+url);
}
JScrollPane jsp = new JScrollPane(jepHtml,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.getContentPane().add(jsp);
addHLinkListenerToJep(jepHtml);
}
/**creates a new instance of this viewer, given the document's url, and sets up
* visualization.
* @param url: url of document that must be displayed.
* @param title: title of the window.
*/
public QuickHTMLViewer(URL url, String title){
this(url);
this.setTitle(title);
}
//Sets up this widow
private void prepareWindow(){
try {
UIManager.setLookAndFeel(new com.jgoodies.looks.plastic.Plastic3DLookAndFeel());
} catch (UnsupportedLookAndFeelException ulafe) {
ulafe.printStackTrace();
}
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
centerWindow();
}
//centers this window on the screen
private void centerWindow(){
Toolkit tk=Toolkit.getDefaultToolkit();
//gets dimensions of the screen to center window.
int xOffset = ((int)tk.getScreenSize().getWidth()-getWidth())/2,
yOffset = ((int)tk.getScreenSize().getHeight()-getHeight())/2;
setBounds(xOffset, yOffset, this.getWidth(), this.getHeight());
}
private void addHLinkListenerToJep(JEditorPane jep){
final JEditorPane jePane = jep;
jePane.addHyperlinkListener(new HyperlinkListener(){
public void hyperlinkUpdate(HyperlinkEvent e) {
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED){
try {
if (e.getURL().sameFile(jePane.getPage()))
jePane.setPage(e.getURL());
else
// Open external links in default browser
BareBonesBrowserLaunch.openURL(e.getURL().toString());
jePane.repaint();
} catch (IOException e1) {
jePane.setContentType("text/plain");
jePane.setText("Page Unavailable");
}
}
}
});
}
/** Sets size of this window and centers it on the screen
* @param d new dimension of this window
*/
public void setSize(Dimension d){
super.setSize(d);
centerWindow();
}
/**added for testing purposes*/
/*
public static void main(String[] args){
URL url = null;
url = QuickHTMLViewer.class.getResource("Prova.html");
new QuickHTMLViewer(url).show();
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -