Java API 分類導覽 - java.awt.Window toFront()

Window 類別 (class) 的 toFront() 方法 (method) 用來將視窗的在螢幕上的順序往前移。



修飾子與參數
public void toFront()


toFront() 沒有回傳值 (return value) ,也不需要參數 (paramenter) 。


舉例如下
import java.awt.*;
import java.awt.event.*;

public class toBackDemo implements ActionListener {
    Frame frame1, frame2;
    Button button1, button2, button3, button4;
    
    public static void main(String[] args) {
        new toBackDemo();
    }

    public toBackDemo() {
        frame1 = new Frame("AWTDemo1");
        frame1.addWindowListener(new AdapterDemo());
        frame1.setSize(320, 240);
        frame1.setLayout(new FlowLayout());
        frame2 = new Frame("AWTDemo2");
        frame2.addWindowListener(new AdapterDemo());
        frame2.setSize(320, 240);
        frame2.setLayout(new FlowLayout());

        button1 = new Button("toBack");
        button2 = new Button("toFront");
        button3 = new Button("toBack");
        button4 = new Button("toFront");
        
        button1.setActionCommand("b1");
        button1.addActionListener(this);
        button2.setActionCommand("b2");
        button2.addActionListener(this);
        button3.setActionCommand("b3");
        button3.addActionListener(this);
        button4.setActionCommand("b4");
        button4.addActionListener(this);
        
        frame1.add(button1);
        frame1.add(button2);
        frame2.add(button3);
        frame2.add(button4);
        
        frame1.setVisible(true);
        frame2.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if (cmd == "b1") {
            frame1.toBack();
        }
        if (cmd == "b2") {
            frame1.toFront();
        }
        if (cmd == "b3") {
            frame2.toBack();
        }
        if (cmd == "b4") {
            frame2.toFront();
        }
    }
}

class AdapterDemo extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        System.exit(0);
    }
}

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:toBackDemo.java
    功能:示範 Java 程式 
    作者:張凱慶
    時間:西元 2012 年 3 月 */


此例建立兩個視窗,每個視窗各有兩個按鈕
button1 = new Button("toBack");
button2 = new Button("toFront");
button3 = new Button("toBack");
button4 = new Button("toFront");


toBack 執行 Window 類別的 toBack() ,將視窗順序往後移,相對的, toFront 執行 toFront() ,將視窗順序往前移
public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    if (cmd == "b1") {
        frame1.toBack();
    }
    if (cmd == "b2") {
        frame1.toFront();
    }
    if (cmd == "b3") {
        frame2.toBack();
    }
    if (cmd == "b4") {
        frame2.toFront();
    }
}


編譯後執行,點擊 AWTDemo2 的 toBack ,結果如下



中英文術語對照
類別class
方法method
回傳值return value
型態type
物件object
參數parameter


您可以繼續參考
AWT 元件


相關目錄
Java API 分類導覽
Java 教材
首頁


參考資料
http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html

沒有留言: