2016-03-07 24 views
1

以下时,进行中的GridBagLayout组件跨越多个行,你可以看到一个简单的例子,我尝试绘制泰国国旗:使用只有一列

image flag Thailand

此标志有一个特殊功能。蓝线比其他线路的两倍大:

public class GridBagLayoutTest { 

private static final Insets insets = new Insets(0, 0, 0, 0); 


public static void main(String args[]) { 

    final JFrame frame = new JFrame("Make a component span multiple rows in GridBagLayout when using only one column"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.setLayout(new GridBagLayout()); 

    JPanel row1 = new JPanel(); 
    row1.setBackground(new Color(237, 27, 36)); 

    JPanel row2 = new JPanel(); 
    row2.setBackground(Color.WHITE); 

    JPanel row3 = new JPanel(); 
    row3.setBackground(new Color(26, 29, 80)); 

    JPanel row4 = new JPanel(); 
    row4.setBackground(Color.WHITE); 

    JPanel row5 = new JPanel(); 
    row5.setBackground(new Color(237, 27, 36)); 

    //We are trying to draw Flag of Thailand 
    addComponent(frame, row1, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
    addComponent(frame, row2, 0, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
    addComponent(frame, row3, 0, 2, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
    addComponent(frame, row4, 0, 4, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
    addComponent(frame, row5, 0, 5, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 


    frame.setSize(500, 200); 
    frame.setVisible(true); 

} 


private static void addComponent(Container container, Component component, int gridx, int gridy, 
    int gridwidth, int gridheight, int anchor, int fill) { 
    GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0, 
                anchor, fill, insets, 0, 0); 
    container.add(component, gbc); 
} 


} 

我不想因为我想保留相同的行为,当我调整框架使用GridBagConstraint的财产ipady。 第三行(蓝色)的高度必须是其他尺寸的两倍。

我使用的是财产gridheight。对于蓝线,我把这个值赋给这个属性,但是什么也没有发生。

你有什么想法来实现这个目标吗?

+2

您可以随时在row3后添加另一个蓝色的行。没有真正回答你的问题,但会解决你的问题 – Ian2thedv

+0

我举了一个简单的例子,以便轻松地解释问题。实际上,它是一些JList或我操纵的某个JTextArea。因此,您的答案不能成为解决方案。 – Rafalsh

回答

4

在这里,你去。

Flag

当您使用GridBagLayout的,你有在Swing组件的大小的唯一控制与的weightx和沉重的参数。

我对代码做了一些修改,主要是为了排除其他问题。

这是代码。

package com.ggl.testing; 

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.Insets; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.SwingUtilities; 

public class GridBagLayoutTest implements Runnable { 

    private static final Insets insets = new Insets(0, 0, 0, 0); 

    public static void main(String args[]) { 
     SwingUtilities.invokeLater(new GridBagLayoutTest()); 
    } 

    @Override 
    public void run() { 
     JFrame frame = new JFrame(
       "Make a component span multiple rows in GridBagLayout when using only one column"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(createFlag()); 
     frame.pack(); 
     frame.setVisible(true); 

    } 

    private JPanel createFlag() { 
     JPanel panel = new JPanel(); 
     panel.setLayout(new GridBagLayout()); 
     panel.setPreferredSize(new Dimension(450, 300)); 

     JPanel row1 = new JPanel(); 
     row1.setBackground(new Color(237, 27, 36)); 

     JPanel row2 = new JPanel(); 
     row2.setBackground(Color.WHITE); 

     JPanel row3 = new JPanel(); 
     row3.setBackground(new Color(26, 29, 80)); 

     JPanel row4 = new JPanel(); 
     row4.setBackground(Color.WHITE); 

     JPanel row5 = new JPanel(); 
     row5.setBackground(new Color(237, 27, 36)); 

     // We are trying to draw Flag of Thailand 
     int gridy = 0; 
     addComponent(panel, row1, 0, gridy++, 1, 1, 1D, 1D, 
       GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     addComponent(panel, row2, 0, gridy++, 1, 1, 1D, 1D, 
       GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     addComponent(panel, row3, 0, gridy++, 1, 1, 1D, 2D, 
       GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     addComponent(panel, row4, 0, gridy++, 1, 1, 1D, 1D, 
       GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     addComponent(panel, row5, 0, gridy++, 1, 1, 1D, 1D, 
       GridBagConstraints.CENTER, GridBagConstraints.BOTH); 

     return panel; 
    } 

    private void addComponent(Container container, Component component, 
      int gridx, int gridy, int gridwidth, int gridheight, 
      double weightx, double weighty, int anchor, int fill) { 
     GridBagConstraints gbc = new GridBagConstraints(gridx, gridy, 
       gridwidth, gridheight, weightx, weighty, anchor, fill, insets, 
       0, 0); 
     container.add(component, gbc); 
    } 

} 
+0

清晰有效,谢谢! – Rafalsh