Java-Swing内嵌网页判断网址类型

1 篇文章 0 订阅
1 篇文章 0 订阅

最近开发了一个项目,用Swing窗口界面内嵌网页,然后从记事本中获取到网址,页面显示网址内容,然后人工点击单选按钮判断所打开的网站是什么类型,将对应类型的网址添加到对应的记事本中,然后自动获取下一个网址,以此来判断网址的类型,下面是完成后的效果图:
在这里插入图片描述
在这里插入图片描述
1.首先,使用swing内嵌浏览器需要导入3个jar包,第3个根据电脑版本选择

dj-native-swing-swt.jar dj-native-swing.jar org.eclipse.swt.win32.win32.x86_64-4.3.jar

下载链接:https://pan.baidu.com/s/1zZ-BfP4LghyrchNc_ltT5g 密码:z0zq

2.因为网址是从记事本中获取,所以需要一个存放所有需要判断网址的记事本,另外几个记事本根据自己的要求来,用来存放判断过后不同类型的网址,当然是和单选按钮对应的,有多少个单选按钮就是多少个类型的记事本。
在这里插入图片描述

在这里插入图片描述

3.判断完后需要自动跳转记事本中的下一个网址,并将判断的网址加入到对应的记事本中,给单选按钮添加一个监听事件,选完后清空单选按钮的选中状态:clearSelection();

ra.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int flag=JOptionPane.showConfirmDialog(null,"该网站为游戏网站?","温馨提示",JOptionPane.YES_NO_OPTION);
				if(flag==JOptionPane.YES_OPTION){
					i++;
					if(i==x-1) {
						JOptionPane.showConfirmDialog(null,"全部网址已经审核完!","温馨提示",JOptionPane.YES_NO_OPTION);
					}
					try {
						fileInputStream = new FileInputStream(file);
						inputStreamReader = new InputStreamReader(fileInputStream);
						bufferedReader = new BufferedReader(inputStreamReader);
						StringBuffer sb = new StringBuffer();
						String text = bufferedReader.readLine();
						List list = new ArrayList();
						while ((text = bufferedReader.readLine()) != null) {
							// 截取得到的一行数据
							list.add(text);
						}
						a = list.get(i).toString();
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					webBrowser.navigate(a);
						FileWriter fw = null;
						PrintWriter pw = null;
						File file2 = new File("文档\\游戏网站.txt");
					        try { 
					        	fw=new FileWriter(file2,true);
					            pw = new PrintWriter(fw);
					            // 把内容转换成字节数组 
					            pw.println(a);
					            pw.flush();
					        } catch (Exception e) { 
					            e.printStackTrace(); 
					        } finally { 
					            try { 
					                // 关闭输出流 
					            	fw.flush();
					            	pw.close();
					                fw.close();
					            } catch (Exception e) { 
					                e.printStackTrace(); 
					            } 
					        }
					     bg.clearSelection();
					}
				}
		});

4.下面是所有的完整代码

package com.yusheng.xiangmu;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

import org.eclipse.swt.events.DisposeEvent;

import chrriis.common.UIUtils;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserAdapter;
import chrriis.dj.nativeswing.swtimpl.components.WebBrowserEvent;

