Java GridBagLayout组件不会到达指定位置

问题描述 投票:0回答:1

对于一个学校项目,我想创建一个GUI,其中显示服务器的一些统计信息。我想使用GridBagLayout将框指定为不同的统计信息。下图是我想要的GUI布局是This is how I want the GUI layout to look like

但是当我运行代码时,它看起来像这样:What the GUI looks like right now

文本应使用背景色填充其区域,并在第一张图片中所示的指定区域中。

这是我正在使用的代码:

package Monitoring;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MonitoringGui extends JFrame implements ActionListener {

    private Configuratie config;
    private MonitoringPanel mp;

    public MonitoringGui(Configuratie config) {

        this.config = config;

        setTitle("MonitoringGui");
        setSize(850, 600);
        setLayout(new GridBagLayout());
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //      onderdelen toevoegen en verwijderen
        JPanel selectorPanel = new JPanel();
        selectorPanel.setLayout(new GridBagLayout());
        selectorPanel.setBorder(BorderFactory.createLineBorder(Color.black));
        addElement(selectorPanel, 0, 0, 4, 2, 0.2, 1, new int[]{20, 20, 20, 20});

//      output en input area
        JPanel drawArea = new JPanel();
        selectorPanel.setLayout(new GridBagLayout());
        drawArea.setBackground(Color.lightGray);
        addElement(drawArea, 5, 0, 10, 1, 1, 1, new int[]{20, 0, 0, 20});

//      buttons, etc
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridBagLayout());
        addElement(buttonPanel, 5, 1, 10, 1, 1, 0.2, new int[]{0, 0, 20, 20});


        //grafiek area
        JPanel grafiekArea = new JPanel();
        grafiekArea.setLayout(new GridBagLayout());
        grafiekArea.setBackground(Color.CYAN);
        grafiekArea.add(new JLabel("GrafiekArea"));
        addElement(grafiekArea, 0, 0, 3, 1, 1, 0, new int[]{20, 20, 20, 20}, drawArea);

        //beschikbaar area
        JPanel beschArea = new JPanel();
        beschArea.setLayout(new GridBagLayout());
        beschArea.setBackground(Color.ORANGE);
        beschArea.add(new JLabel("BeschArea"));
        addElement(beschArea, 0, 1, 1, 1, 0, 0, new int[]{20, 20, 20, 20}, drawArea);

        //uptime area
        JPanel uptimeArea = new JPanel();
        uptimeArea.setLayout(new GridBagLayout());
        uptimeArea.setBackground(Color.RED);
        uptimeArea.add(new JLabel("UptimeArea"));
        addElement(uptimeArea, 2, 1, 1, 1, 0, 0, new int[]{20, 20, 20, 20}, drawArea);

        //schijfruimte area
        JPanel schijfruimteArea = new JPanel();
        schijfruimteArea.setLayout(new GridBagLayout());
        schijfruimteArea.setBackground(Color.GREEN);
        schijfruimteArea.add(new JLabel("Schijfruimte"));
        addElement(schijfruimteArea, 3, 1, 1, 1, 0.2, 0.2, new int[]{20, 20, 20, 20}, drawArea);

        setVisible(true);
    }

    public void addElement(Component comp, int x, int y, int width, int height, double weightx, double weighty, int[] insets) {
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.gridx = x;
        c.gridy = y;
        c.gridheight = height;
        c.gridwidth = width;
        c.weightx = weightx;
        c.weighty = weighty;
        c.insets = new Insets(insets[0], insets[1], insets[2], insets[3]);
        add(comp, c);
    }

    public void addElement(Component comp, int x, int y, int width, int height, double weightx, double weighty, int[] insets, JPanel panel) {
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.gridx = x;
        c.gridy = y;
        c.gridheight = height;
        c.gridwidth = width;
        c.weightx = weightx;
        c.weighty = weighty;
        c.insets = new Insets(insets[0], insets[1], insets[2], insets[3]);
        panel.add(comp, c);
    }
}



java swing jpanel layout-manager gridbaglayout
1个回答
0
投票

我想出了答案。为了使这种布局适合我的应用程序,我必须在每个面板中指定不同面板的大小。这些是我必须添加的代码行:

selectorPanel.setPreferredSize(new Dimension(120,600));
drawArea.setPreferredSize(new Dimension(660, 600));
grafiekArea.setPreferredSize(new Dimension(670,400));
beschArea.setPreferredSize(new Dimension(220,200));
uptimeArea.setPreferredSize(new Dimension(220,200));
schijfruimteArea.setPreferredSize(new Dimension(220,200));
© www.soinside.com 2019 - 2024. All rights reserved.