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).