Programming 板


LINE

※ 引述《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







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Gossiping站內搜尋

TOP