In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how java to achieve Tetris game, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
1. Functional requirements 2. Software functional architecture figure 3. Interface design 4. Program logic figure 5. Implementation code
Create the control panel and add the button initialization interface, add event monitoring to create the box to realize the other block to operate the game main class, and realize the game control.
Functional requirements
1, in the two-dimensional plane with a variety of randomly generated blocks, each full row to eliminate one line, when the top is reached, the game is over. 2. Players use the arrow keys to control the box rotation, left shift, right shift and straight fall. 3. Each type of square has a color. 4. Players can give scores in the process of playing. The scores are determined by the type of squares, and the scores are added to the total score for each square accumulated. 5. The game has the control of pause, start, end, game rules and game description.
Interface design
The game main class inherits the JFrame class and is responsible for the global control of the game. Contains: (1) an instance object of the GameCanvas canvas class (used to store small squares) (2) an object that holds the (RussiaBlock) instance of the current active block (3) an object that holds the current control panel (ControlPanel) instance
Code implementation
1. Create a control panel and add buttons
Public class ControlPanel extends JPanel {private static final long serialVersionUID = 3900659640646175724L / used to indicate the compatibility of classes between different versions / / definition text box private JTextField tfLevel = new JTextField ("" + RussiaBlocksGame.DEFAULT_LEVEL), tfScore = new JTextField ("0"), tfTime = new JTextField ("") / / define buttons private JButton btPlay = new JButton ("start"), btPause = new JButton ("pause"), btStop = new JButton ("stop"), btTurnLevelUp = new JButton ("increase difficulty"), btTurnLevelDown = new JButton ("reduce difficulty"); / / create panel container class as boundary layout BorderLayout () private JPanel plTip = new JPanel (new BorderLayout ()); private TipPanel plTipBlock = new TipPanel () / / create the information panel container class as grid layout GridLayout (4jue 1), 4 rows and 1 column private JPanel plInfo = new JPanel (new GridLayout (4p 1)); / / create button panel container class as grid layout GridLayout (6p 1) private JPanel plButton = new JPanel (new GridLayout (6p 1)); / / define time class / / private Timer timer / / create component border Boreder,EtchedBorder "embossed" border private Border border = new EtchedBorder (EtchedBorder.RAISED, Color.white, new Color (148,145,140)) / * * Constructor of the control panel class * / public ControlPanel (final RussiaBlocksGame game) {/ / TODO Auto-generated constructor stub / / to create a grid layout, GridLayout (3meme 1), specify a grid layout for the number of rows and columns, and specify a horizontal and vertical grid gap setLayout (new GridLayout) / / add components plTip.add (new JLabel ("next square"), BorderLayout.NORTH); plTip.add (plTipBlock); plTip.setBorder (border); plInfo.add (new JLabel ("difficulty factor"); plInfo.add (tfLevel); plInfo.add (new JLabel (score)); plInfo.add (tfScore); plInfo.setBorder (border); plButton.add (btPlay); btPlay.setEnabled (true); plButton.add (btPause); btPause.setEnabled (false); plButton.add (btStop) BtStop.setEnabled (false); plButton.add (btTurnLevelDown); plButton.add (btTurnLevelUp); plButton.setBorder (border); tfLevel.setEnabled (false); tfScore.setEnabled (false); tfTime.setEnabled (false); add (plTip); add (plInfo); add (plButton);}
2. Initialization interface
/ / initialize menu bar private JMenuBar bar = new JMenuBar (); private JMenu mGame = new JMenu ("game"), mControl = new JMenu ("control"), mInfo = new JMenu ("help") Private JMenuItem miNewGame = new JMenuItem ("New Game"), miSetBlockColor = new JMenuItem ("set Square Color..."), miSetBackColor = new JMenuItem ("set background Color..."), miTurnHarder = new JMenuItem ("increase Game difficulty"), miTurnEasier = new JMenuItem ("reduce Game difficulty"), miExit = new JMenuItem ("exit"), miPlay = new JMenuItem ("start"), miPause = new JMenuItem ("pause"), miResume = new JMenuItem ("resume") MiStop = new JMenuItem ("end the game"), miRule = new JMenuItem ("rules of the game"), miAuthor = new JMenuItem ("about the game") / * create and set window menu * / private void creatMenu () {bar.add (mGame); bar.add (mControl); bar.add (mInfo); mGame.add (miNewGame); mGame.addSeparator (); mGame.add (miSetBlockColor); mGame.add (miSetBackColor); mGame.addSeparator (); mGame.add (miTurnHarder); mGame.add (miTurnEasier); mGame.addSeparator (); mGame.add (miExit); mControl.add (miPlay); miPlay.setEnabled (true); mControl.add (miPause) MiPause.setEnabled (false); mControl.add (miResume); miResume.setEnabled (false); mControl.add (miStop); miStop.setEnabled (false); mInfo.add (miRule); mInfo.add (miAuthor); setJMenuBar (bar); miNewGame.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {stopGame (); reset (); setLevel (DEFAULT_LEVEL);}})) / / set square color miSetBlockColor.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {Color newFrontColor = JColorChooser.showDialog (RussiaBlocksGame.this, "set square color", canvas.getBackgroundColor ()); if (newFrontColor! = null) {canvas.setBlockColor (newFrontColor);}) / / set the background color miSetBackColor.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {Color newBackColor = JColorChooser.showDialog (RussiaBlocksGame.this, "set background color", canvas.getBackgroundColor ()); if (newBackColor! = null) {canvas.setBackgroundColor (newBackColor);}); / / define the "about" function of the menu bar and pop up a confirmation box. MiAuthor.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {JOptionPane.showMessageDialog (null, "Software Engineering (4) Class\ n3115005372\ nYang Yujie\ nThe right of interpretation of ©belongs to Yang Yujie", "about Tetris-2016", 1);}}) / / the rules of the game state that miRule.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {JOptionPane.showMessageDialog (null), "plates of different shapes composed of small squares fall one after another from the top of the screen.\ nplayers can make one or more pieces at the bottom of the screen by adjusting the position and orientation of the plates. These complete stripes will disappear, making room for new\ nfalling plates, and at the same time, players will be rewarded with points. \ nThe squares that have not been eliminated continue to pile up, and once they reach the top of the screen, the player loses and the game is over. " , "rules of the Game", 1);}}); / / increase the difficulty of miTurnHarder.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {int curLevel = getLevel (); if (! playing & & curLevel)
< MAX_LEVEL) { setLevel(curLevel + 1); } } }); // 减少难度 miTurnEasier.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int curLevel = getLevel(); if (!playing && curLevel >1) {setLevel (curLevel-1);}); / / exit button action response miExit.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent e) {System.exit (0);}});}
3. Add event listener
/ * * add event listener * / / * * start the game * / btPlay.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent ae) {/ / TODO Auto-generated method stub game.playGame ();}}) / * * suspend the game * / btPause.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent ae) {/ / TODO Auto-generated method stub if (btPause.getText (). Equals ("pause")) {game.pauseGame ();} else {game.resumeGame ();}) / * * stop the game * / btStop.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent ae) {/ / TODO Auto-generated method stub game.stopGame ()}); / * * increase the difficulty * / btTurnLevelUp.addActionListener (new ActionListener () {@ Override public void actionPerformed (ActionEvent ae) {/ / TODO Auto-generated method stub try {int level = Integer.parseInt (tfLevel.getText ()); if (level)
< RussiaBlocksGame.MAX_LEVEL) { tfLevel.setText("" + (level + 1)); } } catch (NumberFormatException e) { // TODO: handle exception requestFocus(); } } }); /* * 降低游戏难度 */ btTurnLevelDown.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { // TODO Auto-generated method stub try { int level = Integer.parseInt(tfLevel.getText()); if (level >1) {tfLevel.setText ("" + (level-1));}} catch (NumberFormatException e) {/ / TODO: handle exception requestFocus ();}); / * * is mainly used to solve the problem of changing the size of the Jframe window, the position of the components in it will also move adaptively. * / addComponentListener (new ComponentAdapter () {public void componentResized (ComponentEvent ce) {plTipBlock.fanning ();}})
4. Create a square
Public class ErsBox implements Cloneable {private boolean isColor; private Dimension size = new Dimension (); / / the Dimension class encapsulates the width and height of components in a single object (accurate to integers) / * * does the checkered class constructor isColor color this grid with foreground color, true foreground color, false background color * / public ErsBox (boolean isColor) {/ / TODO Auto-generated constructor stub this.isColor = isColor } / * * set the color of the box true foreground color, false background color * / public void setColor (boolean isColor) {/ / TODO Auto-generated method stub this.isColor = isColor;} / * * get the size of the box * / public Dimension getSize () {return size;} / * * set the size of the box, * * @ param size Dimension, size of the box * / public void setSize (Dimension size) {this.size = size } / * * overwrite object clone () of object to clone * / public Object clone () {Object cloned = null; try {cloned = super.clone ();} catch (Exception ex) {/ / TODO: handle exception ex.printStackTrace ();} return cloned } / * * is this box represented by foreground color * * @ return boolean, true by foreground color, false by background color * / public boolean isColorBox () {/ / TODO Auto-generated method stub return isColor;}}
5. Realize the operation of each other's block
Public class ErsBlock extends Thread {/ / A block occupies 4 rows public static final int BOXES_ROWS = 4; / / A block occupies 4 columns public static final int BOXES_COLS = 4; / * * smooth factors for upgrade changes to avoid nearly doubling the speed difference between the last levels * / public final static int LEVEL_FLATNESS_GENE = 3 / / between the two similar levels, the time difference of each falling row of blocks (milliseconds) public final static int BETWEEN_LEVELS_DEGRESS_TIME = 50; / / the number of squares is 7 public final static int BLOCK_KIND_NUMBER = 7; / / the type of square inversion state of each style is 4 public final static int BLOCK_STASTUS_NUMBER = 4 / * corresponds to 28 states of 7 models * / public final static int [] [] STYLES = {/ / 28 states {0x0f00, 0x4444, 0x0f00, 0x4444}, / / four states of long strip {0x04e0, 0x0464, 0x00e4, 0x04c4}, / / four states of T type {0x4620, 0x6c00, 0x4620, 0x6c00}, / / four states of inverse Z type {0x2640, 0xc600, 0x2640, 0xc600} / / four states of type Z {0x6220, 0x1700, 0x2230, 0x0740}, / / four states of type 7 {0x6440, 0x0e20, 0x44c0, 0x8e00}, / / four states of inverse type 7 {0x0660, 0x0660, 0x0660, 0x0660}, / / four states of blocks} Private GameCanvas canvas; private ErsBox [] [] boxes = new ErsBox [box _ ROWS] [BOXES_COLS]; private int style, y, x, level; private boolean pausing = false, moving = true / * * construct a function that produces a specific block style as a block style, corresponding to a y starting position in STYLES, a coordinate line * x starting position in the upper left corner in canvas, and a coordinate column in canvas in the upper left corner. The game level controls the descending speed canvas panel * / public ErsBlock (int style, int y, int x, int level, GameCanvas canvas) {this.style = style; this.y = y; this.x = x; this.level = level This.canvas = canvas; int key = 0x8000; for (int I = 0; I
< boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { boolean isColor = ((style & key) != 0); boxes[i][j] = new ErsBox(isColor); key >> = 1;}} display ();} / * * the run () function of the thread class overrides the block until the block can no longer fall * / @ Override public void run () {while (moving) {try {sleep (BETWEEN_LEVELS_DEGRESS_TIME * (RussiaBlocksGame.MAX_LEVEL-level + LEVEL_FLATNESS_GENE));} catch (InterruptedException ie) {ie.printStackTrace () The moving after} / / indicates that the moving has not been changed during the 100ms of waiting. If (! pausing) {moving = (moveTo (y + 1, x) & & moving);} / * * the block moves one grid to the left * / public void moveLeft () {/ / TODO Auto-generated method stub moveTo (y, x-1) } / * * Block right move one grid * / public void moveRight () {/ / TODO Auto-generated method stub moveTo (y, x + 1);} / * Block move down one grid * / public void moveDown () {/ / TODO Auto-generated method stub moveTo (y + 1, x);} / * * Block deformation * / public void turnNext () {for (int I = 0; I)
< BLOCK_KIND_NUMBER; i++) { for (int j = 0; j < BLOCK_STASTUS_NUMBER; j++) { if (STYLES[i][j] == style) { int newStyle = STYLES[i][(j + 1) % BLOCK_STASTUS_NUMBER]; turnTo(newStyle); return; } } } } public void startMove() { // TODO Auto-generated method stub pausing = false; moving = true; } /* * 暂定块的下落,对应暂停游戏 */ public void pauseMove() { // TODO Auto-generated method stub pausing = true; // moving = false; } /* * 继续块的下落,对应游戏的继续 */ public void resumeMove() { // TODO Auto-generated method stub pausing = false; moving = true; } /* * 停止块的下落,对应游戏的停止 */ public void stopMove() { // TODO Auto-generated method stub pausing = false; moving = false; } /* * 将当前块从画布的对应位置移除,要等到下次重画画布时才能反映出来 */ private void erase() { for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { if (boxes[i][j].isColorBox()) { ErsBox box = canvas.getBox(i + y, j + x); if (box == null) { continue; } box.setColor(false); } } } } /* * 让当前块放置在画布的对应位置上,要等到下次重画画布时,才能看见 */ private void display() { for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { if (boxes[i][j].isColorBox()) { ErsBox box = canvas.getBox(i + y, j + x); if (box == null) { continue; } box.setColor(true); } } } } /** * 当前块能否移动到newRow/newCol 所指定的位置 * * @param newRow int,目的地所在行 * @param newCol int,目的地所在列 * @return boolean,true-能移动,false-不能移动 */ public boolean isMoveAble(int newRow, int newCol) { erase(); for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { if (boxes[i][j].isColorBox()) { ErsBox box = canvas.getBox(i + newRow, j + newCol); if (box == null || (box.isColorBox())) { display(); return false; } } } } display(); return true; } /* * 将当前块移动到newRow/newCol所指定位置 newRow 目的所在行 newCol 目的所在列 boolean, true 能移动,false * 不能移动 */ private synchronized boolean moveTo(int newRow, int newCol) { if (!isMoveAble(newRow, newCol) || !moving) { return false; } erase(); y = newRow; x = newCol; display(); canvas.repaint(); return true; } /* * 当前块能否变成newStyle所指定的样式,主要考虑 边界,以及被其他块挡住不能移动的情况 newStyle,希望改变的样式 boolean true * 能改变,false不能改变 */ private boolean isTurnAble(int newStyle) { int key = 0x80000; erase(); for (int i = 0; i < boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { if ((newStyle & key) != 0) { ErsBox box = canvas.getBox(i + y, j + x); if (box == null || (box.isColorBox())) { display(); return false; } } key >> = 1;}} display (); return true;} / * * change the current block into the block style newStyle specified by newStyle. The style you want to change corresponds to a successful change of one of the true in the STYLES. False change failed * / private boolean turnTo (int newStyle) {/ / TODO Auto-generated method stub if (! isTurnAble (newStyle) | |! moving) {return false;} erase (); int key = 0x8000; for (int I = 0; I)
< boxes.length; i++) { for (int j = 0; j < boxes[i].length; j++) { boolean isColor = ((newStyle & key) != 0); boxes[i][j].setColor(isColor); key >> = 1;}} style = newStyle; display (); canvas.repaint (); return true;}}
6. the main class of the game to realize the control of the game
Public RussiaBlocksGame (String title) {super (title); / / set title setSize (500,600); / / set window size setLocationRelativeTo (null); / / set window center creatMenu (); Container container = getContentPane (); / / create menu bar container.setLayout (new BorderLayout (6,0)); / / set window layout manager canvas = new GameCanvas (20,15); / / create new game canvas ctrlPanel = new ControlPanel (this) / / create a new control panel container.add (canvas, BorderLayout.CENTER); / / add canvas container.add (ctrlPanel, BorderLayout.EAST) on the left; / / add control panel / / register window events on the right. When the close button is clicked, the game ends and the system exits. AddWindowListener (new WindowAdapter () {@ Override public void windowClosing (WindowEvent we) {stopGame (); System.exit (0);}}); / / automatically resize the grid according to the window size addComponentListener (new ComponentAdapter () {@ Override public void componentResized (ComponentEvent ce) {canvas.adjust ();}}); setVisible (true); canvas.adjust () } / * reset the game * / public void reset () {/ / reset the control panel to ctrlPanel.setPlayButtonEnable (true); ctrlPanel.setPauseButtonEnable (false); ctrlPanel.setPauseButtonLabel (true); ctrlPanel.setStopButtonEnable (false); ctrlPanel.setTurnLevelDownButtonEnable (true); ctrlPanel.setTurnLevelUpButtonEnable (true); miPlay.setEnabled (true); miPause.setEnabled (false); miResume.setEnabled (false); miStop.setEnabled (false); ctrlPanel.reset (); canvas.reset () } / * determine whether the game is still in progress * * @ return boolean,true-still running, false- has stopped * / public boolean isPlaying () {return playing;} / * get the currently active block * * @ return ErsBlock, reference to the current active block * / public ErsBlock getCurBlock () {return block } / * get the current canvas * * @ return GameCanvas, reference to the current canvas * / public GameCanvas getCanvas () {return canvas;} / * start the game * / public void playGame () {play (); ctrlPanel.setPlayButtonEnable (false); ctrlPanel.setPauseButtonEnable (true); ctrlPanel.setPauseButtonLabel (true); ctrlPanel.setStopButtonEnable (true); ctrlPanel.setTurnLevelDownButtonEnable (false); ctrlPanel.setTurnLevelUpButtonEnable (false); miStop.setEnabled (true); miTurnHarder.setEnabled (false) MiTurnEasier.setEnabled (false); ctrlPanel.requestFocus (); / / set focus} / * * Game pause * / public void pauseGame () {if (block! = null) {block.pauseMove ();} ctrlPanel.setPlayButtonEnable (false); ctrlPanel.setPauseButtonLabel (false); ctrlPanel.setStopButtonEnable (true); miPlay.setEnabled (false); miPause.setEnabled (false); miResume.setEnabled (true); miStop.setEnabled (true) } / * Let the suspended game continue * / public void resumeGame () {if (block! = null) {block.resumeMove ();} ctrlPanel.setPlayButtonEnable (false); ctrlPanel.setPauseButtonEnable (true); ctrlPanel.setPauseButtonLabel (true); miPause.setEnabled (true); miResume.setEnabled (false); ctrlPanel.requestFocus ();} / * user stops the game * / public void stopGame () {playing = false; if (block! = null) {block.stopMove () } ctrlPanel.setPlayButtonEnable (true); ctrlPanel.setPauseButtonEnable (false); ctrlPanel.setPauseButtonLabel (true); ctrlPanel.setStopButtonEnable (false); ctrlPanel.setTurnLevelDownButtonEnable (true); ctrlPanel.setTurnLevelUpButtonEnable (true); miPlay.setEnabled (true); miPause.setEnabled (false); miResume.setEnabled (false); miStop.setEnabled (false); miTurnHarder.setEnabled (true); miTurnEasier.setEnabled (true); reset () / / reset canvas and control panel} / * get the difficulty set by the player * * @ return int, game difficulty 1-MAX_LEVEL * / public int getLevel () {return ctrlPanel.getLevel ();} / * user set game difficulty * * @ param level int, game difficulty 1-MAX_LEVEL * / public void setLevel (int level) {if (level)
< 11 && level >0) {ctrlPanel.setLevel (level);}} / * * get game points * * @ return int, integral * / public int getScore () {if (canvas! = null) {return canvas.getScore ();} return 0 } / * get the game credits since the last upgrade. After the upgrade, the credits are cleared * * @ return int, and the credits * / public int getScoreForLevelUpdate () {if (canvas! = null) {return canvas.getScoreForLevelUpdate ();} return 0 * @ return Boolean,true-update succeed,false-update fail * / public boolean levelUpdate () {int curLevel = getLevel (); if (curLevel < MAX_LEVEL) {setLevel (curLevel + 1); canvas.resetScoreForLevelUpdate (); return true;} return false;} / * Game start * / private void play () {reset (); playing = true Thread thread = new Thread (new Game ()); / / start the game thread thread.start ();} / * report that the game is over * / private void reportGameOver () {new gameOverDialog (this, "Tetris", "game over, your score is + canvas.getScore ())" } / * A round of games implements the Runnable interface. A round of games is a big cycle. In this cycle, every 100ms, check whether the current block in the game has reached the end. If not, * continue to wait. If the bottom is reached, it depends on whether there is a fully filled line, if so, delete it and add points to the player, while randomly generating a new current block and letting it fall automatically. * when a new block is generated, first check whether the top line of the canvas has been occupied, and if so, you can judge the Game Over. * / private class Game implements Runnable {@ Override public void run () {int col = (int) (Math.random () * (canvas.getCols ()-3)); int style = ErsBlock.STYLES [(int) (Math.random () * 7)] [(int) (Math.random () * 4)]; while (playing) {if (block! = null) {/ / the first loop, block is empty if (block.isAlive ()) {try {Thread.currentThread () Thread.sleep;} catch (InterruptedException ie) {ie.printStackTrace ();} continue;}} checkFullLine (); / / check whether there are fully filled rows if (isGameOver ()) {reportGameOver (); miPlay.setEnabled (true); miPause.setEnabled (false); miResume.setEnabled (false); miStop.setEnabled (false); ctrlPanel.setPlayButtonEnable (true); ctrlPanel.setPauseButtonLabel (false); ctrlPanel.setStopButtonEnable (false); return } block = new ErsBlock (style,-1, col, getLevel (), canvas); block.start (); col = (int) (Math.random () * (canvas.getCols ()-3)); style = ErsBlock.STYLES [(int) (Math.random () * 7)] [(int) (Math.random () * 4)]; ctrlPanel.setTipStyle (style) }} / / check whether there are fully filled lines in the canvas, and if so, delete public void checkFullLine () {for (int I = 0; I < canvas.getRows (); iTunes +) {int row =-1; boolean fullLineColorBox = true; for (int j = 0; j < canvas.getCols (); isColorBox +) {if (! canvas.getBox (I, j). IsColorBox ()) {fullLineColorBox = false; break;}} if (fullLineColorBox) {row = imurl; canvas.removeLine (row) } / / based on whether the top line is occupied, determine whether the game is over / / @ return boolean, true- game is over, false- game is not over private boolean isGameOver () {for (int I = 0; I < canvas.getCols (); iTunes +) {ErsBox box = canvas.getBox (0, I); if (box.isColorBox ()) {return true;}} return false;} / * * define GameOver dialog box. * / @ SuppressWarnings ("serial") private class gameOverDialog extends JDialog implements ActionListener {private JButton againButton, exitButton; private Border border = new EtchedBorder (EtchedBorder.RAISED, Color.white, new Color (148,145,140); public gameOverDialog (JFrame parent, String title, String message) {super (parent, title, true); if (parent! = null) {setSize (240,120); this.setLocationRelativeTo (parent); JPanel messagePanel = new JPanel (); messagePanel.add (new JLabel (message)); messagePanel.setBorder (border); Container container = this.getContentPane () Container.setLayout (new GridLayout (2,0,0,10); container.add (messagePanel); JPanel choosePanel = new JPanel (); choosePanel.setLayout (new GridLayout (0,2,4,0)); container.add (choosePanel); againButton = new JButton ("another game"); exitButton = new JButton ("quit the game"); choosePanel.add (new JPanel (). Add (againButton)); choosePanel.add (new JPanel (). Add (exitButton)); choosePanel.setBorder (border);} againButton.addActionListener (this) ExitButton.addActionListener (this); this.setVisible (true);} @ Override public void actionPerformed (ActionEvent e) {if (e.getSource () = = againButton) {this.setVisible (false); reset ();} else if (e.getSource () = = exitButton) {stopGame (); System.exit (0);}} these are all the contents of the article "how java implements Tetris Game". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.