正确显示按钮gridbaglayout

时间:2014-05-21 11:34:19

标签: java swing jbutton layout-manager gridbaglayout

我正在进行测验申请。当一个团队按下他们的按钮时,测验的管理员得到一个弹出的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();

}

1 个答案:

答案 0 :(得分: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);