簡體   English   中英

從視圖gridbaglayout的頂部開始

[英]Start at the top of the view gridbaglayout

我正在申請測驗。 當一個團隊按下他們的按鈕時,我會顯示一個視圖,該視圖顯示哪個團隊按下了他們的按鈕以及問題的答案。 管理員需要選中團隊回答的每個復選框。 適當的分數將在代碼中計算出來並提供給團隊。

問題是,當我顯示視圖時,文本(由JLabels組成)沒有顯示在視圖的頂部。 它們更加居中。 見圖片:

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;

您需要將非零權weighty應用於底行中的組件。 默認情況下,當所有組件的權重為零時,會將任何備用空間放置在整個布局的外部,從而獲得您所看到的效果。 通過應用非零權重,您可以告訴布局引擎將剩余空間放在網格的單元內而不是外部。

您當前的網格有效

當前網格

而你需要的是

最下面一行的weighty = 1

您可以通過將weighty設置為1.0並將anchorNORTHWEST來實現,僅針對最后一行組件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM