gpt4 book ai didi

Java代码: Cant move JButtons in JFrame

转载 作者:太空宇宙 更新时间:2023-11-04 13:18:52 25 4
gpt4 key购买 nike

我一直在关注一个教程,它有点旧,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 位置、宽度或高度时,没有任何反应。它保持在屏幕中央,我无法弄清楚。

  • 顺便说一句,公共(public)启动器正在从不同类的 main 方法中调用。

最佳答案

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

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

关于Java代码: Cant move JButtons in JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33293751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com