Java 在DrawingComponent上显示JButton

Java 在DrawingComponent上显示JButton,java,swing,user-interface,Java,Swing,User Interface,我正在做一个游戏,我必须画出所有与游戏相关的东西。现在我想在DrawingComponent上添加一个“close”按钮 public class SimulationWindow extends JFrame { private static final long serialVersionUID = 1L; public SimulationWindow() { super("Game"); setUndecorated(true); setExtendedSta

我正在做一个游戏,我必须画出所有与游戏相关的东西。现在我想在DrawingComponent上添加一个“close”按钮

public class SimulationWindow extends JFrame 
{
private static final long serialVersionUID = 1L;

public SimulationWindow()
{
    super("Game");
    setUndecorated(true);
    setExtendedState(JFrame.MAXIMIZED_BOTH); 


    getContentPane().setBackground(Color.BLACK);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton closeButton = new JButton();
    closeButton.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            Game.close = true;
        }
    });

    add(new DrawingComponent());
    add(closeButton);

    setVisible(true);
}
}

但这只是一个灰色地带。基本上是游戏的DrawingComponent是不可见的

add(new DrawingComponent());
add(closeButton);
只能将单个组件添加到BorderLayout.CENTER,当您不指定约束时,该位置是默认位置

如果要将按钮添加到绘图面板,基本代码为:

DrawingComponent draw = new DrawingComponent();
draw.setLayout(...); // set the appropriate layout manager
draw.add( closeButton );
add(draw, BorderLayout.CENTER); // explicitly see the location 
1) 默认情况下,
JFrame
使用默认情况下将元素放置在
中心的位置。您需要在末尾添加
closeButton
,例如:
add(closeButton,BorderLayout.SOUTH)或您要添加它的任何位置。2) 不要扩展JFrame