0

I have read a lot about Swing, but can't figure out why my GUI won't display? It compiles absolutely fine. I am guessing it's because I'm not using the setVisible() method properly, or I am not using the pack() method. I must stress I am very much a newb to Swing. I'm not looking for someone to do it for me, just a pointer in the right direction.

My code is as follows :

import java.awt.event.*;
import javax.swing.*;  
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;        
import javax.swing.JLabel;

public class TestGUI3 implements ActionListener
{  
    JRadioButton rb1, rb2, rb3;
    JFrame f;  
    JButton b;
    public void TestGUI3(NewGameGUI NewGameGUIMethod)
    {  
        f = new JFrame("Welcome to Ballon d'or");  
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        rb1 = new JRadioButton("Start New Game");  
        rb2 = new JRadioButton("Load Game");
        rb3 = new JRadioButton("Quit");

        rb1.setBounds(50,100,170,30);  
        rb2.setBounds(50,150,170,30);  
        rb3.setBounds(50,200,170,30);

        ButtonGroup bg=new ButtonGroup();  
        bg.add(rb1);
        bg.add(rb2);
        bg.add(rb3);  

        b = new JButton("OK");
        b.setBounds(200,250,180,30);  
        b.addActionListener(this);
        f.add(rb1);
        f.add(rb2);
        f.add(rb3);
        f.add(b);


        f.setSize(500,500);  
        f.setLayout(null);  
        f.setVisible(true);  
    }  
          @Override public void actionPerformed(ActionEvent e)
            {  
                if(rb1.isSelected())
                {
                    f.dispose();
                    NewGameGUI test = new NewGameGUI();

                    test.getFrame().setVisible(true);
                    //JOptionPane.showMessageDialog(f,"New Game");  
                }  
                if(rb2.isSelected())
                {
                    JOptionPane.showMessageDialog(f,"Load Game");  
                } 
                if(rb3.isSelected())
                {
                    System.exit(0);
                }  

            }

            public static void main(String[] args) 
            {  

                new TestGUI3();  
            }  
}  

// my other class

import java.awt.event.*;
import javax.swing.*;  
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;        
import javax.swing.JLabel;

public class NewGameGUI implements ActionListener
{ 
  JRadioButton rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8 , rb9 , rb10, rb11, rb12, rb13, rb14, rb15, rb16, rb17, rb18, rb19, rb20;
    JFrame f2;  
    JButton b;
    JLabel label1; 
    public void NewGameGUIMethod()
    {  
        label1 = new JLabel("Please choose a team");
        f2 = new JFrame("Ballon d'or");  
        f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        rb1 = new JRadioButton("Arsenal");  
        rb2 = new JRadioButton("Aston Villa");
        rb3 = new JRadioButton("Bournemouth");
        rb4 = new JRadioButton("Chelsea");
        rb5 = new JRadioButton("Crystal Palace");
        rb6 = new JRadioButton("Everton");
        rb7 = new JRadioButton("Leicester City");
        rb8 = new JRadioButton("Liverpool");
        rb9 = new JRadioButton("Manchester United");
        rb10 = new JRadioButton("Manchester City");
        rb11 = new JRadioButton("Newcastle United");
        rb12 = new JRadioButton("Norwich City");
        rb13 = new JRadioButton("Southampton");
        rb14 = new JRadioButton("Stoke City");
        rb15 = new JRadioButton("Sunderland");
        rb16 = new JRadioButton("Swansea City");
        rb17 = new JRadioButton("Tottenham Hotspur");
        rb18 = new JRadioButton("Watford");
        rb19 = new JRadioButton("West Brom");
        rb20 = new JRadioButton("West Ham");


        label1.setBounds(50,25,200,30);
        rb1.setBounds(50,100,170,30);  
        rb2.setBounds(50,150,170,30);  
        rb3.setBounds(50,200,170,30);

        ButtonGroup bg=new ButtonGroup();  
        bg.add(rb1);
        bg.add(rb2);
        bg.add(rb3);  
        bg.add(rb4); 
        bg.add(rb5); 
        bg.add(rb6); 
        bg.add(rb7); 
        bg.add(rb8); 
        bg.add(rb9); 
        bg.add(rb10); 
        bg.add(rb11); 
        bg.add(rb12); 
        bg.add(rb13); 
        bg.add(rb14); 
        bg.add(rb15);
        bg.add(rb16);
        bg.add(rb17);
        bg.add(rb18);
        bg.add(rb19);
        bg.add(rb20);

        b = new JButton("OK");
        b.setBounds(200,250,180,30);  
        b.addActionListener(this);
        f2.add(rb1);
        f2.add(rb2);
        f2.add(rb3);
        f2.add(rb4);
        f2.add(rb5);
        f2.add(rb6);
        f2.add(rb7);
        f2.add(rb8);
        f2.add(rb9);
        f2.add(rb10);
        f2.add(rb11);
        f2.add(rb12);
        f2.add(rb13);
        f2.add(rb14);
        f2.add(rb15);
        f2.add(rb16);
        f2.add(rb17);
        f2.add(rb18);
        f2.add(rb19);
        f2.add(rb20);
        f2.add(b);


        f2.setSize(500,500);  
        f2.setLayout(null);  
        f2.setVisible(true);  

        NewGameGUI test = new NewGameGUI();
        test.setFrame(f2);

    }

