我的目的是想在我所写的这个JPanel组件中用GridBagLayout布局
在这个JPanel共分为4列,让radio显示在一列上,让message显示在其余的三列上,
或者JPanel分为2列,radio和各占一列,但是要让radio占用的空间小些,让message占用的空间大些,我一下的代码是分为4列的情况,但是显示出来的是radio和message显示到一起去了,好像就占用了一列,并且居中显示了
怎样改才能达到我要求的效果呢!package org.compiere.apps.sugar;import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JFrame;
import javax.swing.JRadioButton;import org.compiere.swing.JLabel;
import org.compiere.swing.JPanel;
import org.compiere.util.Language;public class CApprovalRadioButton extends JPanel implements ActionListener{ private static final long serialVersionUID = 1L;
private JRadioButton radio = null;
private JLabel message = null;
private int approval_id;
private Dimension demension = null;  //  @jve:decl-index=0:
private String TableName;  //  @jve:decl-index=0:
private GridBagConstraints m_gbc = new GridBagConstraints();  //  @jve:decl-index=0:
private final Insets  m_zeroInset = new Insets(0,0,0,0);
private final boolean       m_leftToRight = Language.getLoginLanguage().isLeftToRight();
/** Label Inset             */
private final Insets  m_labelInset =
m_leftToRight ? new Insets(2,12,3,0) : new Insets(2,5,3,0);     //  top,left,bottom,right  //  @jve:decl-index=0:
/** Field Inset             */
private final Insets  m_fieldInset =
m_leftToRight ? new Insets(2,5,3,0)  : new Insets(2,12,3,0); //  top,left,bottom,right  //  @jve:decl-index=0:
/**
 * This is the default constructor
 */
public CApprovalRadioButton(int index,String ap_msg,int approval_id,String TableName) {
super(new GridBagLayout());
m_gbc.gridy = 0; // line
m_gbc.gridx = 0;
m_gbc.gridheight = 1;
m_gbc.gridwidth = 1;
m_gbc.insets = m_zeroInset;
m_gbc.weightx = 0;
m_gbc.weighty = 0;
m_gbc.ipadx = 0;
m_gbc.ipady = 0;
this.TableName = TableName;
this.approval_id = approval_id;
initialize(index,ap_msg);
}
/**
 * This method initializes this
 * 
 * @return void
 */
private void initialize(int index,String ap_msg) {
radio = new JRadioButton();
radio.setText("[" + index + "]");
radio.addActionListener(this);
m_gbc.gridy = 0;
m_gbc.gridx = 0;
m_gbc.gridwidth = 1;
m_gbc.insets = m_fieldInset;
this.add(radio,m_gbc);
message = new JLabel();
message.setText(ap_msg);
m_gbc.gridx = 1;
m_gbc.gridwidth = 3;
this.add(message,m_gbc);
} public int getValue(){
return approval_id;
}
/**
 * This method initializes radio
 * 
 * @return javax.swing.JRadioButton
 */
public JRadioButton getRadio() {
return radio;
}

public void actionPerformed(ActionEvent e) {
System.out.println(getValue());
} public Dimension getPreferredSize(){
if(demension != null)
return demension;
return new Dimension(200,20);
}

public void setFont(Font font){
if(radio != null){
radio.setFont(font);
}
if(message != null){
message.setFont(font);
}
}

public void setBackground(Color bg){
if(radio != null){
radio.setBackground(bg);
}
if(message != null){
message.setBackground(bg);
}
}

public static void main(String[] args) {
JFrame f = new JFrame("test");
f.setLayout(new GridLayout(3,1));
CApprovalRadioButton c1 = new CApprovalRadioButton(1,"111111111",111111,"");
CApprovalRadioButton c2 = new CApprovalRadioButton(2,"222222222",222222,"");
CApprovalRadioButton c3 = new CApprovalRadioButton(3,"333333333",333333,"");
f.add(c1);
f.add(c2);
f.add(c3);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

解决方案 »

  1.   

    你可以先把整个界面创建4个GridBagLayout,把它们的坐标(gridx,gridy)设成(0,1)(0,2)(0,3)(0,4),然后把它们的横向占用比例(weightx)都设成1.0,然后在这4个GridBagLayout上在各创建一些GridBagLayout来布局你想要的东西,GridBagLayout是JAVA里我非常喜欢的布局方式,因为它比较灵活,希望我这样说能对你有所帮助。
      

  2.   

    错了 修正一下 是创建GridBagConstraints