I would like to create a JscrollPane jsp in my panel panneau. But This JscroolPane doesn't appear. I think the trouble is that I use setLayout to null.

The code might produce a layout like this, the number of rows & columns of text fields are variable (in constructor):

layout

That's my code :

public class Cadre_fenetreA3 extends JFrame 
{
    JButton boutonOK = new JButton ("OK");
    public Cadre_fenetreA3 (String nom,int X, int Y, int lo, int la, int nbligne,int nbcolonne,String[] nomGrpGen, int Na, File file, boolean dernier)      
    {

        super(nom);                     
        setBounds(X,Y,500,500);     
        setVisible(true);   
        setFocusable(true);

        JLabel phrase = new JLabel("<html>Veuillez indiquer l'appartenance ou non d'un attribut <br> a un groupe d'attribut du Niveau :</html>"+Na);
        JTextField[][] allField = new JTextField [nbligne][nbcolonne];
        JLabel phrases[] = new JLabel [nbligne];
        JPanel panneau = new JPanel();
        JScrollPane jsp = new JScrollPane(panneau);

        jsp.createVerticalScrollBar(); 
        jsp.createHorizontalScrollBar(); 
            jsp.setBounds(20,20, 20, 20);
        panneau.setLayout(null);

        for(int i = 0;i < nbligne;i++)
        {
            for(int j = 0;j<nbcolonne;j++)
            {
                int random = (int)(Math.random()*2);
                 allField[i][j] = new JTextField(String.valueOf(random));
                 allField[i][j].setBounds(150+j * 30, 75 + i * 30, 20, 20);
                 panneau.add(allField[i][j]);
            }

           phrases[i] = new JLabel(nomGrpGen[i]);
           phrases[i].setBounds(5, 75+ i * 30, 200, 20);

          panneau.add( phrases[i]);
        }
        phrase.setBounds(0,0,1000,50);
         panneau.add(phrase);

        boutonOK.setBounds(lo-90,la-110,60,60);
        boutonOK.addActionListener(new ecout(nbligne,nbcolonne,allField, file, dernier));
         panneau.add(boutonOK);
         add(jsp);
    }
有帮助吗?

解决方案

that's because you're adding panneau at the end - you should be adding jsp ;-)

Also, don't set the LayoutManager to null because that will cause problems (unless you know what you're doing). For your requirements, everything you need can be found in the Visual Guide to Layout Managers.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top