Does anyone know why the content of my window is not put in the center?
The content just goes to the top left corner, whatever modification i make : i can’t find out why…
I would like to use a simple BoxLayout on the page-y axis :
package myview;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.io.IOException;
import javax.swing.*;
import Model.Words;
public class MyWindow extends JFrame {
JLabel myWord = new JLabel();
public MyWindow(){
this.setSize(500, 300);
this.setVisible(true);
this.setLocationRelativeTo(null);
JPanel listPane = new JPanel();
listPane.setLayout(new BoxLayout(listPane, BoxLayout.Y_AXIS));
//listPane.setAlignmentX(Component.CENTER_ALIGNMENT);
Words words = new Words();
this.myWord.setText(words.getWord());
listPane.add(myWord);
JLabel myWord2 = new JLabel("test");
listPane.add(myWord2);
this.setContentPane(listPane);
//, BorderLayout.CENTER
//buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
}
public static void main (String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
MyWindow mywindow = new MyWindow();
mywindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mywindow.setVisible(true);
}
});
}
}
Thanks for your help
You probably want to add
Box.createVerticalGluebefore and after your components in yourBoxLayout.Also I’d also use
Box.createVerticalBoxas container.