-1

The project is about designing a GUI questionnaire in which people choose certain options, which are then compared to the developer (i.e my partner and I).

It was working earlier, but my JRadioButton-related code stoutly refuses to work properly. I don't see any buttons, and the only things that do show up are the labels. What can I do to fix this problem? Here is my code:

import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class MyFirstGUIDriver extends JFrame{
    public static void main(String[] args) {
           MyFirstGUI first=new MyFirstGUI();
            first.setTitle("MyFirstGUI");
            first.setSize(700,800);
            first.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            first.setVisible(true);
       }
}

And the definition class:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Container;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

@SuppressWarnings("Serial")
public class MyFirstGUI extends JFrame implements ActionListener {
JPanel apanel;

JButton quitButton;
JButton submitButton;

JLabel label1;
JLabel label2;
JLabel label3;
JLabel label4;
JLabel label5;
JLabel label6;

JTextField Text1;
JTextField Text2;
JTextField Text3;
JTextField Text4;
JTextField Text5;
JTextField Text6;

JFrame frame=new JFrame();


    public MyFirstGUI(){
        apanel=new JPanel(null);
        quitButton=new JButton("  Quit  ");
        quitButton.addActionListener(this);
        quitButton.setBounds(10,10,80,20);
        submitButton=new JButton("Submit");
        submitButton.setBounds(10,390,80,20);
        submitButton.addActionListener(this);
        label1 = new JLabel("Enter your name please.");
        label1.setBounds(10,30,150,20);
        Text1=new JTextField(10);
        Text1.setBounds(10,60,150,20);
        label2 = new JLabel("Enter your age please.");
        label2.setBounds(10,90,150,20);
        Text2=new JTextField(10);
        Text2.setBounds(10,120,150,20);
        label3 = new JLabel("Enter your favorite color please.");
        label3.setBounds(10,150,180,20);
        Text3=new JTextField(10);
        Text3.setBounds(10,180,150,20);
        label4 = new JLabel("PC or Console?");
        label4.setBounds(10,210,150,20);
        Text4=new JTextField(10);
        Text4.setBounds(10,240,150,20);
        label5 = new JLabel("Coke of Pepsi?");
        label5.setBounds(10,270,150,20);
        Text5=new JTextField(10);
        Text5.setBounds(10,300,150,20);
        label6 = new JLabel("McDonalds or Burger King?");
        label6.setBounds(10,330,170,20);
        Text6=new JTextField(10);
        Text6.setBounds(10,360,150,20);
        apanel.add(quitButton);
        apanel.add(submitButton);
        apanel.add(label1);
        apanel.add(label2);
        apanel.add(label3);
        apanel.add(label4);
        apanel.add(label5);
        apanel.add(label6);
        apanel.add(Text1);
        apanel.add(Text2);
        apanel.add(Text3);
        apanel.add(Text4);
        apanel.add(Text5);
        apanel.add(Text6);
        this.add(apanel);

        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(0, 1));

        JRadioButton e1=new JRadioButton("PC");
        JRadioButton e2=new JRadioButton("Console");
        JRadioButton e3=new JRadioButton("Burger King");
        JRadioButton e4=new JRadioButton("McDonalds");
        JRadioButton e5=new JRadioButton("Coke");
        JRadioButton e6=new JRadioButton("Pepsi");

        ButtonGroup bg1 = new ButtonGroup( );
        ButtonGroup bg2 = new ButtonGroup( );
        ButtonGroup bg3 = new ButtonGroup( );

        bg1.add(e1);
        bg1.add(e2);
        bg2.add(e3);
        bg2.add(e4);
        bg3.add(e5);
        bg3.add(e6);

        contentPane.add(e1);
        contentPane.add(e2);
        contentPane.add(e3);
        contentPane.add(e4);
        contentPane.add(e5);
        contentPane.add(e6);

        apanel.add(e1);
        apanel.add(e2);
        apanel.add(e3);
        apanel.add(e4);
        apanel.add(e5);
        apanel.add(e6);






        }


    public void actionPerformed(ActionEvent event){
     if(event.getSource()==quitButton){
         System.exit(0);
        }

     if(event.getSource()==submitButton){
        String a1="Steven";
        int a2=14;
        String a3="Green";
        String a4="PC";
        String a5="Coke";
        String a6="McDonalds";
        String b1=Text1.getText();
        String c2=Text2.getText();
        int b2= Integer.parseInt(c2);
        String b3=Text3.getText();
        String b4=Text4.getText();
        String b5=Text5.getText();
        String b6=Text6.getText();
        String d1="";
        if(a2>b2){d1="You are younger than me.";}
        else if(b2>a2){d1="You are older than me.";}
        else{d1="You are the same age as me.";};
        JOptionPane.showMessageDialog(frame,"Hello "+b1+". My name is "+a1+"."+d1);
        }
    }




}

Note: My program isn't complete either way. I still need to make the buttons functional and replace the textbox code that I wanted to improve on (which did work).

Andorino
  • 43
  • 4
  • 1
    `MyFirstGUIDriver` doesn't need to extend `JFrame` – MadProgrammer May 05 '15 at 12:17
  • 1
    Your code is a mess (sorry). `MyFirstGUI` extends `JFrame`, but you create ANOTHER `JFrame` within in it, to this (inner) instance, you seem to be adding your radio buttons, but this inner frame is never made visible. Instead of extending from `JFrame`, you should extend from `JPanel` and drop the inner `JFrame` altogether. You should use the "driver" to create an instance of a `JFrame` and then add your gui to it – MadProgrammer May 05 '15 at 12:20
  • 1
    Minimize your code and point out at what point it didn't _work_ anymore – Murat Karagöz May 05 '15 at 12:20
  • 2
    'null' will always come back to haunt you, don't use them – MadProgrammer May 05 '15 at 12:21
  • 1
    Also you are never adding `contentPane` to your frame. Idk man, you should start making your code readable. – Murat Karagöz May 05 '15 at 12:24
  • Oh, about the messy code: Like I said, the code was incomplete anyway. I didn't bother to take out the redundancies and make it look good etc. until after I got it running. Sorry for the mess, but I'm a newb; this is literally the first time I've made a program longer than 20 lines, and also the first time I made a legitimate one with a GUI. Also, it stops working when I add from the declaration of contentPane to the last apanel.add() statement. – Andorino May 05 '15 at 21:48

1 Answers1

-1

You need to setBounds for radioButtons Try the following and align as you want.

e1.setBounds(200, 260, 130, 23);
e2.setBounds(300, 260, 130, 23);
e3.setBounds(400, 260, 130, 23);
e4.setBounds(500, 260, 130, 23);
e5.setBounds(600, 260, 130, 23);
e6.setBounds(700, 260, 130, 23);
Pasupathi Rajamanickam
  • 1,982
  • 1
  • 24
  • 48
  • 1
    Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 05 '15 at 12:35