⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qinternet.java

📁 java浏览器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package liulan;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import javax.swing.event.*;
import java.net.*;
import java.util.Vector;
import java.io.*;
import javax.swing.border.*;

import javax.swing.filechooser.FileFilter;

public class Qinternet extends JFrame implements HyperlinkListener,
		ActionListener, MouseListener {

	private JToolBar toolbar, toolbar1;

	private JTextField address;

	private URL url;

	private File savefile;

	private String path;

    private String  sc;
	private CardLayout card;

	private JButton[] button;

	private JLabel label;

	private boolean flag;
	
	private boolean tag;
	
	private JButton zhuandao;

	private JPanel panel, panel1;

	private int i;

	private Frame frame;

	protected static JTextArea textarea;

	protected static int cc;

	private JPopupMenu popmenu;

	private Vector<String> vbiaoji;
	
	private Vector<Boolean> vchange;

	private Vector<JComponent> vzujian;

	private Vector<JButton> vbutton;

	private int dangqian = 0;

	protected static JTextField selectfild;

	protected static JFrame dialogframe;

	protected static JButton dialogbutton;

	protected dialog dia;
	
	Mymenu a=new Mymenu();
	
	Myfile b=new Myfile();
	
	public Qinternet() {
		this.setTitle("Q_1.0浏览器");
		this.setSize(500, 500);
		this.setLocation(100, 60);

		frame = new Frame();// windows对话容器
		this.setJMenuBar(a.goup());

		toolbar = new JToolBar();// 工具条

		button = new JButton[5];
		for (int i = 0; i < 5; i++) {
			button[i] = new JButton();
			button[i].addActionListener(this);
			toolbar.add(button[i]);
		}
		// 定义button
		ImageIcon houdui = new ImageIcon("2.jpg");
		ImageIcon qianjin = new ImageIcon("3.jpg");
		ImageIcon shuaxin = new ImageIcon("0.jpg");
		ImageIcon tingzhi = new ImageIcon("1.jpg");

		button[0].setIcon(houdui);
		button[1].setIcon(qianjin);
		button[2].setIcon(tingzhi);
		button[3].setIcon(shuaxin);
		button[4].setText("百度");
		toolbar1 = new JToolBar();// 存放地址栏和转入按钮等。

		label = new JLabel("地址");

		address = new JTextField(20);

		zhuandao = new JButton("转到");
		address.addActionListener(this);
		zhuandao.addActionListener(this);

		toolbar1.add(label);
		toolbar1.add(address);
		toolbar1.add(zhuandao);

		flag = false;

		panel = new JPanel();

		panel1 = new JPanel();
		panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
		panel1.setBorder(new BevelBorder(BevelBorder.RAISED));
		card = new CardLayout();
		panel.setLayout(card);

		Container cp = getContentPane();
		cp.setLayout(null);
		cp.add(toolbar);
		cp.add(toolbar1);

		cp.add(panel);
		cp.add(panel1);
		init();
		/*
		 * 绝对同步,使用vector;
		 */
		vbiaoji = new Vector<String>();
		vzujian = new Vector<JComponent>();
		vbutton = new Vector<JButton>();

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}

		});
		// 调用方法试着是窗口放缩效果出现
		this.addComponentListener(new ComponentAdapter() {
			public void componentResized(ComponentEvent e) {
				init();
			}

		});
		
		popmenu = createPopupMenu(new Object[] { "剪切", "复制", "粘贴", "全选" },
				new ActionListener() {
					public void actionPerformed(ActionEvent ex) {
						JMenuItem im = (JMenuItem) ex.getSource();

						if (im.getText().equals("复制")) {
							copy();
						}
						if (im.getText().equals("剪切")) {
							cut();
						}
						if (im.getText().equals("粘贴")) {
							paste();
						}
						if (im.getText().equals("全选")) {
							selectall();
						}
					}
				});

		selectfild = new JTextField(20);
		dialogbutton = new JButton("查找");
		dialogframe = new JFrame();
		dia = new dialog(dialogframe, "查找", false);
		dia.setLocation(400, 250);
		dia.setFocusable(true);
	}
	/* 随窗口大小改变组件位置*/
	
	public static JPopupMenu createPopupMenu(Object[] item, Object targer) {
		JPopupMenu pMenu = new JPopupMenu();
		for (int i = 0; i < item.length; i++) {
			if (item[i].equals("")) {
				pMenu.addSeparator();
			} else {
				pMenu.add(createMenuItem(item[i], targer));
			}
		}
		return pMenu;
	}
	
	public static JMenuItem createMenuItem(Object item, Object target) {
		JMenuItem r = null;
		if (item instanceof String) {
			r = new JMenuItem((String) item);

		} else if (item instanceof JMenuItem) {
			r = (JMenuItem) item;
		}
		if (target instanceof ActionListener) {
			r.addActionListener((ActionListener) target);

		}
		return r;
	}

	public void init() {
		toolbar.setVisible(false);
		toolbar1.setVisible(false);

		panel1.setVisible(false);
		panel.setVisible(false);
		toolbar.setBounds(0, 0, this.getWidth() - 5, 35);
		toolbar1.setBounds(0, 35, this.getWidth() - 5, 30);
		panel1.setBounds(0, 65, this.getWidth() - 5, 25);
		panel.setBounds(0, 90, this.getWidth() - 5, this.getHeight()
				- panel1.getY() - 85);

		toolbar.setVisible(true);
		toolbar1.setVisible(true);

		panel1.setVisible(true);
		panel.setVisible(true);
		
		tag=false;

        
        
	}

	public void update(String s, URL url) {// 打开网页更新
		if (vbiaoji.indexOf(url.toString()) == -1) {
			
			JButton button = new JButton(s.substring(11, 17));
			panel1.add(button);

			button.addActionListener(this);
			button.setActionCommand("panel1");
			button.addMouseListener(this);
            
            address.setText(url.toString());
            
			JEditorPane editorpan = new JEditorPane();// 可显示各种内容的面板
			JScrollPane scrol = new JScrollPane(editorpan);
			scrol.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			panel.add(url.toString(), scrol);
			card.show(panel, url.toString());

			editorpan.setEditable(flag);
			try {
				editorpan.setPage(url);
			} catch (Exception e) {

			}
			editorpan.addHyperlinkListener(this);
			editorpan.addMouseListener(new MouseAdapter() {
				public void mouseReleased(MouseEvent e) {
					if (e.isPopupTrigger()) {
						popmenu.show(e.getComponent(), e.getX(), e.getY());
					}
				}
			});
			vbutton.add(button);
			vbiaoji.add(url.toString());
			vzujian.add(editorpan);
			dangqian = vbiaoji.size() - 1;
			bianse();
			panel1.setVisible(false);
			panel1.setVisible(true);
			button.setVisible(true);
		
			if((vbutton.size()>1)&&(tag==false))
			{
               vchange.add(dangqian,true);
			}
		} else {
			card.show(panel, url.toString());
			address.setText(url.toString());
			dangqian = vbiaoji.indexOf(url.toString());
			bianse();
		}

	}

	public void  update1(String s, String path) {// 打开文件更新
		if (vbiaoji.indexOf(path) == -1) {
			
			
			File file = new File(path);
			JButton button = new JButton(s);
			button.addActionListener(this);
			panel1.add(button);
			button.setActionCommand("panel1");
			button.addMouseListener(this);
			JTextArea text = new JTextArea();
			text.setEditable(true);
			JScrollPane scrol = new JScrollPane(text);
			scrol.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

			panel.add(path, scrol);
			card.show(panel, path);
			try {

				RandomAccessFile random = new RandomAccessFile(file, "r");
				int i = 0;

				while ((i = random.read()) != -1) {
					String m = String.valueOf((char) i);
					text.append(m);
				}
				random.seek(0);
				random.close();

			} catch (Exception e) {

			}
			text.addMouseListener(new MouseAdapter() {
				public void mouseReleased(MouseEvent e) {
					if (e.isPopupTrigger()) {
						popmenu.show(e.getComponent(), e.getX(), e.getY());
					}
				}
			});
			vbutton.add(button);
			vbiaoji.add(path);
			vzujian.add(text);
			dangqian = vbiaoji.size() - 1;
			bianse();
			address.setText(path);
			panel1.setVisible(false);
			panel1.setVisible(true);
			button.setVisible(true);
			
			if((vbutton.size()>1)&&(tag==true))
			{
              vchange.add(dangqian,true);
			}
		} else {
			card.show(panel, path);
			dangqian = vbiaoji.indexOf(path);
			bianse();
			address.setText(path);
		}

	}

	public void actionPerformed(ActionEvent e) {// 连接地址
		Object s = e.getSource();
		if (s == address || s == zhuandao) {
			String dizhi = address.getText();
			if (dizhi.startsWith("http://")) {
				try {
					url = new URL(dizhi);
					update(url.toString(), url);

				} catch (Exception ex) {
				}
			} else {
				try {
					url = new URL("http://" + dizhi);
					address.setText("http://" + dizhi);
					update(url.toString(), url);
					System.out.println(url.getContent().toString());

				} catch (Exception ex) {
				}
			}
		}
		if (s == button[0]) {
			if (dangqian> 0) {
				try {					
					dangqian=dangqian-1;
					i=dangqian;
					JButton b=vbutton.get(dangqian);
					xianshi(b);
	
				} catch (Exception ee) {
				}
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -