?? qinternet.java
字號:
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 + -