2017-01-28 63 views
-2
  1. 我遇到了此布局的问题。如何制作第二个按钮2x2,尺寸为网格宽度GridBagLayout组件放置

  2. 我也想知道,如果我想拉伸第二个按钮为两列宽,我该怎么说从哪里开始以及在哪里停止?如何区分覆盖第2列和第3列的第1列和第2列?

    所有的
    public class SwingExample { 
        public static void main(String[] args) { 
        JFrame theFrame = new JFrame("Grid Bag Example"); 
        theFrame.setSize(600,400); 
        theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    
        JPanel thePanel = new JPanel(); 
        theFrame.add(thePanel); 
    
        JButton button1 = new JButton("1"); 
        JButton button2 = new JButton("2"); 
        JButton button3 = new JButton("3"); 
    
        GridBagLayout gbl = new GridBagLayout();            
        thePanel.setLayout(gbl); 
        GridBagConstraints gbc = new GridBagConstraints(); 
        gbc.insets = new Insets(10, 10, 10, 10); 
    
        gbc.gridx = 0; 
        gbc.gridy = 0;     
        thePanel.add(button1,gbc); 
    
        gbc.gridx = 1; 
        gbc.gridy = 1;  
        thePanel.add(button2,gbc); 
    
        gbc.gridwidth = 2; 
        gbc.gridheight = 2; 
        gbc.fill = GridBagConstraints.HORIZONTAL; 
        gbc.fill = GridBagConstraints.VERTICAL; 
        gbc.gridx = 3; 
        gbc.gridy = 3; 
        thePanel.add(button3,gbc); 
    
        theFrame.setVisible(true); 
        } 
    } 
    
+0

网格宽度相对于这些列中的其他组件。你只有3列。您不能只让第三个按钮的宽度为2,因为它是第三列中的唯一按钮。如果将第三个按钮放在第0列中,则可以将其设置为两个宽度,因为现在它可以占用前两列中按钮的宽度。 – camickr

+0

您能否详细说明该句子 - _gridwidth是相对于这些列中的其他组件的__。我不明白这一点。 [Oracle](https://docs.oracle.com/javase/8/docs/api/java/awt/GridBagConstraints.html#gridwidth)明确指出它_指定组件显示区域的行中单元格的数量._看起来很简单明了。 – user7381956

+0

第4列中没有组件,因此您不能说第3列中的组件可以跨越第3列和第4列。我给出了组件跨多个列的示例。阅读Swing教程[如何使用GridBagLayout [(http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html)以获取更多信息和工作示例。 – camickr

回答

0

首先你运行本教程中的演示代码???

我会尝试解释教程代码,以便更好地理解。它说明如何使用gridwidth约束,使得:

  1. 在第二行按钮跨越第三行3列
  2. 按钮跨越2列开始的第二列。

GridBagLayout首先需要确定每个包含组件的列的宽度。因此找到每个列的最大组件。

现在您已知道每列中最大组件的大小,您可以通过累加列宽来确定第2行和第3行按钮的大小。第2行中的按钮将添加所有3列的宽度。第3列中的按钮将仅添加最后两列的宽度。

而且你到底会在第4栏组件有如果你想有一个组件跨越多个列与部件3

的行为,并跨越做的,然后一些其他行都必须有一个分量该列可以确定列的宽度。仅仅因为你使用了gridwidth = 2并不意味着这个按钮的大小会自动增加一倍。

gbc.gridx = 3;

在上面,你不能只是有按钮3奇迹般地在第3栏显示有在COLUMN2没有组件那么第2列的宽度将为0

有没有窍门一些虚拟,不可见,零维组件

那么您不能使用零维组件,因为那么宽度/高度将为零,这意味着列/行大小也将为零。所以你可以添加一个大小的虚拟组件。例如:

import java.awt.*; 
import javax.swing.*; 

public class SwingExample { 
    public static void main(String[] args) { 
    JFrame theFrame = new JFrame("Grid Bag Example"); 
    theFrame.setSize(600,400); 
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    JPanel thePanel = new JPanel(); 
    theFrame.add(thePanel); 

    JButton button1 = new JButton("1"); 
    JButton button2 = new JButton("2"); 
    JButton button3 = new JButton("3"); 

    JLabel label1 = new JLabel(); 
    label1.setPreferredSize(button1.getPreferredSize()); 
    JLabel label2 = new JLabel(); 
    label2.setPreferredSize(button1.getPreferredSize()); 

    GridBagLayout gbl = new GridBagLayout(); 
    thePanel.setLayout(gbl); 
    GridBagConstraints gbc = new GridBagConstraints(); 
    gbc.insets = new Insets(10, 10, 10, 10); 
    gbc.fill = GridBagConstraints.BOTH; 

    gbc.gridx = 0; 
    gbc.gridy = 0; 
    thePanel.add(button1,gbc); 

    gbc.gridx = 1; 
    gbc.gridy = 1; 
    thePanel.add(button2,gbc); 

    gbc.gridx = 2; 
    gbc.gridy = 2; 
    thePanel.add(label1,gbc); // dummy component 

    gbc.gridx = 3; 
    gbc.gridy = 3; 
    thePanel.add(label2,gbc); // dummy component 

    gbc.gridx = 2; 
    gbc.gridy = 2; 
    gbc.gridwidth = 2; 
    gbc.gridheight = 2; 
    thePanel.add(button3,gbc); 

    theFrame.setVisible(true); 
    } 
} 

在出任何标签添加到面板,你会看到它停止工作,因为没有组件占据单个电池的列/行以上代码注释。这是我在说明网格宽度相对于列中其他组件时要解释的。

+0

那么这里没有实际的网格?我不做任何网格,然后扔在组件?只添加了组件,然后我想它们会形成一个网格,然后尝试通过虚构网格来操纵它们? – user7381956

+0

通过向网格添加组件,可以使网格尽可能大。如果您没有将组件添加到网格中的某个单元格,那么该单元格的大小为(0,0)。我不知道如何更好地解释它。要理解的唯一方法就是在教程链接和上面的代码中使用已给出的工作示例。 – camickr

+0

好的,会试试。谢谢。 – user7381956

相关问题