2013-05-08 108 views
1

我試圖將此應用程序的組件設置爲使用setLocation的設置位置,到目前爲止我還沒有能夠移動組件。有更多的代碼,但它的調用和設置大部分代碼。有任何想法嗎?如何使用setLocation移動組件

import javax.swing.*; 



public class HangmanPanel extends JPanel { 
    private static final long serialVersionUID = -5793357804828609325L; 

    public HangmanPanel(){ 

     JLabel heading = new JLabel("Welcome to the Hangman App"); 
     JButton Button = new JButton("Ok"); 
     //Button.addActionListener(); 
     JLabel tfLable = new JLabel("Please Enter a Letter"); 
     JTextField text = new JTextField(10); 

     String input = text.getText(); 
     heading.setLocation(50, 20); 
     tfLable.setLocation(20, 100); 
     text.setLocation(320, 50); 
     Button.setLocation(230, 100); 

     this.add(heading); 
     this.add(tfLable); 
     this.add(text); 
     this.add(Button); 
    } 
} 
+0

也許你有一個佈局,不允許你自由移動的部件 – Evans 2013-05-08 16:52:01

+0

不要使用'setLocation /的setSize/setBounds',這是一個佈局管理 – 2013-05-08 17:34:09

回答

3

您不應該使用setLocation()來佈置Swing組件,使用佈局會好得多。請看these Layout Tutorials

+0

的工作如果我添加this.setLayout(新的BorderLayout()) ;到我的代碼它只是讓JButton覆蓋窗口...... – Sage1216 2013-05-08 17:21:06

+1

@ Sage1216好吧,只需添加一行代碼並在一天中調用它就很少幫助您:)如果您想了解Swing如何佈置組件,則需要閱讀我提供的鏈接左側菜單中的一些教程。 – Marco 2013-05-08 17:27:11

+0

@ Sage1216使用得當,'BorderLayout'非常有用。只是不要把你的按鈕放在中心。查看各種佈局管理器可用來產生不同的佈局 – 2013-05-08 17:33:31