当前位置:   article > 正文

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

dj-native-swing

最近开发了一个项目,用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();
					}
				}
		});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

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();
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/731745
推荐阅读
相关标签
  

闽ICP备14008679号