在一个combobox的下拉选择中,根据不同的选择来生成不同一组button用下面的方法生成不了
JRadioButton b=new JRadioButton();
 b.setName();
 b.setText();
panel.add(b);是怎么回事刚接触 swing不太明白

解决方案 »

  1.   

    LZ得设置大小,坐标,还得设置 b.setVisible(true);
      

  2.   

    试试validate()import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class TestFrame extends JFrame
    {
    private JComboBox comboBox = new JComboBox();
    private JPanel pCenter = new JPanel();
    private JPanel pSouth = new JPanel();

    public TestFrame()
    {
    setLayout(new BorderLayout());
    add(pCenter, BorderLayout.CENTER);
    add(pSouth, BorderLayout.SOUTH);

    pSouth.add(comboBox);
    comboBox.addItem("No Button");
    comboBox.addItem("One Button");
    comboBox.addItem("Two Button");
    comboBox.addItem("Three Button");
    comboBox.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    String str = (String) comboBox.getSelectedItem();
    if (str.equals("No Button"))
    {
    pCenter.removeAll();
    TestFrame.this.validate();
    TestFrame.this.pack();
    }
    else if (str.equals("One Button"))
    {
    pCenter.removeAll();
    pCenter.add(new JButton("1"));
    TestFrame.this.validate();
    TestFrame.this.pack();
    }
    else if (str.equals("Two Button"))
    {
    pCenter.removeAll();
    pCenter.add(new JButton("1"));
    pCenter.add(new JButton("2"));
    TestFrame.this.validate();
    TestFrame.this.pack();
    }
    else if (str.equals("Three Button"))
    {
    pCenter.removeAll();
    pCenter.add(new JButton("1"));
    pCenter.add(new JButton("2"));
    pCenter.add(new JButton("3"));
    TestFrame.this.validate();
    TestFrame.this.pack();
    }
    }
    });
    }

    public static void main(String[] args)
    {
    EventQueue.invokeLater(new Runnable()
    {
    public void run()
    {
    JFrame frame = new TestFrame();
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    });
    }
    }
      

  3.   

    恩恩,对validate(),刷新一下
      

  4.   

    我加了validate还是不行
    我用的是netbean里面的设计模式先拖好frame,并画好不同的panel
    然后再在各个组件的事件里面加的代码,这样会有问题吗
      

  5.   

    现在我又加上大小,位置,还有button.setvisible(true)都还是不行
    在界面上看不到
      

  6.   

    嗯,可以显示了,自己刚刚代码 没写对,谢谢楼上各位
    但是现在又有新问题了,只能显示一组,再选combobox的其他值的时候显示的就是一样的,好像那个panel.removeall()没起作用,这又是怎么回事
      

  7.   

    找到原因了,要update一下才行,谢谢楼上各位