是否有更优雅的方式来创建摇摆按钮和标签?

时间:2017-07-12 19:46:46

标签: java swing

我还在学习Java,目前正在为课堂作业建立多项选择测验。我已经编写了代码并且它工作得很好,但是当我重新编辑它时感觉就像是一个长时间的混乱。我想把它分解成不同的类和方法,但是当我尝试时它似乎变得更加复杂。有没有更好(更优雅)的方式来做到这一点?以下是我正在使用的内容...

    /**
    * 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);

1 个答案:

答案 0 :(得分:-1)

使用数组实现紧凑和更快的结果 这是您的代码的解决方案 如果有效,请给我积极的答复

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)));
}
相关问题