以下のプログラムは、gridbaglayoutを使用してjframeの左上隅にjpanelを配置することですが、代わりに非常に小さなボックスがjframeの中央に表示されます。jframeのレイアウトをnullに設定すると、jpanelが正常に表示されます。jpanelがgridbaglayoutでフレームの中央に圧縮される理由を誰かに教えてもらえますか?私は本当にgridbagを使用する必要があります。助けてください
import java.awt.*;
import javax.swing.*; //swing package
public class Major {
//defining the constructor
public Major() {
JFrame maFrame = new JFrame("The main screen"); //creating main Jframe
JPanel headPanel = new JPanel(); //creating the header panel
maFrame.setSize(900, 700); //setting size
maFrame.setBackground(Color.LIGHT_GRAY); //setting color of frame
Container container = maFrame.getContentPane();
container.setLayout(new GridBagLayout()); //setting layout of main frame
GridBagConstraints cns = new GridBagConstraints(); //creating constraint
cns.gridx = 0;
cns.gridy = 0;
maFrame.setLocationRelativeTo(null); //centering frame
headPanel.setBackground(Color.WHITE);
headPanel.setSize(200, 150);
container.add(headPanel, cns);
maFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting the default close operation of JFrame
maFrame.setVisible(true); //making the frame visible
}
//defining the main method
public static void main(String[] args) {
new Major(); //instantiating the class
}
}