    public void setFrame(JFrame f2){
        this.f2 = f2;
    }
    public JFrame getFrame(){

    return f2;
    }
            public void actionPerformed(ActionEvent e)
            {  
                if(rb1.isSelected())
                {
                    f2.dispose();

                    //JOptionPane.showMessageDialog(f,"New Game");  
                }  
                if(rb2.isSelected())
                {
                    JOptionPane.showMessageDialog(f2,"Load Game");  
                } 
                if(rb3.isSelected())
                {
                    System.exit(0);
                }  

            }

            public static void main(String[] args) 
            {  
                new TestGUI3();  
            }  
}
4
  • Tip: If you are going to use sequential numbering of variable names, it is best to use an array to store those. Commented Jun 10, 2016 at 7:53
  • 2
    Two hints: don't write a lot of code that maybe compiles to then lump everything together. Instead: start with a small JFrame, maybe only containing a button, whatever. Make that work. When it works, add "more" elements. Understand how things work - then extend them (versus building on something that you don't understand yet). Then: you want to learn about arrays. Whenever you start naming variables rb1, rb2, rb3 ... and you have code to deal with all of them separately ... big waste of time and energy! Commented Jun 10, 2016 at 7:53
  • 2
    Sorry for not actually answering, but why don't you try something more recent like javafx - docs.oracle.com/javase/8/javafx/get-started-tutorial/… Commented Jun 10, 2016 at 7:53
  • 3
    Additionally, please edit your question to show a smaller sample of the problem, or what we like to call a minimal reproducible example. Commented Jun 10, 2016 at 7:54

2 Answers 2

5

Both of your files have this

public static void main(String[] args) 
{  
    new TestGUI3();  
}  

Which is going to call the default constructor for the TestGUI3 class, which does nothing.


Additionally, you seem to be conflicting your understanding of constructors with instance methods.

public void TestGUI3(NewGameGUI NewGameGUIMethod)

and

public void NewGameGUIMethod()

Constructors do not have a return type. Plus, the second of these isn't even the name of the class it is in...


I would recommend you do something like class TestGUI3 extends JFrame implements ActionListener, and that way, you don't need to have an inline reference to a Frame, and instead your classes are a Frame that you can setVisible() on to show them.

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

1 Comment

In addition to your answer he also declares his other constructor as "public void TestGUI3()" which results in a class function instead of a constructor.
0

1-remove void because adding it will make it normal method not constructor public void NewGameGUIMethod() , also it should be named NewGameGUI , the final name would be public NewGameGUI()
2- remove void in the other class, for the same of above public void TestGUI3(NewGameGUI NewGameGUIMethod), also there is no need to pass NewGameGUIMethod as paramter since it is not used in TestGUI3
3- You are constructing a new frame everytime you call the NewGameGUIMethod() , which cause a recursion, just comment the frame creation , and set the instance you constructed to visible as follows:

    //NewGameGUI test = new NewGameGUI();
    //test.setFrame(f2);
    f2.setVisible(true);

4- update the main method of NewGameGUI to be :

public static void main(String[] args) {
    new NewGameGUI();
}

enter image description here

Full class code for NewGameGUI class:

