尝试放置按钮并使用中心模式

Trying to put button and using center mode

我正在尝试制作一个选项菜单以便能够选择要显示的标志,我正在尝试将 tailand 按钮放在左下角,但它似乎不想让步。

import java.awt.*;
import javax.swing.*;

public class Flags {
public static void startup() {
    Dimension d = new Dimension(400,300);
    JFrame menu = new JFrame("Flag Menu");
    menu.setResizable(true);
    JButton tailand = new JButton("Tailand");
    JPanel tailandPanel = new JPanel();
    tailand.setLayout(null);
    tailandPanel.setLayout(null);


    tailand.setBounds(300,100,100,600);
    tailandPanel.add(tailand);
    menu.add(tailandPanel);
    tailandPanel.setBackground(Color.LIGHT_GRAY);
    tailand.setBackground(Color.WHITE);
    tailandPanel.setPreferredSize(d);
    tailand.setPreferredSize(d);
    tailandPanel.setLocation(1, 1);
    tailand.setLocation(1, 1);
    menu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    menu.setLocationRelativeTo(null);
    menu.setSize(600, 400);
    menu.setResizable(false);
    menu.setVisible(true);
    menu.setLocationRelativeTo(null);
}
}

我已经尝试了 tailand.setPreferredSize(d);tailandpanel.setPreferredSize(d); 但没有成功。 他们也是使用中心模式的一种方式吗? (例如,给300,200,按钮的中心就是他们的?)

  1. 不要使用空布局
  2. 不要使用 setPreferredSize(...)

Swing 旨在与布局管理器一起使用

例如:

JButton button = new JButton( "Tailand" );

JPanel left = new JPanel( new BorderLayout() );
left.setBackground( Color.WHITE );
left.add(button, BorderLayout.PAGE_END);

JFrame frame = new JFrame("Flag Menu");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frane.setLocationRelativeTo(null);
frame.add(left, BorderLayout.LINE_START);
frame.setSize(600, 400);
frame.setVisible(true);

阅读有关 Layout Manager 的 Swing 教程部分,了解更多信息和使用每个布局管理器的示例。