Swing工具包跟Android app开发的UI方面有相似的地方。本文我们来介绍一下Swing。 先用代码就能抛出一个框框,记录一下操作过程
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"); }
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"); ...... }
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; } }
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; } }
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);
/** * 控制面板,提供很多快捷的控制功能 * @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 } }); } ...... }
public class EraseBlockGame extends JFrame{ ...... private ControlBoard controlBoard; public EraseBlockGame(String title){ ...... Container container = getContentPane(); controlBoard = new ControlBoard(this); container.add(controlBoard, BorderLayout.EAST);//添加控制面板 ...... } }
|