I have been trying to make a button for my program but for some reason I can't click it.
The program is a space invaders game where the gameplay is, for the most part, implemented. However I have decided to create a main menu, and have gotten stuck on getting to button to fully work.
I have been playing around with it for a while, but every time the user clicks where the button is, nothing happens. There is no animation for holding down the left mouse.
Yet when tab is pressed, the button can be highlighted, and can be pressed with the space space.
Here is my code:
public class Main extends Canvas implements Runnable{
public static void main(String[] args) {
JFrame frame = new JFrame();
final JButton startgamebutton = new JButton("Start Game");
startgamebutton.setBounds(200, 500, 400, 50);
JComponent component = frame.getRootPane();
Main main = new Main();
Container cp = frame.getContentPane();
frame.add(main);
frame.setTitle("Space Invaders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
cp.add(startgamebutton);
cp.add(main);
main.Start();
startgamebutton.setBounds(200, 500, 400, 50);
startgamebutton.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == startgamebutton) {
System.out.println("start button Pressed ");
}}
});
}
}
Related
Having problems creating a custom JButton that is an Image. I had everything working with a normal JButton (like in the comment on the 2nd line) this way I wouldn't have to get an InputStream and start the button has an icon.
The trouble I'm having is that when I pressed the replay button (to play again) the window closes and only one window should pop out (as it happens with a "normal" JButton) but in this case 4-5 windows reopen and I don't know why.
I started thinking it was because the time to get an InputStream and doing ImageIO.read() the game would start and see that the variable running was false and then started reopening windows until it's true but I can't see how to verify that.
Note: I have functions that on ActionPerformed verify if the snake has collided and if so running = false and GameOver() will be called
public class GamePanel extends JPanel implements ActionListener {
JButton replay; //= new JButton("Play Again");
GamePanel() {
...
try {
InputStream is_replay = this.getClass().getResourceAsStream("/Snake/lib/img/playagain.png");
ImageIcon icon = new ImageIcon(ImageIO.read(is_replay));
replay = new JButton(icon);
replay.setBorder(BorderFactory.createEmptyBorder());
replay.setContentAreaFilled(false);
...
} catch (IOException|FontFormatException e) {
e.printStackTrace();
}
this.add(replay);
replay.setVisible(false);
replay.setBounds(SCREEN_WIDTH/2 - 100, SCREEN_HEIGHT - 200, 200, 100);
...
startGame();
}
public void startGame() {
spawnApple();
running = true;
timer = new Timer(DELAY, this);
timer.start();
}
public void gameOver(Graphics g) {
...
replay.setVisible(true);
replay.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == replay) {
JComponent comp = (JComponent)e.getSource();
Window win = SwingUtilities.getWindowAncestor(comp);
win.dispose(); //It will close the current window
new GameFrame(); //It will create a new game
}
}
});
}
}
public class GameFrame extends JFrame {
GameFrame() {
JPanel panel = new GamePanel();
this.add(panel);
panel.setLayout(null); //Needed to add components
this.setTitle("Snake Game");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.pack(); //Fit JFrame to the components
this.setVisible(true);
this.setLocationRelativeTo(null);
}
}
public class SnakeGame {
public static void main(String[] args) throws Exception {
new GameFrame();
}
}
"in this case 4-5 windows reopen"
This suggests that you are probably adding multiple ActionListeners to the replay JButton. A new listener is added each time game over method is called, and this is incorrect. I would not add the ActionListener to the button in the game over method but rather add it once where you create the replay button.
I am working on a GUI project with Swing in Java and the program is generally working fine. However, under each screen, I have a back button that calls the method of the screen before it and goes through the ArrayList containing all of the elements on the current screen and calls setVisible(false) on them. Upon running the program, the back button works correctly if you click it once but if you go back on the screen, and click it again, it takes two clicks for it to correctly work and then four clicks and then eight clicks and so on. I have no idea what is going on or why it is behaving this way as nothing in my code seems to do it. Also, sometimes, the button correctly returns to the previous screen but then keeps the components on the current screen active as if setVisible(false) was never called. The following code represents the general structure of my project. Is there anything that it is doing that is generating this problem?
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MAIN {
static JFrame frame;
static JPanel panel;
public static void main(String [] args) {
mainScreen();
}
public static void mainScreen() {
JButton newScreen = new JButton("Next Screen");
frame = new JFrame();
panel = new JPanel();
panel.setBounds(0,0,1920,1080);
panel.setBackground(Color.cyan);
newScreen.setBounds(50, 500, 100, 500);
newScreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newScreen.setVisible(false);
JButton returnButton = new JButton("return");
returnButton.setBounds(50, 50, 100, 100);
panel.add(returnButton);
returnButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
returnButton.setVisible(false);
mainScreen();
}
});
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.add(newScreen);
frame.add(panel);
frame.setSize(1920,1080);
frame.setLayout(null);
frame.setVisible(true);
}
}
public class StartMenu extends JFrame {
public static void StartMenu() {
JFrame StartMenu = new JFrame("SamBallPool"); //New Windwow With Title
StartMenu.add(new JLabel(new ImageIcon("Data/StartMenu.png")));
StartMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set Close Operation
StartMenu.setSize(600, 330);
StartMenu.setVisible(true);
JPanel StartPanel = new JPanel();
StartPanel.setLayout(null);
StartPanel.setVisible(true);
ImageIcon Start = new ImageIcon("Data/StartButton.png"); //Play Button in Center
JButton StartButton = new JButton(Start);
StartButton.setBounds(263,101,70,53); //Positioned Button Over Image of Button
StartButton.setVisible(true);
StartButton.setEnabled(true);
StartButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent StartButtonClicked){
JOptionPane.showMessageDialog(null,"Test");
MainMenu.MainMenu();
StartMenu.dispose();
}
});
StartPanel.add(StartButton);
StartMenu.add(StartPanel);
}
}
The button does not show up, filepath is correct, even without the image it doesn't show up. It used to show up but stopped working for some reason, thanks for help in advance :(
You call StartPanel.setVisible(true); too early. It must be the last statement after configuration of the form is completed.
I am making a pacman game. The plan is to have a GUI that will show the welcome page and a JButton that reads "Play." After the user presses the "Play" button, it will open a new panel that will have an image that will consist of the instructions. On the instructions panel, there will be a JButton that reads "Continue." This will take the user to a GUI with four JButtons reading "Easy," "Medium," "Hard," and "QUIT." The actionPerformed methods for these buttons have already been coded and it works properly (a new JFrame will open with the corresponding Panel to each level, QUIT will quit the program).
The way I created this is that I made a class called GUIPanel and all the panels will open when the GUIPanel object is instantiated.
After I completed the GUI with the levels, that's when I decided to add the Welcome panel and and Instructions panel. So far, I have written code for the Welcome panel. I added it above the code for the level-buttons.
My problem is that whenever I run it, a frame is created but there is no panel. PLEASE HELP ME!! THIS PROJECT IS DUE TOMORROW.
//*****************************
// GUI : Panel
//
// Updated : May 4th, 2017
//
//*****************************
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
public class GUIPanel extends JPanel
{
private Graphics g;
private BufferedImage buffer;
public GUIPanel()
{
try{
//welcome panel
JPanel welcome = new JPanel();
ImageIcon title = new ImageIcon("welcome.png");
buffer = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
g = buffer.getGraphics();
g.drawImage(title.getImage(), 400, 400, 200, 200, null);
//add button and listener and add onto welcome (JPanel)
JButton play = new JButton("Play");
play.addActionListener(new PlayListener());
welcome.add(play);
}
catch(Exception e){
}
}
private class PlayListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
JFrame playFrame = new JFrame();
//creating the panel
JPanel panel = new JPanel();
add(panel);
/*creating the Easy button which will allow the
user to play the EASY LEVEL of Karelman */
JButton easyButton = new JButton("Easy");
easyButton.setBackground(Color.BLUE);
easyButton.setForeground(Color.WHITE);
easyButton.addActionListener(new EasyListener());
panel.add(easyButton);
/*creating the Medium button which will allow the
user to play the MEDIUM LEVEL of Karelman */
JButton mediumButton = new JButton("Medium");
mediumButton.setBackground(Color.BLUE);
mediumButton.setForeground(Color.WHITE);
mediumButton.addActionListener(new MediumListener());
panel.add(mediumButton);
/*creating the random button which will allow the
user to play the HARD LEVEL of Karelman*/
JButton hardButton = new JButton("Hard");
hardButton.setBackground(Color.BLUE);
hardButton.setForeground(Color.WHITE);
hardButton.addActionListener(new HardListener());
hardButton.setBounds(40, 100, 100, 60);
panel.add(hardButton);
/*creating the quit button for exiting the program*/
JButton quitButton = new JButton("QUIT");
quitButton.setBackground(Color.BLUE);
quitButton.setForeground(Color.WHITE);
quitButton.addActionListener(new QuitListener());
quitButton.setBounds(150, 300, 25, 25);
panel.add(quitButton);
}
catch(Exception i){
}
}
}
/*
EasyListener is attached to the Easy Button
It will open the Easy Level by creating a
new frame which will use Easy Ghosts.
*/
private class EasyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
JFrame frame = new JFrame("PLAY KARELMAN -- EASY");
frame.setSize(708, 738);
frame.setLocation(350, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//create panel for Easy Level within frame
EasyPanel easy = new EasyPanel();
frame.setContentPane(easy);
}
catch(IOException i){
}
}
}
/*
MediumListener is attached to the Medium Button
It will open the Medium Level by creating a
new frame which will use Medium Ghosts.
*/
private class MediumListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
JFrame frame = new JFrame("PLAY KARELMAN -- MEDIUM");
frame.setSize(708, 738);
frame.setLocation(350, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//create panel for Medium Level within frame
MediumPanel medium = new MediumPanel();
frame.setContentPane(medium);
}
catch(IOException i){
}
}
}
/*
HardListener is attached to the Hard Button
It will open the Hard Level by creating a
new frame which will use Hard Ghosts.
*/
private class HardListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
try{
JFrame frame = new JFrame("PLAY KARELMAN -- HARD");
frame.setSize(708, 738);
frame.setLocation(350, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//create panel for Hard Level within frame
HardPanel hard = new HardPanel();
frame.setContentPane(hard);
}
catch(IOException i){
}
}
}
/*
QuitListener is attached to the Quit Button
It will make a system call to exit the program.
*/
private class QuitListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
}
So I tried to add a button to my game, but I can't understand, how to make the code work. Here's the code for my Launcher class (which has the button code and launches my whole game, imports and package not included):
public class Launcher extends JFrame {
public static void main(String[] args) {
Game game = new Game("Ninja Adventures", 720, 480);
game.start();
JButton button = new JButton("hello agin1");
Game.add(button);
button.addActionListener (new Action1());
}
static class Action1 implements ActionListener {
public void actionPerformed (ActionEvent e) {
JFrame frame2 = new JFrame("Clicked");
frame2.setVisible(true);
frame2.setSize(200,200);
JLabel label = new JLabel("you clicked me");
}
}
}
Some extra code, that I think might help:
private void createDisplay(){
frame = new JFrame(title);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
As already said you need to add the JButton to the JFrame. Which in this case is in another method. So you can create the button in that method an add it like so: frame.add(button);
Or you pass the button to the method via the constructor (I recommend the first version).