📄 developdocumentviewer.java
字号:
package elegate.cn.edu.nju;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.util.Stack;
/**
* a editor to display the html file
* @author Elegate,elegate@gmail.com
* @author cs department of NJU
*/
public class DevelopDocumentViewer extends JFrame
{
public static void main(String[] args)
{
JFrame f=new DevelopDocumentViewer("doc/index.html");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private static final long serialVersionUID = 1L;
private JEditorPane editorPane;
private Stack<URL> urlStack;
private JButton btnBack;
private JButton btnReload;
private int HEIGHT=600;
private int WIDTH=800;
public DevelopDocumentViewer(final String path)
{
super("Develop Document");
urlStack=new Stack<URL>();
editorPane=new JEditorPane();
editorPane.setEditable(false);
editorPane.addHyperlinkListener(new MyHyperlinkListener());
try
{
File file=new File(path);
editorPane.setPage(file.toURL());
}
catch(Exception e)
{
editorPane.setText("Exception:"+e);
}
btnBack=new JButton("Back");
btnBack.setMnemonic('B');
btnBack.setToolTipText("Back to the last page");
btnBack.addActionListener(new BtnBackL());
btnReload=new JButton("Reload");
btnReload.setMnemonic('R');
btnReload.setToolTipText("Reload the URL");
btnReload.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try
{
File file=new File(path);
urlStack.removeAllElements();
//System.out.println("reload "+file.toURL());
editorPane.setPage(file.toURL());
}
catch(Exception ex)
{
editorPane.setText("Exception:"+ex.toString());
}
}
});
JToolBar toolBar=new JToolBar();
toolBar.add(btnBack);
toolBar.add(btnReload);
this.getContentPane().add(toolBar,BorderLayout.NORTH);
this.getContentPane().add(new JScrollPane(editorPane),BorderLayout.CENTER);
this.pack();
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(this.WIDTH,this.HEIGHT);
this.setLocation((d.width-WIDTH)/2,(d.height-HEIGHT)/2);
this.setVisible(true);
}
class BtnBackL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(urlStack.size()<1)
return;
try
{
URL url=urlStack.pop();
editorPane.setPage(url);
}
catch(Exception ex)
{
editorPane.setText("Exception:"+e);
}
}
}
class MyHyperlinkListener implements HyperlinkListener
{
public void hyperlinkUpdate(HyperlinkEvent e)
{
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
{
try
{
urlStack.push(editorPane.getPage());
editorPane.setPage(e.getURL());
}
catch(IOException ex)
{
//editorPane=new JEditorPane();
//editorPane.setEditable(false);
//editorPane.addHyperlinkListener(new MyHyperlinkListener());
editorPane.setText("Exception:"+e);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -