📄✅🖥️🌵Description

Ce code permet de prendre en main le composant JScrollPane.

✅Résultat

Untitled

🖥️Code scrollpane.java

package scrollpane;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTextArea;
import java.awt.Component;
import java.awt.Dimension;

public class scrollpane{

	private JFrame frame;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					scrollpane window = new scrollpane();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public scrollpane() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		//Début paramétrage de la fênetre
		frame = new JFrame();
		frame.setVisible(true);
		frame.setTitle("ScrollPanel");
		frame.setBounds(100, 100, 450, 376);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
	
		/*-----*/
    //Création d'une zone de texte de 10 lignes et 30 colonnes
    JTextArea textArea = new JTextArea(10, 30); 
    //Création d'un scollPane et ajout de la zone de texte
    JScrollPane scrollPane1 = new JScrollPane(textArea); 
    scrollPane1.setBounds(95, 49, 246, 77);
    frame.getContentPane().add(scrollPane1); 
    
    /*-----*/
    //Création d'un long label
    JLabel label1 = new JLabel("HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello");
		label1.setBounds(57, 10, 348, 13);
		//Création d'un scollPane et ajout du label
    JScrollPane scrollPane2 = new JScrollPane(label1);
    scrollPane2.setBounds(95, 150, 246, 77);
    frame.getContentPane().add(scrollPane2);
    
    
    /*-----*/
    //Création d'un panel d'une grande taille et d'un label d'une taille normal
    JPanel panel = new JPanel();
		panel.setLayout(null);
		JLabel label2 = new JLabel("Hello");
		label2.setBounds(46, 50, 45, 13);
		panel.add(label2);
		panel.setPreferredSize(new Dimension(100, 100)); //C'est la ligne magique.
		//Création d'un scollPane et ajout du panel
		JScrollPane scrollPane3 = new JScrollPane(panel);
		scrollPane3.setBounds(95, 239, 246, 77);
		frame.getContentPane().add(scrollPane3); 
	}
}

🌵Arborescence

Untitled