Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我将如何在框架的某个部分随机放置一个按钮。我试图设置两个随机变量 x 和 y,但它不起作用,按钮消失了,我猜它位于屏幕外。
这是我尝试过的:
int y = ran.nextInt(0 - frame.getHeight()); int x = ran.nextInt(0 - frame.getHeight());
我也收到错误“AWT-EventQueue -0”
谢谢
不是frame.getHeight()但是frame.getContentPane.getHeight()
frame.getHeight()
frame.getContentPane.getHeight(
需要用于放置AbsoluteLayout的容器JButton
AbsoluteLayout
JButton
将(re)validate和添加到放置repaint的容器中JButton
(re)validate
repaint
用作JPanel容器JButton
JPanel
您在括号中有一个负值。
int y = ran.nextInt(frame.getHeight()); int x = ran.nextInt(frame.getWidth());