Scrollpane

- - Dernière réponse : tarek_dotzero
Messages postés
801
Date d'inscription
jeudi 19 juillet 2007
Statut
Membre
Dernière intervention
19 août 2019
- 7 août 2019 à 01:44
Bonjour,

j'ai créé une interface qui comporte des JLabel, JtextField et JBouton en java et je voulais y mettre du scrollpane mais je n'arrive pas, j'ai vraiment besoin de votre aide merci. Voici mon code

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
 
import java.awt.BorderLayout;
 
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.ScrollPaneConstants;
 
public class TestScroll {
 
    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;
    private JTextField textField_7;
    private JTextField textField_8;
    private JTextField textField_9;
 
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestScroll window = new TestScroll();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
 
    /**
     * Create the application.
     */
    public TestScroll() {
        initialize();
    }
 
    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1327, 748);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
         
         
        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.CENTER);
        panel.setLayout(null);
         
         
        JScrollPane scrollPane = new JScrollPane(panel, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setBounds(0, 0, 930, 610);
         
         
        panel.add(scrollPane);
         
        JLabel lblChamp = new JLabel("champ1");
        lblChamp.setBounds(15, 16, 69, 20);
        panel.add(lblChamp);
         
        JLabel lblChamp_1 = new JLabel("champ2");
        lblChamp_1.setBounds(15, 72, 69, 20);
        panel.add(lblChamp_1);
         
        textField = new JTextField();
        textField.setBounds(103, 13, 146, 26);
        panel.add(textField);
        textField.setColumns(10);
         
        textField_1 = new JTextField();
        textField_1.setBounds(99, 69, 146, 26);
        panel.add(textField_1);
        textField_1.setColumns(10);
         
        JLabel lblChamp_2 = new JLabel("champ3");
        lblChamp_2.setBounds(15, 128, 69, 20);
        panel.add(lblChamp_2);
         
        textField_2 = new JTextField();
        textField_2.setBounds(103, 125, 146, 26);
        panel.add(textField_2);
        textField_2.setColumns(10);
         
        JLabel lblChamp_3 = new JLabel("champ5");
        lblChamp_3.setBounds(15, 188, 69, 20);
        panel.add(lblChamp_3);
         
        textField_3 = new JTextField();
        textField_3.setBounds(103, 185, 146, 26);
        panel.add(textField_3);
        textField_3.setColumns(10);
         
         
         
        JLabel lblChamp_4 = new JLabel("champ6");
        lblChamp_4.setBounds(15, 257, 69, 20);
        panel.add(lblChamp_4);
         
        textField_4 = new JTextField();
        textField_4.setBounds(103, 254, 146, 26);
        panel.add(textField_4);
        textField_4.setColumns(10);
         
        JLabel lblChamp_5 = new JLabel("champ7");
        lblChamp_5.setBounds(15, 315, 69, 20);
        panel.add(lblChamp_5);
         
        textField_5 = new JTextField();
        textField_5.setBounds(103, 312, 146, 26);
        panel.add(textField_5);
        textField_5.setColumns(10);
         
         
         
        JLabel lblChamp_6 = new JLabel("champ8");
        lblChamp_6.setBounds(15, 387, 69, 20);
        panel.add(lblChamp_6);
         
        textField_6 = new JTextField();
        textField_6.setBounds(103, 384, 146, 26);
        panel.add(textField_6);
        textField_6.setColumns(10);
         
        JLabel lblChamp_7 = new JLabel("champ9");
        lblChamp_7.setBounds(15, 452, 69, 20);
        panel.add(lblChamp_7);
         
        textField_7 = new JTextField();
        textField_7.setBounds(103, 449, 146, 26);
        panel.add(textField_7);
        textField_7.setColumns(10);
         
        JLabel lblChamp_8 = new JLabel("champ10");
        lblChamp_8.setBounds(15, 536, 69, 20);
        panel.add(lblChamp_8);
         
        textField_8 = new JTextField();
        textField_8.setBounds(103, 533, 146, 26);
        panel.add(textField_8);
        textField_8.setColumns(10);
         
        JLabel lblChamp_9 = new JLabel("champ11");
        lblChamp_9.setBounds(15, 612, 69, 20);
        panel.add(lblChamp_9);
         
        textField_9 = new JTextField();
        textField_9.setBounds(103, 609, 146, 26);
        panel.add(textField_9);
        textField_9.setColumns(10);
         
         
        JLabel lblChamp_11 = new JLabel("champ11");
        lblChamp_11.setBounds(15, 736, 69, 20);
        panel.add(lblChamp_11);
         
        JTextField textField_11 = new JTextField();
        textField_11.setBounds(103, 736, 146, 26);
        panel.add(textField_11);
        textField_11.setColumns(11);
         
        JLabel lblChamp_12 = new JLabel("champ12");
        lblChamp_12.setBounds(15, 800, 69, 20);
        panel.add(lblChamp_12);
         
        JTextField textField_12 = new JTextField();
        textField_12.setBounds(103, 800, 146, 26);
        panel.add(textField_12);
         
             
        JLabel lblChamp_13 = new JLabel("champ13");
        lblChamp_13.setBounds(15, 1162, 69, 20);
        panel.add(lblChamp_13);
         
        JTextField textField_13 = new JTextField();
        textField_13.setBounds(103, 1162, 146, 26);
        panel.add(textField_13);
        textField_13.setColumns(13);   
         
         
        JLabel lblChamp_14 = new JLabel("champ13");
        lblChamp_14.setBounds(15, 1362, 69, 20);
        panel.add(lblChamp_14);
         
        JTextField textField_14 = new JTextField();
        textField_14.setBounds(103, 1362, 146, 26);
        panel.add(textField_14);
        textField_13.setColumns(14);
         
        JLabel lblChamp_15 = new JLabel("champ15");
        lblChamp_15.setBounds(205, 16, 69, 20);
        panel.add(lblChamp_15);
         
        JTextField textField_15 = new JTextField();
        textField_15.setBounds(305, 13, 146, 26);
        panel.add(textField_15);
        textField_13.setColumns(14);
         
         
        JLabel lblChamp_16 = new JLabel("champ16");
        lblChamp_16.setBounds(205, 72, 69, 20);
        panel.add(lblChamp_16);
         
        JTextField textField_16 = new JTextField();
        textField_16.setBounds(305, 69, 146, 26);
        panel.add(textField_16);
        textField_16.setColumns(14);   
         
    }  
     
     
}
Afficher la suite 

1 réponse

Messages postés
801
Date d'inscription
jeudi 19 juillet 2007
Statut
Membre
Dernière intervention
19 août 2019
107
0
Merci
Bonjour,

Sur la ligne 65, on peut lire :

frame.getContentPane().add(panel, BorderLayout.CENTER);


Ainsi, vous ajoutez le 'panel' et non pas le 'scrollpane", ce dernier n'est jamais utilisé.
Il suffit de laisser cette ligne à la fin et de de la corriger comme suit :

frame.getContentPane().add(scrollpane, BorderLayout.CENTER);


Bon Courage,
Commenter la réponse de tarek_dotzero