package test.q5;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class NewGameGUI implements ActionListener {
    JRadioButton rb1, rb2, rb3, rb4, rb5, rb6, rb7, rb8, rb9, rb10, rb11, rb12, rb13, rb14, rb15, rb16, rb17, rb18, rb19, rb20;
    JFrame f2;
    JButton b;
    JLabel label1;

    public  NewGameGUI() {
        label1 = new JLabel("Please choose a team");
        f2 = new JFrame("Ballon d'or");
        f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        rb1 = new JRadioButton("Arsenal");
        rb2 = new JRadioButton("Aston Villa");
        rb3 = new JRadioButton("Bournemouth");
        rb4 = new JRadioButton("Chelsea");
        rb5 = new JRadioButton("Crystal Palace");
        rb6 = new JRadioButton("Everton");
        rb7 = new JRadioButton("Leicester City");
        rb8 = new JRadioButton("Liverpool");
        rb9 = new JRadioButton("Manchester United");
        rb10 = new JRadioButton("Manchester City");
        rb11 = new JRadioButton("Newcastle United");
        rb12 = new JRadioButton("Norwich City");
        rb13 = new JRadioButton("Southampton");
        rb14 = new JRadioButton("Stoke City");
        rb15 = new JRadioButton("Sunderland");
        rb16 = new JRadioButton("Swansea City");
        rb17 = new JRadioButton("Tottenham Hotspur");
        rb18 = new JRadioButton("Watford");
        rb19 = new JRadioButton("West Brom");
        rb20 = new JRadioButton("West Ham");

        label1.setBounds(50, 25, 200, 30);
        rb1.setBounds(50, 100, 170, 30);
        rb2.setBounds(50, 150, 170, 30);
        rb3.setBounds(50, 200, 170, 30);

        ButtonGroup bg = new ButtonGroup();
        bg.add(rb1);
        bg.add(rb2);
        bg.add(rb3);
        bg.add(rb4);
        bg.add(rb5);
        bg.add(rb6);
        bg.add(rb7);
        bg.add(rb8);
        bg.add(rb9);
        bg.add(rb10);
        bg.add(rb11);
        bg.add(rb12);
        bg.add(rb13);
        bg.add(rb14);
        bg.add(rb15);
        bg.add(rb16);
        bg.add(rb17);
        bg.add(rb18);
        bg.add(rb19);
        bg.add(rb20);

        b = new JButton("OK");
        b.setBounds(200, 250, 180, 30);
        b.addActionListener(this);
        f2.add(rb1);
        f2.add(rb2);
        f2.add(rb3);
        f2.add(rb4);
        f2.add(rb5);
        f2.add(rb6);
        f2.add(rb7);
        f2.add(rb8);
        f2.add(rb9);
        f2.add(rb10);
        f2.add(rb11);
        f2.add(rb12);
        f2.add(rb13);
        f2.add(rb14);
        f2.add(rb15);
        f2.add(rb16);
        f2.add(rb17);
        f2.add(rb18);
        f2.add(rb19);
        f2.add(rb20);
        f2.add(b);

        f2.setSize(500, 500);
        f2.setLayout(null);
        f2.setVisible(true);

        //NewGameGUI test = new NewGameGUI();
        //test.setFrame(f2);
        f2.setVisible(true);

    }

    public void setFrame(JFrame f2) {
        this.f2 = f2;
    }

    public JFrame getFrame() {

        return f2;
    }

    public void actionPerformed(ActionEvent e) {
        if (rb1.isSelected()) {
            f2.dispose();

            // JOptionPane.showMessageDialog(f,"New Game");
        }
        if (rb2.isSelected()) {
            JOptionPane.showMessageDialog(f2, "Load Game");
        }
        if (rb3.isSelected()) {
            System.exit(0);
        }

    }

    public static void main(String[] args) {
        new NewGameGUI();
    }
}

Class 2:

package test.q5;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class TestGUI3 implements ActionListener {
    JRadioButton rb1, rb2, rb3;
    JFrame f;
    JButton b;

    public  TestGUI3() {
        f = new JFrame("Welcome to Ballon d'or");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        rb1 = new JRadioButton("Start New Game");
        rb2 = new JRadioButton("Load Game");
        rb3 = new JRadioButton("Quit");

        rb1.setBounds(50, 100, 170, 30);
        rb2.setBounds(50, 150, 170, 30);
        rb3.setBounds(50, 200, 170, 30);

        ButtonGroup bg = new ButtonGroup();
        bg.add(rb1);
        bg.add(rb2);
        bg.add(rb3);

        b = new JButton("OK");
        b.setBounds(200, 250, 180, 30);
        b.addActionListener(this);
        f.add(rb1);
        f.add(rb2);
        f.add(rb3);
        f.add(b);

        f.setSize(500, 500);
        f.setLayout(null);
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (rb1.isSelected()) {
            f.dispose();
            NewGameGUI test = new NewGameGUI();

            test.getFrame().setVisible(true);
            // JOptionPane.showMessageDialog(f,"New Game");
        }
        if (rb2.isSelected()) {
            JOptionPane.showMessageDialog(f, "Load Game");
        }
        if (rb3.isSelected()) {
            System.exit(0);
        }

    }

    public static void main(String[] args) {

        new TestGUI3();
    }
}

Final note : you should consider reading about swing layouts(FlowLayout,BorderLayout,GridLayout,BoxLayout) to be able to control your components correctly.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.