📄 webbrowser.java
字号:
try{
jEditorPane1.setPage(URL);
history.add(URL);
historyIndex=history.size()-1;
//设置成非编辑状态jEditorPane1.setEditable(false);
jEditorPane1.revalidate();
}
catch(Exception ex){
JOptionPane.showMessageDialog(WebBrowser.this,"无法打开该搜索页","闫培峰的NetLook QQ:472556417",JOptionPane.ERROR_MESSAGE);
}
}
else if(URL.length()==0){
JOptionPane.showMessageDialog(WebBrowser.this,"请输入链接地址","闫培峰的NetLook QQ:472556417",JOptionPane.ERROR_MESSAGE);
}
}
//另存为...
else if(e.getSource()==picSave||e.getSource()==saveAsItem){
URL=jurl.getText().toString().trim();
if(URL.length()>0&&!URL.startsWith("http://")){
URL="http://"+URL;
}
if(!URL.equals("")){
//保存文件
saveFile(URL);
}
else{
JOptionPane.showMessageDialog(WebBrowser.this,"请输入链接地址","闫培峰的NetLook QQ:472556417",JOptionPane.ERROR_MESSAGE);
}
}
//退出
else if(e.getSource()==exitItem||e.getSource()==picExit){
System.exit(0);
}
//后退
else if(e.getSource()==backItem||e.getSource()==picBack){
historyIndex--;
if(historyIndex<0)
historyIndex=0;
URL=jurl.getText();
try{
//获得history对象中本地址之前访问的地址
URL=(String)history.get(historyIndex);
jEditorPane1.setPage(URL);
jurl.setText(URL.toString());
//设置成非编辑状态jEditorPane1.setEditable(false);
jEditorPane1.revalidate();
}
catch(Exception ex){
}
}
//前进
else if(e.getSource()==forwardItem||e.getSource()==picForward){
historyIndex++;
if(historyIndex>=history.size())
historyIndex=history.size()-1;
URL=jurl.getText();
try{
//获得对象中本地址之后访问的地址
URL=(String)history.get(historyIndex);
jEditorPane1.setPage(URL);
jurl.setText(URL.toString());
//设置成非编辑状态jEditorPane1.setEditable(false);
jEditorPane1.revalidate();
}
catch(Exception ex){
}
}
//全屏
else if(e.getSource()==fullscreenItem){
boolean add_button2=true;
//获得屏幕大小
Dimension size=Toolkit.getDefaultToolkit().getScreenSize();
Container content=Window.getContentPane();
content.add(bar,"North");
content.add(ScrollPane,"Center");
//button2为单击"全屏"后的还原按钮
if(add_button2==true){
bar.add(button2);
}
//为button2添加事件
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
WebBrowser.this.setEnabled(true);
Window.remove(bar);
Window.remove(toolBar);
Window.remove(ScrollPane);
Window.setVisible(false);
ScrollPane.setPreferredSize(new Dimension(100,500));
getContentPane().add(ScrollPane,BorderLayout.SOUTH);
getContentPane().add(bar,BorderLayout.CENTER);
getContentPane().add(toolBar,BorderLayout.NORTH);
bar.remove(button2);
pack();
}
});
Window.setSize(size);
Window.setVisible(true);
}
//查看源文件
else if(e.getSource()==sourceItem||e.getSource()==picView){
URL=jurl.getText().toString().trim();
if(URL.length()>0&&!URL.startsWith("http://")){
URL="http://"+URL;
}
if(!URL.equals("")){
//根据url,获得源代码
getHtmlSource(URL);
//生成显示源代码的框对象
ViewSourceFrame vsframe=new ViewSourceFrame(htmlSource);
vsframe.setBounds(0,0,800,500);
vsframe.setVisible(true);
}
else{
JOptionPane.showMessageDialog(WebBrowser.this,"请输入链接地址","闫培峰的NetLook QQ:472556417",JOptionPane.ERROR_MESSAGE);
}
}
//刷新
else if(e.getSource()==reloadItem){
URL=jurl.getText();
if(URL.length()>0&&URL.startsWith("http://")){
try{
jEditorPane1.setPage(URL);
//设置成非编辑状态jEditorPane1.setEditable(false);
jEditorPane1.revalidate();
}
catch(Exception ex){
}
}
else if(URL.length()>0&&!URL.startsWith("http://")){
URL="http://"+URL;
try{
jEditorPane1.setPage(URL);
//设置成非编辑状态jEditorPane1.setEditable(false);
jEditorPane1.revalidate();
}
catch(Exception ex){
}
}
}
}
/*
**保存文件
*/
void saveFile(final String URL){
final String linesep=System.getProperty("line.separator");
chooser1.setCurrentDirectory(new File("."));
chooser1.setDialogType(JFileChooser.SAVE_DIALOG);
chooser1.setDialogTitle("另存为...");
if(chooser1.showSaveDialog(this)!=JFileChooser.APPROVE_OPTION)
return;
this.repaint();
Thread Thread=new Thread(){
public void run(){
try{
java.net.URL source=new URL(URL);
InputStream in=new BufferedInputStream(source.openStream());
BufferedReader br=new BufferedReader(new InputStreamReader(in));
File fileName=chooser1.getSelectedFile();
FileWriter out=new FileWriter(fileName);
BufferedWriter bw=new BufferedWriter(out);
String line;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
}
bw.flush();
bw.close();
out.close();
String dMessage=URL+"已经被保存至"+linesep+fileName.getAbsolutePath();
String dTitle="另存为";
int dType=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component)null,dMessage,dTitle,dType);
}
catch(java.net.MalformedURLException muex){
JOptionPane.showMessageDialog((Component)null,muex.toString(),"闫培峰的NetLook QQ:472556417",JOptionPane.ERROR_MESSAGE);
}
catch(Exception ex){
JOptionPane.showMessageDialog((Component)null,ex.toString(),"闫培峰的NetLook QQ:472556417",JOptionPane.ERROR_MESSAGE);
}
}
};
Thread.start();
}
/*
**获得源代码
*/
void getHtmlSource(String URL){
String linesep,htmlLine;
linesep=System.getProperty("line.separator");
htmlSource="";
try{
java.net.URL source=new URL(URL);
InputStream in=new BufferedInputStream(source.openStream());
BufferedReader br=new BufferedReader(new InputStreamReader(in));
while((htmlLine=br.readLine())!=null){
htmlSource=htmlSource+htmlLine+linesep;
}
}
catch(java.net.MalformedURLException muex){
JOptionPane.showMessageDialog(WebBrowser.this,muex.toString(),"闫培峰的NetLook QQ:472556417",JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(WebBrowser.this,e.toString(),"闫培峰的NetLook QQ:472556417",JOptionPane.ERROR_MESSAGE);
}
}
/**
**实现监听器接口的hyperlinkUpdate函数
*/
public void hyperlinkUpdate(HyperlinkEvent e){
try{
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
jEditorPane1.setPage(e.getURL());
}
catch (Exception ex){
ex.printStackTrace(System.err);
}
}
/*生成一个IE对象*/
public static void main(String[] args){
try{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e){
}
WebBrowser webBrowser=new WebBrowser();
webBrowser.pack();
webBrowser.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -