⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 oxogui.java

📁 This the OXO example code plus the presentation. It is intended to provide you with some clues about
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *  This class provides the GUI for a simple two person OXO game.
 *  The playing board is made up of an array of JButtons.
 *  Instructions and messages are displayed in a JTextField 
 *
 *  @version 1.0
 *  @author Ian Bradley 
 */

public class OxoGui extends JFrame implements ActionListener
{

    
    // The array of JButtons used to represent the board 
    private JButton [] moveButtons;
    // The text field used for messages 
    private JTextField messageField;
    // Control Buttons 
    private JButton playButton;
    private JButton stopButton;
    
    // the game
    OXOGame game ;

    
    /**
     *  The constructor used to create the GUI 
     */
    public OxoGui()
    {
        super("OXO");
        makeFrame();
        showFrame();
    }
    
    private void makeFrame()
    {
    
        Container contentPane = getContentPane();
        contentPane.setLayout( new BorderLayout() );        
        // set up the board 
        moveButtons = new JButton [9];
        for ( int i = 0; i< moveButtons.length ; i++)
        {
            moveButtons[i] = new JButton("");
            moveButtons[i].setActionCommand("" + i);
            moveButtons[i].setEnabled(false);
        }   
        // set up the message text field 
        messageField = new JTextField(15);
        messageField.setEditable(false);
        messageField.setBackground(Color.yellow);
        messageField.setHorizontalAlignment(JTextField.CENTER );
        messageField.setText("Press Play to Start");
        // set up the control buttons 
        playButton = new JButton ("play");
        stopButton = new JButton ("stop");
        setControlButtons(true, false);
        // define the JPanels and add content                 
        JPanel boardPanel = new JPanel();
        JPanel messagePanel = new JPanel(); 
        JPanel controlPanel = new JPanel();
        boardPanel.setLayout( new GridLayout(3,3));
        for (int i = 0 ; i < moveButtons.length ; i++)
            boardPanel.add(moveButtons[i]); 
        messagePanel.add(messageField);
        controlPanel.add(playButton);
        controlPanel.add(stopButton);
        add(messagePanel,BorderLayout.NORTH);
        add(boardPanel, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.SOUTH);
        // registering the action listeners 
        for( int i = 0 ;  i < moveButtons.length ; i++)
           moveButtons[i].addActionListener(this);
        playButton.addActionListener(this);
        stopButton.addActionListener(this);


    }
    
    private void showFrame()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200,200);
        setLocationRelativeTo( null);    
        setVisible( true );      
    }
    
    /**
     *  defines the response for the JButtons
     *
     *  @param e  the ActionEvent
     **/
    public void actionPerformed (ActionEvent e) 
    {       

        String actionLabel = e.getActionCommand();
        if (actionLabel.equals("play") )
        {
            game = new OXOGame();
            for ( int i = 0; i< moveButtons.length ; i++)
            { 
                moveButtons[i].setText("");
                moveButtons[i].setEnabled(true);
            } 
            setControlButtons(false, true);
            messageField.setText("Playing");
        }
        else
        if ( actionLabel.equals("stop") )  
        {
            enableMoveButtons( false); 
            setControlButtons(true, false);
            messageField.setText("Press Play to Start");
        }

        else
        {
           // process a user move 
           int buttonNumber = Integer.parseInt(actionLabel);
           game.userUpdate(buttonNumber);        
           moveButtons[buttonNumber].setText("O");
           moveButtons[buttonNumber].setEnabled(false);
           // checking for a user win 
           if ( game.checkForWin())
           {
              enableMoveButtons(false);
              messageField.setText("User Wins!");
              setControlButtons(true, false);
           }
           else
           // check to see if there is still space
           if ( game.checkForSpace() )
           {
               // process a computer move
               Coord coord = game.computerUpdate();
               buttonNumber = coord.getRow() * 3 + coord.getColumn();
               moveButtons[buttonNumber].setText("X");
               moveButtons[buttonNumber].setEnabled(false);
               // check to see if the computer has won
               if ( game.checkForWin())
               {
                    enableMoveButtons(false);
                    messageField.setText("Computer Wins!");
                    setControlButtons(true, false);
                }
            }
            else
            {
                // no space left so must bwe a draw
                enableMoveButtons( false);
                messageField.setText("A Draw");
                setControlButtons(true, false);
            }
        }
    } 
    
    private void enableMoveButtons( boolean bool)
    {
        for ( int i = 0; i< moveButtons.length ; i++)
            moveButtons[i].setEnabled(bool);
    }
    
    private void setControlButtons(boolean play, boolean stop)
    {
        playButton.setEnabled(play);
        stopButton.setEnabled(stop);
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -