如何在jframe上添加“position absolute”元素?

时间:2015-05-05 06:21:54

标签: java swing canvas absolute jlayeredpane

实际上这是我的代码:

private void createDisplay(){
    frame = new JFrame(title);
    frame.setSize(width, height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setUndecorated(true);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    canvas = new Canvas();
    canvas.setPreferredSize(new Dimension(width, height));
    canvas.setMaximumSize(new Dimension(width, height));
    canvas.setMinimumSize(new Dimension(width, height));
    canvas.setFocusable(false);

    frame.add(canvas);
    frame.pack();
}

它正在努力将我的游戏放在我的屏幕上(游戏在画布上绘制)。
我需要在屏幕前面添加一些其他元素,如JButtons例如。登记/> 我试过“JLayeredPane”就像这样:

    private void createDisplay(){
    frame = new JFrame(title);
    frame.setSize(width, height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setUndecorated(true);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    layeredPane = new JLayeredPane();
    layeredPane.setPreferredSize(new Dimension(width, height));
    //layeredPane.addMouseMotionListener((MouseMotionListener) this);
    frame.add(layeredPane);

    canvas = new Canvas();
    canvas.setPreferredSize(new Dimension(width, height));
    canvas.setMaximumSize(new Dimension(width, height));
    canvas.setMinimumSize(new Dimension(width, height));
    canvas.setFocusable(false);

    layeredPane.add(canvas,new Integer(10));
    frame.pack();
}

但是当我启动程序时,我看不到画布,只看到了派对:/ / 安妮的想法?对所有人来说。

PS:如果你有其他解决方案我没关系,我只需要保留我的JPanel。

0 个答案:

没有答案