简体   繁体   English

有没有更优雅的方式来创建秋千按钮和标签?

[英]Is there a more elegant way to create swing buttons and labels?

I'm still learning Java and am currently building a Multiple-Choice Quiz for a class assignment. 我仍在学习Java,目前正在为课程分配建立多项选择测验。 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);

use array for compact and faster result here is the solution to your code plzz give me positive reply if this works 使用数组以获得紧凑和更快的结果,这是您的代码的解决方案plzz如果有效,请给我积极的答复

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)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM