📄 webbrowser.java
字号:
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){
}
}
//fullscreen
else if(e.getSource()==fullscreenItem){
boolean add_button2=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("")){
getHtmlSource(url);
ViewSourceFrame vsframe=new ViewSourceFrame(htmlSource);
vsframe.setBounds(0,0,800,500);
vsframe.setVisible(true);
}
else{
JOptionPane.showMessageDialog(WebBrowser.this,"please input url's adress","network by lijia",JOptionPane.ERROR_MESSAGE);
}
}
//查看网页原代码
else if(e.getSource()==buttonSource||e.getSource()==jurlSource){
url=jurlSource.getText().toString().trim();
if(url.length()>0&&!url.startsWith("http://")){
url="http://"+url;
}
if(!url.equals("")){
getHtmlSource(url);
ViewSourceFrame vsframeSource=new ViewSourceFrame(htmlSource);
vsframeSource.setBounds(0,0,800,500);
vsframeSource.setVisible(true);
}
else{
JOptionPane.showMessageDialog(WebBrowser.this,"please input url's adress","network by lijia",JOptionPane.ERROR_MESSAGE);
}
}
//....................收藏夹
else if(e.getSource()==saveBookMarkItem){
}
//................
//刷新
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){
}
}
}
}
//.............savefile
void saveFile(final String url){
final String linesep=System.getProperty("line.separator");//获得指定键指示的系统属性
chooser1.setCurrentDirectory(new File("."));//设置当前目录
chooser1.setDialogType(JFileChooser.SAVE_DIALOG);//设置此对话框的类型,||指示 JFileChooser 支持 "Save" 文件操作的类型值
chooser1.setDialogTitle("Save as..");//设置显示在 JFileChooser 窗口标题栏的字符串
if(chooser1.showSaveDialog(this)!=JFileChooser.APPROVE_OPTION)// 弹出一个 "Save File" 文件选择器对话框||选择确认(yes、ok)后返回该值
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+" had saved to"+linesep+fileName.getAbsolutePath();//返回抽象路径名的绝对路径名字符串
String dTitle="Save as";
int dType=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog((Component)null,dMessage,dTitle,dType);
}
catch(java.net.MalformedURLException muex){
JOptionPane.showMessageDialog((Component)null,muex.toString(),"network by liujia",JOptionPane.ERROR_MESSAGE);
}
catch(Exception ex){
JOptionPane.showMessageDialog((Component)null,ex.toString(),"network by liujia",JOptionPane.ERROR_MESSAGE);
}
}
};
thread.start();
}
//....................
//getsource
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(),"network by liujia",JOptionPane.ERROR_MESSAGE);
}
catch(Exception e){
JOptionPane.showMessageDialog(WebBrowser.this,e.toString(),"network by liujia",JOptionPane.ERROR_MESSAGE);
}
}
//............
public void hyperlinkUpdate(HyperlinkEvent e)
{
try{
if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED)
jEditorPane1.setPage(e.getURL());
jurl.setText(e.getURL().toString());
history.add(e.getURL().toString());
historyIndex=history.size()-1;
}
catch(Exception ex){
ex.printStackTrace(System.err);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());//使用 LookAndFeel 对象设置当前的默认外观
}
catch(Exception e)
{
}
WebBrowser webBrowser=new WebBrowser();
webBrowser.pack();
webBrowser.setVisible(true);
}
class JEditorPane2 extends JEditorPane implements MouseListener {
private static final long serialVersionUID = -2308615404205560110L;
private JPopupMenu pop = null; // 弹出菜单
private JMenuItem copy = null, paste = null, cut = null; // 三个功能菜单
public JEditorPane2() {
super();
init();
}
private void init() {
this.addMouseListener(this);
pop = new JPopupMenu();
pop.add(copy = new JMenuItem("COPY"));
pop.add(paste = new JMenuItem("PASTE"));
pop.add(cut = new JMenuItem("CUT"));
copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));
copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
paste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
cut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
action(e);
}
});
this.add(pop);
}
/**
* 菜单动作
* @param e
*/
public void action(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals(copy.getText())) { // 复制
this.copy();
} else if (str.equals(paste.getText())) { // 粘贴
this.paste();
} else if (str.equals(cut.getText())) { // 剪切
this.cut();
}
}
public JPopupMenu getPop() {
return pop;
}
public void setPop(JPopupMenu pop) {
this.pop = pop;
}
/**
* 剪切板中是否有文本数据可供粘贴
*
* @return true为有文本数据
*/
public boolean isClipboardString() {
boolean b = false;
Clipboard clipboard = this.getToolkit().getSystemClipboard();
Transferable content = clipboard.getContents(this);
try {
if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) {
b = true;
}
} catch (Exception e) {
}
return b;
}
/**
* 文本组件中是否具备复制的条件
*
* @return true为具备
*/
public boolean isCanCopy() {
boolean b = false;
int start = this.getSelectionStart();
int end = this.getSelectionEnd();
if (start != end)
b = true;
return b;
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
copy.setEnabled(isCanCopy());
paste.setEnabled(isClipboardString());
cut.setEnabled(isCanCopy());
pop.show(this, e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -