Java Swing intro
如果有Android app開發經驗,快速上手Swing不是問題。UI方面有相似的地方。
簡單的幾行代碼就能抛出一個框框,記錄一下操作過程
1.先顯示一個框框
EraseBlockGame類是主類,包含了main入口,繼承自 JFrame
public class EraseBlockGame extends JFrame{
......
public EraseBlockGame(String GameTitle){ // 構造方法
super(GameTitle);
setSize(408, 640);
setLocationRelativeTo(null);// place in the center of screen
......
setVisible(true);
}
}
設定視窗大小,設定視窗在螢幕上的位置,視窗可見
public static void main(String args[]){
EraseBlockGame e = new EraseBlockGame("Erase Block Game");
}
運作一下程式,彈出一個視窗;視窗名稱為Erase Block Game
2.菜單欄
菜單欄有菜單按鈕,以及菜單選項
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
- JMenuBar 是整個菜單
- JMenu 是菜單欄上的單個按鈕
- JMenuItem 點開單個餐單鍵,彈出的子選項item
public class EraseBlockGame extends JFrame{
private static final long serialVersionUID = 1L;
private JMenuBar menuBar = new JMenuBar();
private JMenu mGame = new JMenu("Game");
private JMenuItem miNewGame = new JMenuItem("New game");
private JMenuItem miExit = new JMenuItem("Exit");
......
}
如果多幾個選項,總是new似乎不大好,用簡單工廠來代替new
定義JMenuFactory,裡面有建立JMenu的方法
package com.rust.util;
import javax.swing.JMenu;
public class JMenuFactory {
JMenu menu;
public JMenuFactory(){
}
public JMenu createMenu(String title){
JMenu menu = new JMenu(title);
return menu;
}
}
同樣定義JMenuItemFactory
package com.rust.util;
import javax.swing.JMenuItem;
public class JMenuItemFactory {
JMenuItem item;
public JMenuItemFactory(){
}
public JMenuItem createMenuItem(String title){
item = new JMenuItem(title);
return item;
}
}
原來的new就可以替換為
private JMenu mGame;
private JMenu mControl;
private JMenu mInfo;
private JMenuItem miNewGame;
private JMenuItem miExit;
......
mGame = menuFactory.createMenu("Game");
mControl = menuFactory.createMenu("Control");
mInfo = menuFactory.createMenu("Info");
miNewGame = miFactory.createMenuItem("New game");
miExit = miFactory.createMenuItem("Exit");
在構造函數中給菜單item添加ActionListener,和Android app的Button差不多
miNewGame.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
miExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
mGame.add(miNewGame);//這裡添加的順序就是排列的順序
mGame.add(miExit);//往menu中添加子項
menuBar.add(mGame);
menuBar.add(mControl);//這裡添加的順序就是排列的順序
setJMenuBar(menuBar);
如此看來,Swing活在了Android中
3.放置按鈕
此時界面上隻有一些菜單按鍵,多擺幾個按鈕上去看看
定義一個控制台類ControlBoard 繼承自 JPanel
/**
* 控制台,提供很多快捷的控制功能
* @author Rust Fisher
*/
public class ControlBoard extends JPanel{
private JButton btnStart;
private JButton btnStop;
private JButton btnPause;
private JButton btnReset;
private JButton btnExit;
/*定義一個按鈕區域areaButton,用來存放btn*/
private JPanel areaButton = new JPanel(new GridLayout(5, 1));
private EraseBlockGame game;
/*按鈕區域的框框*/
private Border border = new EtchedBorder(EtchedBorder.RAISED, Color.WHITE,Color.gray);
public ControlBoard(final EraseBlockGame game){
setLayout(new GridLayout(3,1,0,1));
this.game = game;//用于控制
btnStart = new JButton("Start");
btnStart.setEnabled(true);
btnStop = new JButton("Stop");
btnStop.setEnabled(false);
btnPause = new JButton("Pause");
btnPause.setEnabled(false);
btnReset = new JButton("Reset");
btnReset.setEnabled(true);
btnExit = new JButton("Exit");
btnExit.setEnabled(true);
areaButton.add(btnStart);
areaButton.add(btnPause);
areaButton.add(btnStop);
areaButton.add(btnReset);
areaButton.add(btnExit);
areaButton.setBorder(border);
add(areaButton);// 把按鈕區添加到控制台上
btnStart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// go go go
}
});
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);//886
}
});
}
......
}
在EraseBlockGame類裡加載按鈕區域
public class EraseBlockGame extends JFrame{
......
private ControlBoard controlBoard;
public EraseBlockGame(String title){
......
Container container = getContentPane();
controlBoard = new ControlBoard(this);
container.add(controlBoard, BorderLayout.EAST);//添加控制台
......
}
}
于是按鈕就被裝到程式上了
其他的就先不糾結了,Swing了解個大概就好;可以多看看android開發