web.java

来自「21天学通JAVA2(第三版)(专业参考版)」· Java 代码 · 共 57 行

JAVA
57
字号

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

public class web extends JApplet implements ActionListener {
    WebButton1[] choices = new WebButton1[10];

    public void init() {
        FlowLayout flo = new FlowLayout();
        getContentPane().setLayout(flo);
        for (int i = 0; i < 10; i++) {
            StringBuffer randomDomain = new StringBuffer();
            for (int j = 0; j < 3; j++) {
                // Create a random number from 0 to 25
                double tempValue = Math.random() * 26 - 1;
                int randomNumber = (int) Math.floor( tempValue ) + 1;
                int firstLetter = (int)'a';
                char letter = (char) (firstLetter + randomNumber);
                randomDomain.append(letter);
            }
            String address = new String("http://www." + randomDomain.toString() +
                ".com/");
            choices[i] = new WebButton1("WWW." + randomDomain.toString().toUpperCase() +
                ".COM", address);
            choices[i].addActionListener(this);
            getContentPane().add(choices[i]);
        }
    }

    public void actionPerformed(ActionEvent evt) {
        WebButton1 clicked = (WebButton1)evt.getSource();
        try {
            URL load = new URL(clicked.address);
            getAppletContext().showDocument(load);
        } catch (MalformedURLException e) {
            showStatus("Bad URL:" + clicked.address);
        }
    }
}

class WebButton1 extends JButton {
    String address;

    WebButton1(String iLabel, String iAddress) {
        super(iLabel);
        address = iAddress;
    }
}






⌨️ 快捷键说明

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