0

I'm still learning Java and am currently building a Multiple-Choice Quiz for a class assignment. I have the code written and it works great, but it feels like an over-long mess when I go back in to edit it. I would like to break it down into different classes and methods, but it seems to get more complicated as I try. Is there a better (more elegant) way to do this? Below is a piece of what I'm working with...

    /**
    * Initialize the contents of the frame.
    */
    private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 700, 700);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    Font smallFont = new Font("Bookman Old Style", Font.ITALIC, 12);
    Font smallBoldFont = new Font("Bookman Old Style", Font.BOLD, 12);
    Font medBoldFont = new Font("Bookman Old Style", Font.BOLD, 18);
    Font medBoldItalFont = new Font("Bookman Old Style", Font.BOLD + 
         Font.ITALIC, 18);

    JLabel lblSubjectQuiz = new JLabel("SUBJECT QUIZ");
    lblSubjectQuiz.setHorizontalTextPosition(SwingConstants.CENTER);
    lblSubjectQuiz.setVerticalTextPosition(SwingConstants.CENTER);
    lblSubjectQuiz.setHorizontalAlignment(SwingConstants.CENTER);
    lblSubjectQuiz.setBounds(10, 10, 664, 70);
    lblSubjectQuiz.setFont(new Font("Bookman Old Style", Font.BOLD, 28));

    JLabel lblQuestion = new JLabel();
    lblQuestion.setText("<html>" + 
         qAndA.getQuestion(randNum.qNumbers.get(qCount)) + "</html>");
    lblQuestion.setHorizontalTextPosition(SwingConstants.CENTER);
    lblQuestion.setVerticalTextPosition(SwingConstants.CENTER);
    lblQuestion.setHorizontalAlignment(SwingConstants.CENTER);
    lblQuestion.setFont(new Font("Bookman Old Style", Font.PLAIN, 18));
    lblQuestion.setBounds(10, 86, 664, 126);


    // Create radio buttons for the answer choices
    JRadioButton btnAnswerA = new JRadioButton();
    btnAnswerA.setFont(smallFont);
    btnAnswerA.setBounds(28, 252, 325, 35);
    btnAnswerA.setText(qAndA.getAnsA(randNum.qNumbers.get(qCount)));
    JRadioButton btnAnswerB = new JRadioButton();
    btnAnswerB.setFont(smallFont);
    btnAnswerB.setBounds(28, 292, 325, 35);
    btnAnswerB.setText(qAndA.getAnsB(randNum.qNumbers.get(qCount)));
    JRadioButton btnAnswerC = new JRadioButton();
    btnAnswerC.setFont(smallFont);
    btnAnswerC.setBounds(28, 332, 325, 35);
    btnAnswerC.setText(qAndA.getAnsC(randNum.qNumbers.get(qCount)));
    JRadioButton btnAnswerD = new JRadioButton();
    btnAnswerD.setFont(smallFont);
    btnAnswerD.setBounds(28, 372, 325, 35);
    btnAnswerD.setText(qAndA.getAnsD(randNum.qNumbers.get(qCount)));
    ButtonGroup radioButtons = new ButtonGroup();
    radioButtons.clearSelection();
    radioButtons.add(btnAnswerA);
    radioButtons.add(btnAnswerB);
    radioButtons.add(btnAnswerC);
    radioButtons.add(btnAnswerD);

    JLabel lblRightAns = new JLabel("<html>" + "Correct!" + "</html>");
    lblRightAns.setVisible(false);
    lblRightAns.setHorizontalAlignment(SwingConstants.CENTER);
    lblRightAns.setFont(medBoldItalFont);
    lblRightAns.setForeground(Color.GREEN);
    lblRightAns.setBounds(427, 240, 222, 64);
  • 3
    If you're using a UI builder, consider using a LayoutManager for sub-areas. JPanels given the Grid Layout can be used in a 1x5 layout to do 1 question and 4 lines of multiple choice radio buttons without any positioning information needed other than order of adding. – Compass Jul 12 '17 at 19:50
  • 2
    You could make some helper methods for creating JLabel and JRadioButton that take parameters for the stuff that changes. This also helps isolate the creation code to one place and makes fixing bugs/adding functionality easier. – Michael Krause Jul 12 '17 at 19:50
  • 1
    Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour), have a look around, and read through the [help center](http://stackoverflow.com/help), in particular [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [What topics can I ask about here?](http://stackoverflow.com/help/on-topic). you might consider posting this to https://codereview.stackexchange.com/ – Timothy Truckle Jul 12 '17 at 19:55
  • 1
    I'm voting to close this question as off-topic because belongs to https://codereview.stackexchange.com/ – Timothy Truckle Jul 12 '17 at 19:56
  • @Compass Thanks, I wasn't using a layout manager, I will look into it. – Daniel Hamar Jul 12 '17 at 20:10
  • @Michael Those were my thoughts, and that is what I'm attempting to do now, I appreciate the response. – Daniel Hamar Jul 12 '17 at 20:10
  • @TimothyTruckle Thanks, I'll look into codereview. – Daniel Hamar Jul 12 '17 at 20:11
  • Separate you model/data and the means by which you display it, then you can generate a "engine" which generates the UI based on the model information- [this answer](https://stackoverflow.com/questions/31602113/listener-placement-adhering-to-the-traditional-non-mediator-mvc-pattern/31604919#31604919) provides an example of a quiz implemented using a MVC - if you're willing to read through it – MadProgrammer Jul 12 '17 at 21:47

1 Answers1

-1

use array for compact and faster result here is the solution to your code plzz give me positive reply if this works

JRadioButton btnAnswer[]=new JRadioButton[4];
ButtonGroup radioButtons = new ButtonGroup();
radioButtons.clearSelection();
for (int i=0;i<4;i++){
btnAnswer[i].setFont(smallFont);
btnAnswer[i].setBounds(28, 252 + i*40, 325, 35);
radioButtons.add(btnAnswer[i]);
btnAnswer[i].setText(qAndA.getAnsB(randNum.qNumbers.get(qCount)));
}
  • Thanks, this will make the radio buttons easier to manage. I was close to this originally, but was having a hard time with the setBounds(). I will see about implementing this. Appreciate the help. – Daniel Hamar Jul 13 '17 at 12:22