2022-02-07 lambda表达式练习题

NewLinkRotator.java
https://gist.github.com/augustrobo/d1b22613ff13c8ffa45afd497832b2b5
使用Lambda表达式作为事件监听器。

image.png
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.net.*;

public class NewLinkRotator extends JFrame
    implements Runnable{
    
    String[] pageTitle = new String[6];
    URI[] pageLink = new URI[6];
    int current = 0;
    Thread runner;
    JLabel siteLabel = new JLabel();
    
    public NewLinkRotator() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(300, 100);
        FlowLayout flo = new FlowLayout();
        setLayout(flo);
        add(siteLabel);
        pageTitle = new String[] {
            "Oracle's Java site",
            "Server Side",
            "JavaWorld",
            "Java in 24 Hours",
            "Sams Publishing",
            "Workbench"
        };
        pageLink[0] = getURI("http://www.oracle.com/technetwork/java");
        pageLink[1] = getURI("http://www.theserverside.com");
        pageLink[2] = getURI("http://www.javaworld.com");
        pageLink[3] = getURI("http://www.java24hours.com");
        pageLink[4] = getURI("http://www.samspublishing.com");
        pageLink[5] = getURI("http://workbench.cadenhead.org");
        Button visitButton = new Button("Visit Site");
        ActionListener act = (event) -> { 
            Desktop desktop = Desktop.getDesktop();
            if (pageLink[current] != null) {
                try{
                    desktop.browse(pageLink[current]);
                    runner = null;
                    System.exit(0);
                } catch (IOException exc) {
                    //
                }
            }
        };
        visitButton.addActionListener(act); //点击访问网页
        add(visitButton);
        setVisible(true);
        start();
    }
    
    private URI getURI(String urlText) {
        URI pageURI = null;
        try {
            pageURI = new URI(urlText);
        } catch (URISyntaxException ex) {
            //
        }
        return pageURI;
    }
    
    public void start() {
        if (runner == null) {
            runner = new Thread(this);
            runner.start();
        }
    }
    
    public void run() {
        Thread thisThread = Thread.currentThread(); //当前线程
        while (runner == thisThread) {
            current++;
            if (current > 5) {
                current = 0;
            }
            siteLabel.setText(pageTitle[current]);
            repaint();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException exc) {
                //
            }
        }
    }
    
    public static void main(String[] arguments) {
        new NewLinkRotator();
    }
}

你可能感兴趣的:(2022-02-07 lambda表达式练习题)