Hi!
I'm making an app and I need to know how to set position of strings in java. Here is a code:

import java.awt.*;

import javax.swing.*;

public class Draw extends JFrame{
    JPanel panel;
    JLabel string;


    public Draw(){
        super("CO-LERNER");
        setSize(300,150);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);


        string = new JLabel("Example");
        panel.add(string);
        add(panel);
    }
}
  • you are added JComponents to the already (potentially can be) visible Swing GUI, meaning code line setVisible(true);

  • you hae to move this code line to the end of constructor, after all JComonents are added to container (JFrame)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.