1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
class MyPanelScrolled extends JPanel implements ComponentListener{
private JLabel[] labels = new JLabel[100];
final int TOTAL_CELLULE = 100;
public MyPanelScrolled(){
setBackground(Color.WHITE);
setBorder(BorderFactory.createLineBorder(Color.GRAY));
setLayout(new FlowLayout(FlowLayout.LEFT, -1, -1));
setPreferredSize(new Dimension(0, 0));
addComponentListener(this);
for(int i=0; i< TOTAL_CELLULE; i++){
labels[i] = new JLabel("Label " + i);
labels[i].setPreferredSize(new Dimension(150, 150));
labels[i].setVerticalAlignment(JLabel.CENTER);
labels[i].setHorizontalAlignment(JLabel.CENTER);
labels[i].setBorder(BorderFactory.createLineBorder(Color.GRAY));
add(labels[i]);
}
}
@Override
public void componentResized(ComponentEvent e) {
int colonne = this.getWidth() / 150;
int row = this.getHeight() / 150;
int totalScroll = (150*TOTAL_CELLULE) / colonne;
setPreferredSize(new Dimension((colonne*150) + 1, totalScroll));
System.out.println("Nombre colonne : " + colonne);
System.out.println("Nombre ligne : " + row);
//System.out.println("Total Scroll : " + totalScroll);
System.out.println("Total Height : " + this.getHeight());
//revalidate();
//repaint();
}
@Override
public void componentMoved(ComponentEvent e) {}
@Override
public void componentShown(ComponentEvent e) {}
@Override
public void componentHidden(ComponentEvent e) {}
}
public class TestScrollPane2 extends JFrame {
private MyPanelScrolled panel;
public TestScrollPane2(){
setTitle("Scroll test panel");
setSize(320, 200);
Container contenu = getContentPane();
panel = new MyPanelScrolled();
JScrollPane scroll = new JScrollPane(panel);
scroll.setWheelScrollingEnabled(true);
contenu.add(scroll, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args){
new TestScrollPane2().setVisible(true);
}
} |
Partager