我创建了一个扩展JDialog的组件。它在JScrollPane中创建一个JPanel(大尺寸)。然后我添加RadioButtons和JLabels到JPanel为什么我的RadioButtons&JLabel不显示?
两个问题: 1 - RadioButtons和JLabels不显示。 2 - JScrollPane的的滚动条不显示
这里是我的代码:
public class xuggleJOptionPane1 extends JDialog{
Container pane;
JPanel paneMain;
JLabel ms1;
xuggleJOptionPane1 myFrame;
JPanel panel;
JScrollPane paneScroll;
JPanel paneScrollpanel;
public xuggleJOptionPane1(JFrame parent, String str, int nf)
{
super(parent, str);
myFrame = this;
myFrame.setPreferredSize(new Dimension(400, 160));
myFrame.setSize(new Dimension(400, 160));
panel = new JPanel();
panel.setSize(400, 160);
ms1 = new JLabel();
paneScroll = new JScrollPane();
paneScroll.setPreferredSize(new Dimension(380,100));
paneScrollpanel = new JPanel();
paneScrollpanel.setPreferredSize(new Dimension(1600, 1600));
//if i add this line the whole paneScrollpanel disappears
//paneScrollpanel.setSize(1600, 1600);
String pl ="";
if (nf != 1) pl = "es";
String s1 = "We found " + nf + " flux"+pl+". Which one do you wanna choose ?";
ms1.setText(s1);
ArrayList<JRadioButton> Buttons = new ArrayList<JRadioButton>();
ArrayList<JLabel> Labels = new ArrayList<JLabel>();
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < nf; i++)
{
Buttons.add(new JRadioButton());
Labels.add(new JLabel("test"));
paneScrollpanel.add(Buttons.get(i));
paneScrollpanel.add(Labels.get(i));
group.add(Buttons.get(i));
}
Buttons.get(0).setSelected(true);
paneScroll.add(paneScrollpanel);
panel.add(ms1);
panel.add(paneScroll);
myFrame.add(panel);
myFrame.setVisible(true);
myFrame.setResizable(false);
}
}
我不现在没有时间来回答你的问题,其他人可以肯定地做,但我可以告诉你的是你需要学习Java编码约定。类以caps开头,局部变量不应该这样做。因为你没有遵循惯例,像“Buttons.add(..”这样的行看起来像你使用的是“Buttons”类,并且使你的代码很难理解。阅读:http://www.oracle.com /technetwork/java/javase/documentation/codeconventions-135099.html#367 – bluehallu 2012-08-16 09:46:13
非常感谢你我将来会这样!对不起的命名约定 – IonOne 2012-08-16 12:00:16
没问题的伴侣,我们都是这样开始的;) – bluehallu 2012-08-16 14:52:56