Java 如何使用 setLocation 移动组件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16446056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:56:54  来源:igfitidea点击:

how to use setLocation to move componets

javaswinguser-interfacejpanellayout-manager

提问by Sage1216

I'm trying to set the components of this application to a set location using setLocation so far i haven't been able to move the components. There is more code but its get calling and setting this code for the most part. any ideas?

我正在尝试使用 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);
    }
}

采纳答案by Marco

You should not use setLocation()to layout Swing components, it is much better to use a Layout. Please have a look at these Layout Tutorials.

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