【问题标题】:Display buttons properly gridbaglayout正确显示按钮 gridbaglayout
【发布时间】:2014-05-21 11:34:18
【问题描述】:

我正在做一个测验应用程序。当一个团队按下他们的按钮时,测验的管理员会弹出一个 jframe,表明团队按下了他们的按钮,在那个 JFrame 中有问题和答案,如果团队回答正确,他可以转到下一个问题,如果他们难道他不能继续这个问题。见图片:http://imgur.com/EtzH8Hq // 抱歉我还没有 10 声望。

由于此 JFrame 中的大部分文本都是动态的(您不知道一个问题有多少答案/选项等),因此我为此使用了 gridbaglayout。

在我添加按钮之前,一切都显示得很好,我想要右下角的恢复按钮和计算按钮,或者左下角的 1 和右下角的另一个。我尝试在我的代码中执行此操作,但它没有放在应有的位置。

有什么帮助吗?

这是我尝试执行此操作的代码:

public class GiveScoreView extends JFrame implements View {
Observable $model;
Controller $controller;

private Question $question; /* Saves the question that is passed by the update */

/*GUI elements */
ArrayList<JLabel> $answerLabels;
ArrayList<JCheckBox> $checkBoxes;
JLabel $questionLabel;
JLabel $teamPressedLabel;

/* 2 buttons to resume/end the question */
JButton $resumeButton;
JButton $calculateButton;

public GiveScoreView(Observable model, Controller controller) {

    setModel(model);
    setController(controller);

    $answerLabels = new ArrayList<JLabel>();
    $checkBoxes = new ArrayList<JCheckBox>();
    $questionLabel = new JLabel();

    $teamPressedLabel = new JLabel();

    $question = null;

    $resumeButton = new JButton("Resume question"); /* TODO messagebundle */
    $calculateButton = new JButton("Calculate score"); /* TODO messagebundle */

    /* Add actionlisteners to the buttons */
    $resumeButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if($question != null){
                if($question.getMediaPath() != null && QuizSoftwareModel.$vlcPath != null) /* If there is an audio/video piece */
                    ((GiveScoreController)getController()).resumeMP(); /* Resume the mediaplayer if there is an audio/video piece */

                ((GiveScoreController)getController()).resumeQuestion(); /* Resume the question */
                closeFrame(); /* Close the frame */
            }
        }
    });

    $calculateButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if($question != null){
                if($question.getMediaPath() != null && QuizSoftwareModel.$vlcPath != null) /* If there is an audio/video piece */
                    ((GiveScoreController)getController()).closeMP(); /* Close the mediaplayer */
                ((GiveScoreController)getController()).nextQuestion(); /* Go on to the next question */
                closeFrame(); /* Close the frame */
            }
        }
    });


    initializeFrame();
}

/**
 * Initializes the frame
 */
private void initializeFrame() {
    setTitle("Give a score to the teams"); /* TODO languagebundle, Change the title of the frame */

    getContentPane().setLayout(new GridBagLayout());/* Set the layout to gridbaglayout */

    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            dispose();
        }
    });

    pack();
    setVisible(false); /* Don't display it on default */

}

@Override
public void update(Observable arg0, Object arg1) {
    $question = (Question) arg1;

    /* Now we need to display this frame */
    if(((QuizModel) getModel()).getDisplayScoreView()){
        $teamPressedLabel.setText("Team " + Integer.toString(((QuizModel) getModel()).getTeamPressed()) + " is ready to answer!"); /* TODO messagebundle */
        $teamPressedLabel.setFont(new Font("Serif", Font.PLAIN, 34)); /* Change the font */
        displayScoreView();
        setVisible(true); /* Now display the JFrame */
    }
}




private void displayScoreView() {
    Multipleanswer multipleanswerQuestion; /* a multiple answer question to display the multipleanswer questions */
    Multiplechoice multiplechoiceQuestion; /* a multiple choice question to display the multiplechoice questions */
    ArrayList<String> answers = null;

    GridBagConstraints c = new GridBagConstraints();

    /* Set the position of the JFrame so it's centered */
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the window
    int w = getSize().width;
    int h = getSize().height;
    int x = (dim.width - w) / 2;
    int y = (dim.height - h) / 2;
    int i = 0;

    // Move the window
    setLocation(x - 150, y - 150);

    /* Set size */
    setSize(550, 300);


    /* If the question isn't empty */
    if (!($question == null)) {
        c.anchor = GridBagConstraints.NORTHWEST; 

        $questionLabel.setText($question.getQuestion()); /* Set the text of the JLabel to the question itself */
        $questionLabel.setFont(new Font("Serif", Font.PLAIN, 26)); /* Change the font */

        getContentPane().add($teamPressedLabel,c); /* Add the label to the JFrame, the team that has pressed it's button */

        c.weighty = 1.0;

        /* Display the question under the team pressed text */
        c.gridx = 0;
        c.gridy = 1;

        getContentPane().add($questionLabel,c); /* Add the label to the JFrame, the question itself */

        /* If the type of the question is multipleanswer */
        if ($question.getType() == QuestionType.MULTIPLEANSWER) {

            /* Cast it to multipleanswer question */
            multipleanswerQuestion = (Multipleanswer) $question;

            /* Get the answers */
            answers = multipleanswerQuestion.getAnswers();
        } else if ($question.getType() == QuestionType.MULTIPLECHOICE) {

            /* Cast it to multiplechoice question */
            multiplechoiceQuestion = (Multiplechoice) $question;

            /* Get the answers */
            answers = multiplechoiceQuestion.getAnswers();
        }

        /* Speed questions don't show answers so we only display answers if it's not a speed question */
        if ($question.getType() != QuestionType.SPEED) {
            /* Make a JLabel and JCheckBox for each answer */
            for (i = 0; i < answers.size(); i++) {
                $answerLabels.add(new JLabel(answers.get(i))); /* Make a new JLabel with answer string as text */
                $checkBoxes.add(new JCheckBox()); /* Make a new JCheckBox */

                $answerLabels.get(i).setFont(new Font("Serif", Font.PLAIN, 16));
                c.gridx = 0;
                c.gridy = i + 2;
                getContentPane().add($answerLabels.get(i),c); /* Add the label to the JFrame */
                c.gridx = 1;
                c.gridy = i + 2;
                getContentPane().add($checkBoxes.get(i),c); /* Add the checkbox to the JFrame */

            }
        }

        /* Place the buttons */
        c.gridx = 0;
        c.gridy = i + 2;


        getContentPane().add($resumeButton);

        c.gridx = 1;
        c.gridy = i + 2;

        getContentPane().add($calculateButton);
    }
}


/**
 * Closes the JFrame
 */
protected void closeFrame() {
    this.dispose();

}

【问题讨论】:

    标签: java swing jbutton layout-manager gridbaglayout


    【解决方案1】:

    您可以毫无限制地添加它们。

        /* Place the buttons */
        c.gridx = 0;
        c.gridy = i + 2;
    
        getContentPane().add($resumeButton);
    
        c.gridx = 1;
        c.gridy = i + 2;
    
        getContentPane().add($calculateButton);
    

    只需以这种方式添加(带有约束)并将它们添加到适当的锚 NORTHEAST

    getContentPane().add($resumeButton, c);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 2014-04-08
      • 2020-01-10
      • 1970-01-01
      • 2015-08-02
      • 2021-03-07
      相关资源
      最近更新 更多