Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java的GridBagLayout中使网格不同?_Java_User Interface_Gridbaglayout - Fatal编程技术网

如何在Java的GridBagLayout中使网格不同?

如何在Java的GridBagLayout中使网格不同?,java,user-interface,gridbaglayout,Java,User Interface,Gridbaglayout,我想要实现的是这样的目标: 这是我的代码: JDialog messageDialog = new JDialog(); messageDialog.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); messageDialog.setBounds(0, 0, 350, 250); messageDialog.setLocationRelativeTo(null); messageDia

我想要实现的是这样的目标:

这是我的代码:

JDialog messageDialog = new JDialog();
messageDialog.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
messageDialog.setBounds(0, 0, 350, 250);
messageDialog.setLocationRelativeTo(null);
messageDialog.setVisible(true);

JPanel btnPanel = new JPanel();
JPanel clearPanel = new JPanel();
JPanel labelsPanel = new JPanel();
JPanel txtPanel = new JPanel();
JButton newMessage = new JButton("New");
JButton recievedMessages = new JButton("Recieved");
JButton sendMessages = new JButton("Sent");
JButton refreshMessages = new JButton("Refresh");
JLabel recievedMessLab = new JLabel("Messages get:");
JTextPane txtToSend = new JTextPane();

btnPanel.setLayout(new GridLayout(4, 1));
btnPanel.add(newMessage);
btnPanel.add(recievedMessages);
btnPanel.add(sendMessages);
btnPanel.add(refreshMessages);

c.gridx = 0;
c.gridy = 0;
messageDialog.add(clearPanel, c);

c.gridx = 1;
c.gridy = 0;
messageDialog.add(labelsPanel, c);

c.gridx = 0;
c.gridy = 1;
messageDialog.add(btnPanel, c);

c.gridx = 1;
c.gridy = 1;
messageDialog.add(txtPanel, c);

labelsPanel.add(recievedMessLab);
我不知道为什么我在所有面板周围都有一些空闲空间,我不知道如何调整网格的大小。Oracle教程也没有帮助。调整大小的最简单方法是什么?如何消除这些可用空间?

您需要将权重和填充信息添加到GridBagConstraints中,以便布局管理器知道要在可用空间上分布哪些组件

请尝试以下操作:

c.gridx = 0;
c.gridy = 0;
c.fill = c.NONE;  // dont fill (strech)
messageDialog.add(clearPanel, c);

c.gridx = 1;
c.gridy = 0;
c.weightx = 1; // horizontal weight: 1
c.fill = c.HORIZONTAL; // fill (strech) horizontally
messageDialog.add(labelsPanel, c);

c.gridx = 0;
c.gridy = 1;
c.weightx = 0; // horizontal weight: back to 0
c.weighty = 1; // vertical weight: 1
c.fill = c.VERTICAL; // fill (strech) vertically
messageDialog.add(btnPanel, c);

c.gridx = 1;
c.gridy = 1;
c.weightx = 1; // both weights: 1
c.weighty = 1; // both weights: 1
c.fill = c.BOTH; // and fill both ways, vertically and horizontally
messageDialog.add(txtPanel, c);
重温关于weightx和weighty的部分,并填写教程以了解它们是如何工作的

PS:txtPanel是空的,txtToSend从未使用过