如何将JPanel添加到Graphics?

时间:2017-12-28 13:53:55

标签: java swing jpanel

如何将带有JButton的JPanel添加到Graphics?

我只想添加JButtons,因为我想制作暂停菜单。

窗口代码:

public JFrame frame;
private BufferedImage image;
private Canvas canvas;
private BufferStrategy bs;
private Graphics g;

public Window(GameContainer gc)
{
    image = new BufferedImage(gc.getWidth(), gc.getHeight(), BufferedImage.TYPE_INT_RGB);
    canvas = new Canvas();
    Dimension s = new Dimension((int)(gc.getWidth() * gc.getScaleX()), (int)(gc.getHeight() * gc.getScaleY()));
    canvas.setPreferredSize(s);
    canvas.setMaximumSize(s);
    canvas.setMinimumSize(s);

    frame = new JFrame(gc.getTitle());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    //frame.setUndecorated(true);
    frame.add(canvas, BorderLayout.CENTER);

    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    canvas.createBufferStrategy(2);
    bs = canvas.getBufferStrategy();
    g = bs.getDrawGraphics();
}

public void update()
{
    g.drawImage(image, 0, 0, canvas.getWidth(), canvas.getHeight(), null);
    bs.show();
}

一些玩家代码:

if(player.getPaused())
    {
        JPanel mainPanel = new JPanel(new GridBagLayout());
        JPanel panel = new JPanel(new GridLayout(0, 1));

        JButton button = new JButton("Resume");

        panel.add(button);
        mainPanel.add(panel);
        w.frame.add(mainPanel);
    }

一切正常但是当我使用JButton添加其他类JPanel时,它并不是apear。

没有图形,它可以工作。

0 个答案:

没有答案