Java: reset button for your game 🎮

preview_player
Показать описание
#Java #reset #button

Java reset button game tutorial explained

//--------------------------------------------------------------------------------------------
public class Main {

public static void main(String[] args) {

new GameFrame();

}
}
//--------------------------------------------------------------------------------------------

public class GameFrame extends JFrame implements ActionListener{

Game game;
JButton resetButton;

GameFrame(){


resetButton = new JButton();

game = new Game();


}

@Override
public void actionPerformed(ActionEvent e) {
game = new Game();
}
}
}
//--------------------------------------------------------------------------------------------

public class Game extends JPanel{

Random random;

Game(){
// code for game goes here :D

random = new Random();



}
}
//--------------------------------------------------------------------------------------------
Рекомендации по теме
Комментарии
Автор


public class Main {

public static void main(String[] args) {

new GameFrame();

}
}

import java.awt.event.ActionEvent;
import
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class GameFrame extends JFrame implements ActionListener{

Game game;
JButton resetButton;

GameFrame(){


this.setSize(600, 500);
this.setLayout(null);

resetButton = new JButton();

resetButton.setSize(100, 50);
resetButton.setLocation(0, 200);


game = new Game();

this.add(resetButton);
this.add(game);
this.setVisible(true);

}

@Override
public void actionPerformed(ActionEvent e) {
{
this.remove(game);
game = new Game();
this.add(game);
Game.requestFocusInWindow(); #You may need to add this line. When you click the reset button, it may take focus away from the game.

}
}
}

import java.awt.Color;
import java.util.Random;
import javax.swing.JPanel;

public class Game extends JPanel{

Random random;

Game(){
// code for game goes here :D

random = new Random();

int r = random.nextInt(256);
int g = random.nextInt(256);
int b = random.nextInt(256);

this.setSize(500, 500);
this.setLocation(100, 0);
this.setBackground(new Color(r, g, b));

}
}

BroCodez
Автор

You are amazing!
Keep these Java videos going!!! <3

lubodimoff
Автор

fantastic. I have been wrestling with your TicTacToe game, trying to create a Reset button and an Exit button. The reset was giving me fits. now, I can go back. thank you much!

stephanieezat-panah
Автор

Just created my first game with your help (it was like i watched your java tutorials and said in my mind "ok i will do something ez - rolling the dice with computer till someone get 30 points" but that took me 4 days after get back from my real job) i went through many tutorials of yours, and you helped me a lot, when i done this - i felt excited and satisfy! big thanks for this Bro!

bubu
Автор

Thanks bro! I discovered your channel some time ago and watched your JButton video to create something like a reset button, but this is way better !!

dekin
Автор

This was extremely helpful! Thanks again

Garrison
Автор

Your Java series is doing very well!
Wholesome;

noah
Автор

What is the use of ??
Btw, LOVE your tutorials

ad
Автор

I searched for revalidate()/repaint() because my frame does not update to the newly added component after removing the previous one (probably because I don't know how to use it).

... and worked very well.

cheeese
Автор

I tried to create this reset button for the game snake, but it doesn appear me... I have created a new class called "Reset" where I puted your "GameFrame" code, because if I put that on the GameFrame of the Snake Game one, its bugs...

How I create a reset button for that game. Thanks for advance

shadikaes
Автор

I wanted to add a restart Button to the snake game you made a tutorial on how would i do that

Abhi-pqqs
Автор

@Bro Code how can we add this to the snake game?

marwayassine
Автор

can you make the same button to calculate and reset ?

vitz
Автор

Thanks bro
tho I have a problem where the KeyAdapters stop working when I click the restart button
any suggestions?

janil
Автор

How do we get the JButton to take up only the space required for the size of the button? Why does it cover up all the rest of graphics that I already have and the button itself is the correct size? Does it come with a whole screen along with a button? If so, how do I get rid of that extra room?

RyandracusChapman
Автор

have to admit that java is much faster than python in game programming

madeariartha
Автор

The code works fine for me except after I click it the KeyAdapaters stop working, can you tell me why?

MrLegendGaming
Автор

How can I create reset button for that TICTACTOE game?

niteshkuriyal
visit shbcf.ru