简体   繁体   English

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

[英]Java code: Cant move JButtons in JFrame

I have been following a tutorial, its a little old, 1.6 Java, I use 1.8. 我一直在遵循一个教程,它有点旧,Java 1.6,我使用1.8。 So something has changed, heres my code: 所以发生了一些变化,这是我的代码:

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);

}

When I change rplay's x position, y position, width, or height nothing happens. 当我更改rplay的x位置,y位置,宽度或高度时,什么也没有发生。 It stays centered to the center of the screen and I cant figure it out. 它始终居中于屏幕中心,我无法弄清楚。

  • BTW public Launcher is being called from the main method in a different class. 从另一个类的main方法中调用BTW公共启动器。

So, I didn't have a line of code need to stop it from parenting to a layout manager. 因此,我没有一行代码需要阻止它成为布局管理器的父母。

window.setLayout(null);

in

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM