精華區beta Programming 關於我們 聯絡資訊
※ 引述《costbook (CB)》之銘言: : 因為專題需要寫GUI的程式,所以我就 : 把JAVA的書拿出來,翻開老是被我跳過的 : GUI章節,好像是在講swing吧... : 然後看著看著...就睡著了,真的是無聊 : 又繁瑣啊... : 現在只搞出一個frame和一個button : 幸好沒流口水到書上 swing的用法還蠻簡單的 (當然撇開了細節的部分) 反正你就是弄一個TOPLEVEL Widget出來就對了 寫application大概就是JFrame ============================================================ :: import javax.swing.JFrame; public class Sample extends JFrame { public Sample() { this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setSize(400, 300); this.setTitle("TOPLEVEL widget"); this.setVisible(true); } public static void main(String[] args){ new Sample(); } } ============================================================ :: 有了JFrame就可以用他旳ContentPane Q: 為什麼要用ContentPane? A: 因為Swing JFrame的LayoutManager是用在這個"薄薄的一片之上的" 雖然看不見,但是他的好你會明白的 (toplevel widget不是light-weight widget所以會有pane的結構) (其他的元件也就是非heavy-weight widget則靠其他用 Container性質的物件來管理,通常會是JPanel或其他Pane-like物件) ============================================================ :: import java.awt.Container; import java.awt.GridLayout; import javax.swing.JFrame; public class Sample extends JFrame { Container placeholder; public Sample() { /* choice layout */ this.placeholder = this.getContentPane(); placeholder.setLayout(new GridLayout(3, 3)); /* basic settings */ this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setSize(400, 300); this.setTitle("TOPLEVEL widget"); this.setVisible(true); } public static void main(String[] args) { new Sample(); } } ============================================================ :: 把最底層的layout方式決定好之後,就算是打好了"遊樂場"的地基了。 是該玩點什麼。你可以加入任何元件在JFrame之上,呼叫add method即可 ============================================================ :: import java.awt.Container; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; public class Sample extends JFrame { Container placeholder; public Sample() { /* choice layout */ this.placeholder = this.getContentPane(); placeholder.setLayout(new GridLayout(3, 3)); /* add some widget */ JButton[] b = new JButton[9]; for (int i = 0; i < 9; i++) { b[i] = new JButton(""); this.add(b[i]); } /* basic settings */ this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setSize(400, 300); this.setTitle("TOPLEVEL widget"); this.setVisible(true); } public static void main(String[] args) { new Sample(); } } ============================================================ :: 新增了9個按鈕在上面,但是按了也沒什麼事發生。 為了讓他動起來,你還需要學習事件處理。 針對於JButton常用的是ActionListener介面,所有的Event Listener都是介面 因為他保留了實作的空間讓你決定,收到該event notification要做些什麼? ActionListener只有一個mehtod (http://0rz.net/3c1HC) void actionPerformed(ActionEvent e) Invoked when an action occurs. 所以你只要覆寫這一個method,就有一個能用的ActionListener了。 FAQ : 其他的Listener有很多方法,但又不是每一個方法都有用到。 這時候我們可以使用Adapter。Adapter簡單說就是一個轉接插頭。 把三腳的插頭轉成二腳了 (概念上是如此^^) 列幾個例子: MouseListener <--> MouseAdapter KeyListener <--> KeyAdapter *. 如果你用了Adapter卻什麼也沒發生, 那肯定是你method不match父類別的method 或是壓根就沒有加在widget上 ============================================================ :: import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; public class Sample extends JFrame { Container placeholder; public Sample() { /* choice layout */ this.placeholder = this.getContentPane(); placeholder.setLayout(new GridLayout(3, 3)); /* add some widget */ JButton[] b = new JButton[9]; for (int i = 0; i < 9; i++) { b[i] = new JButton(""); b[i].addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { JButton b =(JButton) e.getSource(); b.setText("has been clicked"); }}); this.add(b[i]); } /* basic settings */ this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this.setSize(400, 300); this.setTitle("TOPLEVEL widget"); this.setVisible(true); } public static void main(String[] args) { new Sample(); } } ============================================================ :: 基本上swing入門就這樣而已嚕 其中您還需要的基本知識就是其他的... Layout Manager Listener light-weight Container or likely container (JPanel; JSplitPane ...) other widgets 這些排列組合起來用就綽綽有餘 ============================================================ :: 深入一點,你要先有MVC(隱藏了Observer)的概念。 再來學JTable JList會比較容易上手 最後還有閒的話,可以看看Swing的其他Feature。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 210.59.94.161