请看下面代码package test;import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;public class Test extends JPanel implements ActionListener{ private static final long serialVersionUID = 1L;
private JRadioButton radio = null;
private JLabel message = null;
private String approval_id = null; /**
 * This is the default constructor
 */
public Test(String value) {
super();
initialize(value);
} /**
 * This method initializes this
 * 
 * @return void
 */
private void initialize(String value) {
approval_id = value;
message = new JLabel();
message.setText("<html>messagenfsfdfertertertertetertertertertsfsdfsdf<html>");
message.setBounds(50, 0, 200, 20);
this.setSize(331, 31);
radio = new JRadioButton();
radio.addActionListener(this);
radio.setBounds(0, 0, 20, 20);
this.setLayout(null);
this.add(radio);
this.add(message);
this.setSize(200,20);
} /**
 * This method initializes radio
 * 
 * @return javax.swing.JRadioButton
 */
private JRadioButton getRadio() {
return radio;
}

public String getValue(){
return approval_id;
}

public static void main(String[] args) {
JFrame f = new JFrame();
f.setLayout(new GridLayout(3,1));
ButtonGroup bgroup = new ButtonGroup();

Test t1 = new Test("11111111");
bgroup.add(t1.getRadio());
Test t2 = new Test("22222222");
bgroup.add(t2.getRadio());
Test t3 = new Test("333333333");
bgroup.add(t3.getRadio());
f.setSize(600,400);
f.add(t1);
f.add(t2);
f.add(t3);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
} public void actionPerformed(ActionEvent e) {
System.out.println(getValue());
}
}package test;import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;public class GridBagWithContaints { public static void main(String[] args) {
JFrame f = new JFrame(
"Demonstrates the use of gridx, gridy,ipadx, ipady and insets constraints");
JPanel p = new JPanel(); p.setLayout(new GridBagLayout());
// creates a constraints object
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(2, 2, 3, 3); // insets for all components
c.gridx = 0; // column 0
c.gridy = 0; // row 0
c.ipadx = 10; // increases components width by 10 pixels
c.ipady = 10; // increases components height by 10 pixels
p.add(new JButton("Java"), c); // constraints passed in
c.gridx = 1; // column 1
// c.gridy = 0; // comment out this for reusing the obj
c.ipadx = 0; // resets the pad to 0
c.ipady = 0;
p.add(new JButton("Source"), c);
c.gridx = 0; // column 0
c.gridy = 1; // row 1
p.add(new JButton("and"), c);
c.gridx = 1; // column 1
p.add(new JButton("sdfsfdsdf"), c);
c.gridy = 2;
c.gridx = 0;
p.add(new Test("sdfsfdsdf"),c); f.getContentPane().add(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(600, 200);
f.setVisible(true);
}
}
为什么红色标记的地方不能显示该面板上呢,
Test是一个组件,包含一个radio按钮和一个JLabel
怎样才能显示出来呢!

解决方案 »

  1.   

    答:很简单,你在Test类中加入如下代码就行了:public Dimension getPreferredSize() {
    // TODO Auto-generated method stub
    return new Dimension(200,20);
    }原因是:当你定制自己的GUI组件(如:Test)时,要记住重写JComponent的public Dimension getPreferredSize() 方法,这样在布局时,布局管理器会首先选择你的这一个getPreferredSize() 返回的尺寸大小.