0

Hi take a look on this code: package arkanoid;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.event.*;

public class Arkanoid extends JFrame 
{
private static final long serialVersionUID = 6253310598075887445L;
static JFrame frame;


static class Action1 implements ActionListener {        
      public void actionPerformed (ActionEvent e) {     
        //frame = new JFrame("Arkanoid");
        frame.setLocationRelativeTo(null);
        frame.setIgnoreRepaint(true);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setSize(500,400);
        frame.add(new Gra());
      }
    }   
    static class Action2 implements ActionListener {        
      public void actionPerformed (ActionEvent e) {     
          frame.dispose();
          System.exit(0);
      }
} 
public static void main(String[] args) 
{
    //new Arkanoid();
      frame = new JFrame("Arkanoid");
      frame.setSize(500,400);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("Arkanoid BETA");
      frame.setLocationRelativeTo(null);
      frame.setIgnoreRepaint(true);
      frame.setResizable(false);
      frame.setVisible(true);
      JPanel panel = new JPanel();
      frame.add(panel);

      JButton button = new JButton("Nowa Gra");
      panel.add(button);
      button.addActionListener (new Action1());

      JButton button2 = new JButton("Wyjscie");
      panel.add(button2);
      button2.addActionListener (new Action2());
}
}

This code almost works, I want to make a button2 a quit button working like X button in top right frame's icons and button1 need to open a Gra() in the same window. When im doing it like this it isnt work fine:/ i need to click 2 times on button1 to go to Gra() and what is more KeyListeners in Gra() arent working :( Im new in buttons, frames and panels in java so please help with this code. Correct it please.

0

3 Answers 3

3

There are a number of fundamental problems with your code, the least of which is why your button1 requires 2 clicks.

However, for your problem you should try rearranging the order of your button1 listener, so that your Component is added to the frame first, before setting it to be visible. An example that should work:

static class Action1 implements ActionListener {        
     public void actionPerformed (ActionEvent e) {   
        frame.add(new Gra());
        frame.revalidate();
     }
}   

Note you have already set the size, location etc of frame in main, so there is no need to set them again every time the button is clicked.

I stress that there are more important problems with your code than this issue. You should take a look at Java's Modifier Types (static does not seem applicable here), as well as object-oriented concepts such as inheritance (you define your Arkanoid class to extend JFrame, yet have a JFrame object as a class variable).

Sign up to request clarification or add additional context in comments.

Comments

2

I want to make a button2 a quit button working like X button in top right frame's

You can use the ExitAction class found in Closing an Application.

For other examples of how to use buttons read the Swing tutorial on How to Use Buttons. This is the place to start for all you Swing related questions.

Comments

1

There are many problems with your code. I've refactored it a little. With below code & @ricky116 answer I think you should get all of them.

import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;

public class Arkanoid extends JFrame 
{
    public Arkanoid() {
        super("Arkanoid");
        setSize(500,400);
        setTitle("Arkanoid BETA");
        setLocationRelativeTo(null);
        setResizable(false);

        final JPanel panel = new JPanel();
        setContentPane(panel);

        panel.add(new JButton(new AbstractAction("Nowa Gra") {
            public void actionPerformed (ActionEvent e) {     
              panel.removeAll();
              panel.add(new Gra());
              panel.revalidate();
              panel.repaint();
            }
        });

        panel.add(new JButton(new AbstractAction("Wyjscie") {
            public void actionPerformed (ActionEvent e) {     
                Arkanoid.this.setVisible(false);
            }
        });
    }

    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
              Arkanoid frame = new Arkanoid();
              frame.setVisible(true);
            }
        });
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.