2022-02-07 lambda表達(dá)式練習(xí)題

NewLinkRotator.java
https://gist.github.com/augustrobo/d1b22613ff13c8ffa45afd497832b2b5
使用Lambda表達(dá)式作為事件監(jiān)聽(tīng)器。

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); //點(diǎn)擊訪問(wèn)網(wǎng)頁(yè)
        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(); //當(dāng)前線程
        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();
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容