Je veux pouvoir positionner mes deux JLabels mais quand je change les valeurs dans les lignes de position, cela ne fait rien. Aussi quand je l'exécute seulement la deuxième étiquette est montrée.Comment positionner et dimensionner JLabels en Java?
Mon code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class cubeTimerClass {
public static void main(String[] args) {
window(); //Runs the window method
}
public static void window() {
//Create a window
JFrame window = new JFrame(); //Create the window object
window.setSize(900, 600); //Set the size of the window
window.setTitle("Cube Timer"); //Set the title of the window
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Tells the program to quit when user closes the window
window.setVisible(true); //Make the window visible
//Create a label
JLabel label1 = new JLabel(""); //Create the label1 object
label1.setText("Message 1"); //Set the text for label1
label1.setAlignmentX(0);
label1.setAlignmentY(0);
window.add(label1); //Place the label on the window
//Create a label
JLabel label2 = new JLabel(""); //Create the label2 object
label2.setText("Message 2"); //Set the text for label2
label2.setAlignmentX(0);
label2.setAlignmentY(50);
window.add(label2); //Place the label on the window
}
}
Vous devriez commencer par lire que [Guide visuel aux gestionnaires de mise en page] (https://docs.oracle.com/javase/ tutorial/uiswing/layout/visual.html). Et bien sûr, la documentation de 'JLabel.setAlignment()' n'est pas ce qu'elle est. – AxelH
Si vous souhaitez définir vous-même la position de vos étiquettes, vous devez supprimer le gestionnaire de disposition (paramètre 'null'). Cependant, ce n'est pas recommandé et vous devriez d'abord lire dans le sujet AxelH lié (le sujet entier, pas seulement la partie liée). – Thomas