javaswinguser-interfacemultiple-choice

How to Make a Multiple Choice in Java GUI's


This program is supposed to be for someone who wants to make a reservation for a banquet hall and asks the user to answer some simple questions about the event. For the meal plan, room type, and 10 days questions, I want to just have two options that the user can choose, and the user to click one of the options. So like a multiple choice. I tried looking stuff up but all the tutorials I found were for the console version of a multiple choice quiz. I know that one way to do this is by using JButtons, but I wanted to know if there was a more user-friendly way to do this.

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
abstract class Main implements ActionListener{
  public static void main(String[] args) {
    
        JFrame frame = new JFrame("Catering Company");
        frame.setSize(770,490);
        frame.setVisible(true);

        JPanel panel = new JPanel();
        panel.setLayout(null);

        JLabel DayLabel = new JLabel("What day of the week is it?:");
        DayLabel.setBounds(150, 80, 200, 20);
        JTextField DayLabelTXT = new JTextField(5);
        DayLabelTXT.setBounds(360, 80, 70, 25);

        JLabel AdultCount = new JLabel("How many adults are there?:");
        AdultCount.setBounds(145, 120, 220, 20);
        JTextField AdultCountTXT = new JTextField(5);
        AdultCountTXT.setBounds(360, 120, 70, 25);

        JLabel ChildCount = new JLabel("How many children are there?:");
        ChildCount.setBounds(134, 160, 225, 20);
        JTextField ChildCountTXT = new JTextField(5);
        ChildCountTXT.setBounds(360, 160, 70, 25);

        JLabel MealPlan = new JLabel("Choose a meal plan:");
        MealPlan.setBounds(208, 200, 200, 20);


        JLabel room = new JLabel("Which room would you like to choose?:");
        room.setBounds(78, 240, 280, 20);

        JLabel TenDays = new JLabel("Will you pay within the next 10 days?:");
        TenDays.setBounds(85, 280, 275, 20);

        JLabel initialDep = new JLabel("What is your initial deposit:");
        initialDep.setBounds(155, 320, 200, 20);
        JTextField initialDepTXT = new JTextField(5);
        initialDepTXT.setBounds(360, 320, 70, 25);



        panel.add(DayLabel);
        panel.add(DayLabelTXT);

        panel.add(AdultCount);
        panel.add(AdultCountTXT);

        panel.add(ChildCount);
        panel.add(ChildCountTXT);

        panel.add(MealPlan);

        panel.add(room);

        panel.add(TenDays);

        panel.add(initialDep);
        panel.add(initialDepTXT);

        frame.add(panel);
  }
}

Solution

  • I think you have multi option to do that, you can use JRadioButton, JComboBox or JCheckBox, because you have multichoice I think JCheckBox is the good one for you, take a look at this example from https://docs.oracle.com/ :

    //In initialization code:
    chinButton = new JCheckBox("Chin");
    chinButton.setMnemonic(KeyEvent.VK_C); 
    chinButton.setSelected(true);
    
    glassesButton = new JCheckBox("Glasses");
    glassesButton.setMnemonic(KeyEvent.VK_G); 
    glassesButton.setSelected(true);
    
    hairButton = new JCheckBox("Hair");
    hairButton.setMnemonic(KeyEvent.VK_H); 
    hairButton.setSelected(true);
    
    teethButton = new JCheckBox("Teeth");
    teethButton.setMnemonic(KeyEvent.VK_T); 
    teethButton.setSelected(true);
    

    Result :

    enter image description here