public class Bro extends JPanel {
	FileInputStream fileInputStream;
	InputStreamReader inputStreamReader;
	BufferedReader bufferedReader;
	int x =0;
	private int i = 0;
	private String a;
	public Bro() {
		super(new BorderLayout());
		JPanel webBrowserPanel = new JPanel(new BorderLayout());
		webBrowserPanel.setBorder(BorderFactory.createTitledBorder(""));
		File file = new File("文档\\网址.txt");
		try {
			fileInputStream = new FileInputStream(file);
			inputStreamReader = new InputStreamReader(fileInputStream);
			bufferedReader = new BufferedReader(inputStreamReader);
			FileReader fr = new FileReader(file);
			BufferedReader br = new BufferedReader(fr);
			StringBuffer sb = new StringBuffer();
			String text = bufferedReader.readLine();
			List list = new ArrayList();
			while(br.readLine() != null) {
				x++;
				}
			while ((text = bufferedReader.readLine()) != null) {
				// 截取得到的一行数据
				list.add(text);
			}
				a = list.get(i).toString();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
				bufferedReader.close();
				inputStreamReader.close();
				fileInputStream.close();
					// 关闭的时候最好按照先后顺序关闭最后开的先关闭所以先关s,再关n,最后关m
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		final JWebBrowser webBrowser = new JWebBrowser();
		webBrowser.navigate(a);
		webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
		add(webBrowserPanel, BorderLayout.CENTER);
		// Create an additional bar allowing to show/hide the menu bar of the web
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 4, 4));
		JRadioButton rab = new JRadioButton("电影网站");
		JRadioButton ra = new JRadioButton("游戏网站");
		JRadioButton rad = new JRadioButton("新闻网站");
		JRadioButton rat = new JRadioButton("打不开");
		ButtonGroup bg = new ButtonGroup();
		bg.add(ra);
		bg.add(rad);
		bg.add(rab);
		bg.add(rat);
		buttonPanel.add(ra);
		buttonPanel.add(rad);
		buttonPanel.add(rab);
		buttonPanel.add(rat);
		ra.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int flag=JOptionPane.showConfirmDialog(null,"该网站为游戏网站?","温馨提示",JOptionPane.YES_NO_OPTION);
				if(flag==JOptionPane.YES_OPTION){
					i++;
					if(i==x-1) {
						JOptionPane.showConfirmDialog(null,"全部网址已经审核完!","温馨提示",JOptionPane.YES_NO_OPTION);
					}
					try {
						fileInputStream = new FileInputStream(file);
						inputStreamReader = new InputStreamReader(fileInputStream);
						bufferedReader = new BufferedReader(inputStreamReader);
						StringBuffer sb = new StringBuffer();
						String text = bufferedReader.readLine();
						List list = new ArrayList();
						while ((text = bufferedReader.readLine()) != null) {
							// 截取得到的一行数据
							list.add(text);
						}
						a = list.get(i).toString();
					} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					webBrowser.navigate(a);
						FileWriter fw = null;
						PrintWriter pw = null;
						File file2 = new File("文档\\游戏网站.txt");
					        try { 
					        	fw=new FileWriter(file2,true);
					            pw = new PrintWriter(fw);
					            // 把内容转换成字节数组 
					            pw.println(a);
					            pw.flush();
					        } catch (Exception e) { 
					            e.printStackTrace(); 
					        } finally { 
					            try { 
					                // 关闭输出流 
					            	fw.flush();
					            	pw.close();
					                fw.close();
					            } catch (Exception e) { 
					                e.printStackTrace(); 
					            } 
					        }
					     bg.clearSelection();
					}
				}
		});
		rad.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int flag =JOptionPane.showConfirmDialog(null,"该网站为新闻网站?","温馨提示",JOptionPane.YES_NO_OPTION);
				if(flag==JOptionPane.YES_OPTION) {
				i++;
				if(i==x-1) {
					JOptionPane.showConfirmDialog(null,"全部网址已经审核完!","温馨提示",JOptionPane.YES_NO_OPTION);
				}
				try {
					fileInputStream = new FileInputStream(file);
					inputStreamReader = new InputStreamReader(fileInputStream);
					bufferedReader = new BufferedReader(inputStreamReader);
					StringBuffer sb = new StringBuffer();
					String text = bufferedReader.readLine();
					List list = new ArrayList();
					while ((text = bufferedReader.readLine()) != null) {
						// 截取得到的一行数据
						list.add(text);
					}
					a = list.get(i).toString();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				webBrowser.navigate(a);
					FileWriter fw = null;
					PrintWriter pw = null;
					File file2 = new File("文档\\新闻网站.txt");
				        try { 
				        	fw=new FileWriter(file2,true);
				            pw = new PrintWriter(fw);
				            // 把内容转换成字节数组 
				            pw.println(a);
				            pw.flush();
				        } catch (Exception e) { 
				            e.printStackTrace(); 
				        } finally { 
				            try { 
				                // 关闭输出流 
				            	fw.flush();
				            	pw.close();
				                fw.close();
				            } catch (Exception e) { 
				                e.printStackTrace(); 
				            } 
				        }
				        bg.clearSelection();
				     }
			}
		});
		rab.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int flag =JOptionPane.showConfirmDialog(null,"该网站为电影网站?","温馨提示",JOptionPane.YES_NO_OPTION);
				if(flag==JOptionPane.YES_OPTION){
				i++;
				if(i==x-1) {
					JOptionPane.showConfirmDialog(null,"全部网址已经审核完!","温馨提示",JOptionPane.YES_NO_OPTION);
				}
				try {
					fileInputStream = new FileInputStream(file);
					inputStreamReader = new InputStreamReader(fileInputStream);
					bufferedReader = new BufferedReader(inputStreamReader);
					StringBuffer sb = new StringBuffer();
					String text = bufferedReader.readLine();
					List list = new ArrayList();
					while ((text = bufferedReader.readLine()) != null) {
						// 截取得到的一行数据
						list.add(text);
					}
					a = list.get(i).toString();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				webBrowser.navigate(a);
					FileWriter fw = null;
					PrintWriter pw = null;
					File file2 = new File("文档\\电影网站.txt");
				        try { 
				        	fw=new FileWriter(file2,true);
				            pw = new PrintWriter(fw);
				            // 把内容转换成字节数组 
				            pw.println(a);
				            pw.flush();
				        } catch (Exception e) { 
				            e.printStackTrace(); 
				        } finally { 
				            try { 
				                // 关闭输出流 
				            	fw.flush();
				            	pw.close();
				                fw.close();
				            } catch (Exception e) { 
				                e.printStackTrace(); 
				            } 
				        }
				        bg.clearSelection();
					}
				}
		});
		rat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int flag =JOptionPane.showConfirmDialog(null,"确定该网站打不开?","温馨提示",JOptionPane.YES_NO_OPTION);
				if(flag==JOptionPane.YES_OPTION){
				i++;
				if(i==x-1) {
					JOptionPane.showConfirmDialog(null,"全部网址已经审核完!","温馨提示",JOptionPane.YES_NO_OPTION);
				}
				try {
					fileInputStream = new FileInputStream(file);
					inputStreamReader = new InputStreamReader(fileInputStream);
					bufferedReader = new BufferedReader(inputStreamReader);
					StringBuffer sb = new StringBuffer();
					String text = bufferedReader.readLine();
					List list = new ArrayList();
					while ((text = bufferedReader.readLine()) != null) {
						// 截取得到的一行数据
						list.add(text);
					}
					a = list.get(i).toString();
				} catch (FileNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				webBrowser.navigate(a);
					FileWriter fw = null;
					PrintWriter pw = null;
					File file2 = new File("文档\\打不开.txt");
				        try { 
				        	fw=new FileWriter(file2,true);
				            pw = new PrintWriter(fw);
				            // 把内容转换成字节数组 
				            pw.println(a);
				            pw.flush();
				        } catch (Exception e) { 
				            e.printStackTrace(); 
				        } finally { 
				            try { 
				                // 关闭输出流 
				            	fw.flush();
				            	pw.close();
				                fw.close();
				            } catch (Exception e) { 
				                e.printStackTrace(); 
				            } 
				        }
				        bg.clearSelection();
				   }
			}
		});
		add(buttonPanel, BorderLayout.SOUTH);
	}

	public static void main(String[] args) {
		UIUtils.setPreferredLookAndFeel();
		NativeInterface.open();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.getContentPane().add(new Bro(), BorderLayout.CENTER);
				frame.setSize(1000, 600);
				frame.setLocationByPlatform(true);
				frame.setVisible(true);
			}
		});
		NativeInterface.runEventPump();
	}
}

第一次写文章,也是一位Java小萌新,希望大佬们多多指教、、

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值