Java TableLayout

Je veux actuellement construire un type de tableau pour JPanels. J’ai découvert qu’il existe un TableLayout pour Java mais je ne sais pas comment l’importer. D’autre part, j’ai découvert qu’il existe un GridBagLayOut qui peut également construire un tableau comme celui-ci. Mais cela semble plus compliqué. Aucun conseil.

Voici un exemple d’utilisation de TableLayout par SSCCE ( Introduction à TableLayout )

 import javax.swing.JButton; import javax.swing.JFrame; import layout.TableLayout; public class TestTableLayout { public static void main(Ssortingng args[]) { JFrame frame = new JFrame("Example of TableLayout"); frame.setSize(450, 450); double size[][] = {{10, 75, 75, 75, 75, 75, 10}, // Columns {10, 75, 75, 75, 75, 75, 10}}; // Rows frame.setLayout(new TableLayout(size)); Ssortingng label[] = {"(1,1)", "(1,5)", "(1,3)", "(5,3)", "(3,3)"}; JButton button[] = new JButton[label.length]; for (int i = 0; i < label.length; i++) { button[i] = new JButton(label[i]); } frame.add(button[0], "1, 1"); frame.add(button[1], "1, 5"); frame.add(button[2], "1, 3"); frame.add(button[3], "5, 3"); frame.add(button[4], "3, 3"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } 

Le fichier JAR requirejs pour TableLayout peut être téléchargé ici.


Jetez également un coup d'œil à: Un guide visuel des gestionnaires de disposition , au cas où.


Si vous optez pour GridBagLayout, consultez: Comment utiliser GridBagLayout