如何使用setLocation来移动组件



我正在尝试使用setLocation将此应用程序的组件设置为设置位置,到目前为止我还无法移动组件。有更多的代码,但它的get调用和设置这些代码的大部分。什么好主意吗?

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

您不应该使用setLocation()来布局Swing组件,使用layout要好得多。请看看这些布局教程

最新更新