为什么我的RadioButtons& JLabels展示?

时间:2012-08-16 09:38:17

标签: java swing jpanel jscrollpane

我创建了一个扩展JDialog的组件。它在JScrollPane中创建一个JPanel(大尺寸)。然后我将RadioButtons和JLabel添加到JPanel

两个问题:  1 - RadioButtons和JLabel不显示。  2 - JScrollPane的scrollBars不显示

这是我的代码:

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);  
    }  


}  

1 个答案:

答案 0 :(得分:4)

更改

paneScroll.add(paneScrollpanel); 

paneScroll.setViewportView(paneScrollpanel);  

我在这儿的时候......

这有点无意义(恕我直言)

myFrame = this;  
myFrame.setPreferredSize(new Dimension(400, 160));  
myFrame.setSize(new Dimension(400, 160)); 

您可以使用

this.setSize(new Dimension(400, 160)); 

setSize(new Dimension(400, 160)); 

这是毫无意义的

panel.setSize(400, 160); 

由于布局管理员会自行决定制作面板的大小

您可能还想查看pack

从个人角度来说,我真的非常不喜欢不可调整大小的帧:P