简体   繁体   中英

how to use setLocation to move componets

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM