Je suis très nouveau sur java (je suis habitué à python).
REMARQUE: je souhaite que la position reste au centre même lorsque l'interface graphique est redimensionnée.
Je me demandais comment je peux centrer un seul bouton? Pour le moment, le bouton est en haut de l'interface graphique.
public class main_gui extends JFrame{
public static void main(String[] args) {
// Initial window
JFrame start_frame = new JFrame("P.D");
start_frame.setSize(1200, 800);
start_frame.setVisible(true);
start_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Panel to hold our buttons
JPanel start_panel = new JPanel();
start_frame.add(start_panel);
// Button to initialize everything
JButton start_button = new JButton("Start");
// Take out the border around the text
start_button.setFocusable(false);
start_panel.add(start_button);
}
}
Voici à quoi ressemble actuellement, je veux juste que ce bouton soit un peu plus bas, au centre.
Il n'y a pas besoin de panneau. Ajoutez simplement le bouton directement au cadre.
Le moyen le plus simple est d'utiliser un GridBagLayout
:
frame.setLayout( new GridBagLayout() );
frame.add(startButton, new GridBagConstraints());
Par défaut, le composant sera centré horizontalement et verticalement dans le GridBagLayout
.
Cet article est collecté sur Internet, veuillez indiquer la source lors de la réimpression.
En cas d'infraction, veuillez [email protected] Supprimer.
laisse moi dire quelques mots