从视图gridbaglayout的顶部开始

时间:2014-05-20 09:37:34

标签: java swing jframe layout-manager gridbaglayout

我正在进行测验申请。当团队按下按钮时,我会显示一个视图,显示哪个团队按下了他们的按钮以及问题的答案。管理员需要检查团队回答的每个复选框。将在代码中计算出适当的分数并将其提供给团队。

问题在于,当我显示视图时,文本(由JLabel组成)不会显示在视图的顶部。他们更集中。见图:

http://imgur.com/Pp3scVT //图片

这是我的代码:

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;

public GiveScoreView(Observable model, Controller controller) {

    setModel(model);
    setController(controller);

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

    $teamPressedLabel = new JLabel();

    initializeFrame();
}

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

    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()) + " pushed their button!"); /* 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;

    // 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.WEST;


        $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 */

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

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

        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 (int 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;
                add($answerLabels.get(i),c); /* Add the label to the JFrame */
                c.gridx = 1;
                c.gridy = i + 2;
                add($checkBoxes.get(i),c); /* Add the checkbox to the JFrame */

            }
        }
    }
}

解决方案:
weighty = 1;之后放置c.anchor = GridBagConstraints.WEST;

1 个答案:

答案 0 :(得分:0)

您需要将非零weighty应用于底行的组件。默认情况下,当所有组件的权重为零时,任何备用空间都放置在整个布局的外部,从而产生您看到的效果。通过应用非零权重,您可以告诉布局引擎将备用空间放在网格的单元格内而不是外部。

您当前的网格实际上是

current grid

而你需要的是

grid with weighty=1 on the bottom row

您可以通过将weighty设置为1.0并将anchor设置为NORTHWEST来实现最后一行组件。