Java GridBagLayout组件不会到达指定位置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java GridBagLayout组件不会到达指定位置相关的知识,希望对你有一定的参考价值。
对于一个学校项目,我想创建一个GUI,其中显示服务器的一些统计信息。我想使用GridBagLayout将框指定为不同的统计信息。下图是我想要的GUI布局是
文本应使用背景色填充其区域,并在第一张图片中所示的指定区域中。
这是我正在使用的代码:
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);
}
}
答案
我想出了答案。为了使这种布局适合我的应用程序,我必须在每个面板中指定不同面板的大小。这些是我必须添加的代码行:
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));
以上是关于Java GridBagLayout组件不会到达指定位置的主要内容,如果未能解决你的问题,请参考以下文章
在 Java 中使用 GridBagLayout 时防止 Swing 组件偏离中心
JSeparator 不会与 GridBagLayout 一起显示
java gridbaglayout 一个组件变化会影响其它组件的大小,能不能做一个设置能让每个组件的大小和位置固定呢