关于动态添加button的问题 在一个combobox的下拉选择中,根据不同的选择来生成不同一组button用下面的方法生成不了JRadioButton b=new JRadioButton(); b.setName(); b.setText();panel.add(b);是怎么回事刚接触 swing不太明白 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 LZ得设置大小,坐标,还得设置 b.setVisible(true); 试试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); } }); }} 恩恩,对validate(),刷新一下 我加了validate还是不行我用的是netbean里面的设计模式先拖好frame,并画好不同的panel然后再在各个组件的事件里面加的代码,这样会有问题吗 现在我又加上大小,位置,还有button.setvisible(true)都还是不行在界面上看不到 嗯,可以显示了,自己刚刚代码 没写对,谢谢楼上各位但是现在又有新问题了,只能显示一组,再选combobox的其他值的时候显示的就是一样的,好像那个panel.removeall()没起作用,这又是怎么回事 找到原因了,要update一下才行,谢谢楼上各位 关于动态代理 限时折扣程序设计方式 JAVA数字签名用C++验证签名 关于JTextPane的光标问题 新手上路 请问高手netbeans形成的目标文件jar,是否把jar的应用程序变成能直接运行的exe文件 调查加请教:J2EE软件工程师应具备的核心技术能力 java连接oracle8练习程序,调试无错误,但无法运行出正确结果,请教为什么,源代码内详,捕捉异常内详 前辈求帮助一下,下面的代码为什么会错 value = value == null 这个语法 何解??? java用线程池丢任务了怎么办 java有趣的小题,无分看题
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);
}
});
}
}
我用的是netbean里面的设计模式先拖好frame,并画好不同的panel
然后再在各个组件的事件里面加的代码,这样会有问题吗
在界面上看不到
但是现在又有新问题了,只能显示一组,再选combobox的其他值的时候显示的就是一样的,好像那个panel.removeall()没起作用,这又是怎么回事