showdocument.java

来自「几个简单的java学习代码」· Java 代码 · 共 125 行

JAVA
125
字号
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import java.net.URL;
import java.net.MalformedURLException;

public class ShowDocument extends JApplet
                          implements ActionListener {
    URLWindow urlWindow;

    public void init() {
        JPanel panel=new JPanel();

        JButton button = new JButton("Bring up URL window");
        button.addActionListener(this);
        panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)) ;
        panel.add(button, BorderLayout.CENTER );
        panel.setBackground(Color.white );
        this.setContentPane(panel) ;

        urlWindow = new URLWindow(getAppletContext());

        urlWindow.pack();
    }

    public void destroy() {
        urlWindow.setVisible(false);
        urlWindow = null;
    }

    public void actionPerformed(ActionEvent event) {
          urlWindow.setVisible(true);
    }
}

class URLWindow extends JFrame
                implements ActionListener {
    JTextField urlField;
    JComboBox choice;
    String[] iterms = { "(browser's choice)","My Personal Window",
                            "_blank" ,  "_self", "_parent", "_top"  };

    AppletContext appletContext;

    public URLWindow(AppletContext appletContext) {
        super("Show a Document!");

        this.appletContext = appletContext;

        GridBagLayout gridBag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10) );
        panel.setLayout(gridBag);

        JLabel label1 = new JLabel("URL of document to show:",
				 JLabel.RIGHT);
        gridBag.setConstraints(label1, c);
        panel.add(label1);

        urlField = new JTextField("http://www.sun.com/", 40);
        urlField.addActionListener(this);
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        gridBag.setConstraints(urlField, c);
        panel.add(urlField);

        JLabel label2 = new JLabel("Window/frame to show it in:",
				 JLabel.RIGHT);
        c.gridwidth = 1;
        c.weightx = 0.0;
        gridBag.setConstraints(label2, c);
        panel.add(label2);

        choice=new JComboBox(iterms);
        c.fill = GridBagConstraints.NONE;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.anchor = GridBagConstraints.WEST;
        gridBag.setConstraints(choice, c);
        panel.add(choice);

        JButton button = new JButton("Show document");
        button.addActionListener(this);
        c.weighty = 1.0;
        c.ipadx = 10;
        c.ipady = 10;
        c.insets = new Insets(5,0,0,0);
        c.anchor = GridBagConstraints.SOUTH;
        gridBag.setConstraints(button, c);
        panel.add(button);

        this.setContentPane(panel);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                setVisible(false);
            }
        });
    }

    public void actionPerformed(ActionEvent event) {
        System.out.println("how ?");
        String urlString = urlField.getText();
        URL url = null;
        try {
            url = new URL(urlString);
        } catch (MalformedURLException e) {
            System.err.println("Malformed URL: " + urlString);
        }

        if (url != null) {
            if (choice.getSelectedIndex() == 0) {
                appletContext.showDocument(url);
            } else {
                appletContext.showDocument(url,
				  (String)choice.getSelectedItem());
            }
        }
    }
}

⌨️ 快捷键说明

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