简体   繁体   中英

How to make Jbutton run another class?

Hi stackoverflow community!

I'm making an AI TicTacToe project for my finals, and I'm having a problem trying to run another class after pressing one of jButtons in the jFrame class.

I'm using NetBean's jFrame class, where you can design easily by placing it from the container, and some of the codes are not editable.

What I want to make is a workable Main Menu (which is a jFrame class) for my gaming project, and it contains three buttons which are Normal, Large and Extra Large. For Normal button, I want to make this button to run TicTacToe (which is a normal java class) after being pressed, but for some reasons I can't make it work. Here are the codes:

MainMenu.java

private void ButtonNormal(java.awt.event.ActionEvent evt) {                              
    // TODO add your handling code here:
    Normal_TicTacToe SIZE1 = new Normal_TicTacToe();  // This is the problem
    SIZE1.setVisible(true);                             
}
/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() 
    {
        public void run()
        {
            new MainMenu().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton buttonNormal;
// End of variables declaration                   
}

Normal_TicTacToe.java - I got this code from the internet, and I'm modifying it for Large and Extra Large size. I'll credit this guy as the original author in the documentation.

public final class Normal_TicTacToe extends JApplet 
{

private static final long serialVersionUID = 1L;
private final Normal_Tile[] TILES = new Normal_Tile[9];
private final int TILE_SPACING = 96;
private final int WIDTH = 96, HEIGHT = 96;
private final JFrame GAMEFRAME = new JFrame("Tic-Tac-Toe");
private final Normal_TilePainter PAINTER = new Normal_TilePainter(this);
private final Normal_ClickHandler CLICK_HANDLER = new Normal_ClickHandler(this);
private final boolean AI;
private boolean aiTurn = false;
private Normal_Holder turn = Normal_Holder.X;
private int whoseTurn = 0;
private final Dimension FRAME_SIZE = new Dimension(295, 304);
private final int FONT_SIZE = 64;
private int oWins = 0;
private int xWins = 0;
private boolean gameOver = false;
private boolean nextTurn = false;
public final Normal_AI GAME_AI = new Normal_AI(this);

public void init() 
{
    Normal_TicTacToe game = new Normal_TicTacToe(true);
game.newGame();
}

public Normal_TicTacToe(boolean ai) 
{
    this.AI = ai;
    PAINTER.setSize(FRAME_SIZE);
    buildFrame();
    loadTiles();
}

Also, both java files are in the same package.

If you're looking for extended codes and all the java files, you can find it here:
My MainMenu.java
Chall's TicTacToe and his java files (Scroll down until you see source files).

You should create an instance of it in your button.

Your code:

buttonXLarge.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        ButtonXLarge(evt); // not sure what it does, but it doesn't make Tic Tac Toe
    }
});

What you want most likely want it to do is initialize and start a new tic tac toe board.

buttonXLarge.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        Normal_TicTacToe myBoard = new Normal_TicTacToe(true);
        myBoard.newGame();
    }
});

I am unsure of how JApplet will handle inside what you're currently doing, as I normally never mix applets with JFrames, but specifically to activate the tic tac toe board, you should be writing what you want it to do within the actionPerformed listener.

If you REALLY wanted to save time on coding, you could probably rebuild TicTacToe using the guts of the JApplet in a JFrame.

The constructor of the Normal_TICTACTOE looks like this:

public Normal_TicTacToe(boolean ai) 
{
this.AI = ai;
PAINTER.setSize(FRAME_SIZE);
buildFrame();
loadTiles();
}

It has a boolean variable in its parameter list.

So the default constructor is overwritten.

call the constructor with a boolean value (true or false):

Normal_TicTacToe game = new Normal_TicTacToe(true);

(I think it has something to do with the artifical inteligence on or off)

call the method newGame() on the instance you got.

game.newGame();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM