2013-04-18 66 views
-1

我將如何在框架的某個部分隨機定位一個按鈕。我試圖設置兩個變量x和y是隨機的,但它不起作用,按鈕消失,我猜它定位在屏幕之外。我怎樣才能設置一個隨機位置爲java swing中的按鈕

氏姐姐是我曾嘗試:

int y = ran.nextInt(0 - frame.getHeight()); 
int x = ran.nextInt(0 - frame.getHeight()); 

我也得到一個錯誤 'AWT-EventQueue的-0'

感謝

回答

2

你必須在支架上的negetive值。

int y = ran.nextInt(frame.getHeight()); 
int x = ran.nextInt(frame.getWidth()); 
+0

當我嘗試這方面,我仍然得到一個錯誤。 – Rachel

+0

錯誤說的是什麼? –

+0

'AWT-EventQueue -0'當我在frame.getHeight()和frame.getWidth()之前添加一個數字時,錯誤不再存在,但按鈕不會出現在屏幕上。 – Rachel

2
  1. frame.getHeight()frame.getContentPane.getHeight(

  2. 使用AbsoluteLayout集裝箱其中JButton放置

  3. 添加(re)validaterepaint到容器其中JButton放置

  4. 需要

    使用JPanel作爲容器JButton

相關問題