how to use setLocation to move componets

丶灬走出姿态 提交于 2019-12-11 06:34:07

问题


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?

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

回答1:


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.



来源:https://stackoverflow.com/questions/16446056/how-to-use-setlocation-to-move-componets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!