首页 > 解决方案 > 使用 GridBagLayout 在一个面板中设置多个组件,但我不想填满整个面板

问题描述

我正在开发一个使用 java swing 构建 GUI 的项目。目前我有一个Jpanel应该JFrame包含一个标签(问题)和一组按钮/文本字段(答案),它们可以根据被问到的问题而改变。我想让问题集中在顶部的某个地方,答案适当地集中在中间。目前我GridBagLayout用来设置JLabeland JButtons 但仍然没有成功。

public class QuestionPanel extends MoviePanel{

    private String question;

    public QuestionPanel(String type, Application app, String question) throws Exception{
        super(type, app);
        this.question = question;
        JLabel lab = new JLabel(this.question);

        this.getGbc().anchor = GridBagConstraints.PAGE_START;
        this.getGbc().fill = GridBagConstraints.HORIZONTAL;
        this.getGbc().weighty = 0.5;
        this.getGbc().gridx = 2;
        this.getGbc().gridy = 0;

        lab.setFont(new Font("Helvetica", Font.BOLD, 40));
        lab.setForeground(new Color(189, 189, 189, 255));
        add(lab, this.getGbc());

        getAnswers(type);
    }

    private void getAnswers(String type){
        if(type.equals("AgeRestriction")){
            this.getGbc().gridx=0;
            this.getGbc().gridy=1;
            add(new YesButton(this.getApp()),this.getGbc());

            this.getGbc().gridx = 3;
            this.getGbc().gridy = 1;
            add(new NoButton(this.getApp()),this.getGbc());
        }
    }
}

GridBagLayout正在设置super MoviePanelthis.getGbc()返回适当的GridBagConstraints。目前我的布局是这样的:

布局

这个想法是让“是”和“否”按钮彼此靠近,并且未来按钮动态位于中心。

解决方案

我最终通过结合angushjoshic0der的答案来解决,因此在 questionPanel 中添加了一个 answerPanel 和 GridBagLayout。我的代码现在看起来像这样:

public class QuestionPanel extends MoviePanel {

    private String question;
    private JPanel answerPanel = new JPanel();

    public QuestionPanel(String type, Application app, String question) throws Exception {
        super(type, app);
        this.question = question;
        JLabel lab = new JLabel(this.question);
        answerPanel.setOpaque(false);


        this.getGbc().anchor = GridBagConstraints.PAGE_START;
        this.getGbc().weighty = 1;
        this.getGbc().gridy = 0;
        this.getGbc().gridx = 2;
        this.getGbc().gridwidth = 3;

        lab.setFont(new Font("Helvetica", Font.BOLD, 40));
        lab.setForeground(new Color(189, 189, 189, 255));
        add(lab, this.getGbc());

        getAnswers(type);
    }

    public void getAnswers(String type) {
        if (type.equals("AgeRestriction")) {
            createYesNoButtons();
        }
        if (type.equals("Age")) {
            createAgeField();
        }
        if (type.equals("Genre")){
            createGenreButtons();
        }
        if (type.equals("Actor")){
            createActorField();
        }
    }


    private void createYesNoButtons() {
        this.getGbc().weighty = 0.5;
        this.getGbc().anchor = GridBagConstraints.CENTER;
       answerPanel.add(new YesButton(this.getApp()));
       answerPanel.add(new NoButton(this.getApp()));
       this.add(answerPanel,this.getGbc());
    }

    private void createAgeField() {
        this.getGbc().weighty = 0.5;
        this.getGbc().anchor = GridBagConstraints.CENTER;
        answerPanel.add(new AgeField(this.getApp()));
        this.add(answerPanel,this.getGbc());
    }

    private void createGenreButtons(){
    }

    private void createActorField(){
    }

}

现在面板看起来像这样:

布局

谢谢您的帮助!

标签: javaswinglayoutjpanelgridbaglayout

解决方案


很难为您的问题提供一个准确的解决方案,但我可以告诉您,您可能想要使用GridBagConstraints.insetsand GridBagConstraints.anchor

insets用于更改布局中对象周围的填充,即距 JPanel 边缘的最小距离。你可以这样使用它:

this.getGbc().insets = new Inset(top,right,bottom,left);

anchor用于表示您希望将对象放置在布局中的哪个位置,例如: this.getGbc().anchor = GridBagConstraints.CENTER;

以下是一些有用的GridBagConstraints.anchor值:

FIRST_LINE_START ... PAGE_START ... FIRST_LINE_END

LINE_START ... CENTER ... LINE_END

LAST_LINE_START ... PAGE_END ... LAST_LINE_END


推荐阅读