how to use setLocation to move componets

2019-08-09 09:38发布

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条回答
Bombasti
2楼-- · 2019-08-09 10:20

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.

查看更多
登录 后发表回答