作者qrtt1 (愚者)
看板Programming
标题Re: [闲聊] 看GUI看到睡着
时间Fri Aug 4 22:04:46 2006
※ 引述《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