Java代码:无法在JFrame中移动JButtons

时间:2015-10-23 02:07:16

标签: java jframe

我一直在关注一个教程,它有点旧,1.6 Java,我用1.8。所以事情发生了变化,继承我的代码:

private int width = 600;
private int height = 400;

private int lHeight = 4;

private int bWidth = 80;
private int bHeight = 40;

private int lItemWidth = 5;

private JPanel window = new JPanel();
private JButton play, options, help, updatenotes, quit;
private Rectangle rplay, roptions, rhelp, rupdatenotes, rquit;

public Launcher() {
    try{
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch (Exception e) {
        e.printStackTrace();
    }
    setTitle("Launcher - Final frontier");
    setSize(new Dimension(width, height));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().add(window);
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);

    drawButtons();
}

private void drawButtons() {
    play = new JButton("Play");
    rplay = new Rectangle(0, 0, bWidth, bHeight);
    play.setBounds(rplay);
    window.add(play);

}

当我改变rplay的x位置时,y位置,宽度或高度都没有发生。它保持在屏幕中心的中心,我无法弄明白。

  • BTW公共启动器正在从另一个类的主方法中调用。

1 个答案:

答案 0 :(得分:0)

所以,我没有一行代码需要阻止它从父级到布局管理器。

window.setLayout(null);

public Launcher() {
    try{
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }catch (Exception e) {
        e.printStackTrace();
    }
    setTitle("Launcher - Final frontier");
    setSize(new Dimension(width, height));
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    getContentPane().add(window);
    setLocationRelativeTo(null);
    setVisible(true);
    setResizable(false);

    //Here
    window.setLayout(null);

    drawButtons();
}