The Swing toolkit is similar to the Android app development UI. This article introduces Swing.
You can use the code to throw a box and record the operation process.
1. Display a box first
The EraseBlockGame class is a main class that contains the main entry and inherits from JFrame.
Public class EraseBlockGame extends JFrame {...... public EraseBlockGame (String GameTitle) {// Constructor super (GameTitle); setSize (408,640); setLocationRelativeTo (null); // place in the center of screen ...... setVisible (true );}}
Set the window size, set the position of the window on the screen, and the window is visible.
Public static void main (String args []) {EraseBlockGame e = new EraseBlockGame ("Erase Block Game ");}
Run the program and a window is displayed. The window name is Erase Block Game.
2. Menu bar
The menu bar includes menu buttons and menu options.
Import javax. swing. JFrame; import javax. swing. JMenu; import javax. swing. JMenuBar; import javax. swing. JMenuItem; JMenuBar is the entire menu. JMenu is a single button on the menu bar. JMenuItem opens a single meal single key. The pop-up sub-option itempublic 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 ");......}
If there are a few more options, it always seems that new is not very good. Replace new with a simple factory.
Define JMenuFactory, which contains the JMenu creation method.
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 ;}}
Similarly, JMenuItemFactory is defined
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 ;}}
The original new can be replaced
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 ");
Add ActionListener to the menu item in the constructor, which is similar to the Android app Button.
MiNewGame. addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {}}); miExit. addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {System. exit (0) ;}}); mGame. add (miNewGame); // The order added here is the ordered mGame. add (miExit); // add the subitem menuBar to the menu. add (mGame); menuBar. add (mControl); // The order added here is the ordered setJMenuBar (menuBar );
In this case, Swing lives in Android.
3. Place Button
At this time, there are only some menu buttons on the interface. Put a few more buttons to see them.
Define a control panel class ControlBoard inherited from JPanel
/*** Control panel provides many convenient control functions * @ author Rust Fisher */public class ControlBoard extends JPanel {private JButton btnStart; private JButton btnStop; private JButton btnPause; private JButton btnReset; private JButton btnExit;/* defines a button area areaButton to store btn */private JPanel areaButton = new JPanel (new GridLayout (5, 1 )); private EraseBlockGame game;/* box in the button area */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; // used to control 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); // add the button area to the control panel. addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {// go}); btnExit. addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {System. exit (0); // 886 }});}......}
Load the button area in the EraseBlockGame class
Public class EraseBlockGame extends JFrame {...... private ControlBoard controlBoard; public EraseBlockGame (String title ){...... container container = getContentPane (); controlBoard = new ControlBoard (this); container. add (controlBoard, BorderLayout. EAST); // add control panel ......}}
The button is mounted to the program.
The rest will not be tangled first, and Swing should be familiar with it. You can take a look at android development.