0

I have a problem with adding a JLabel to a JFrame. I use a JPanel as described in several tutorials, but the Label doesn't pop up in the Frame; the same problem with other swing components like JTextField, JButton etc.. Drawing lines, rectangles and so on does work. I hope someone knows, what kind of mistake I am making here:

package plot1;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Gui extends JFrame {

    private JPanel contentPane;

    public Gui(){

        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);    

        setSize(1200, 600);
        setLayout(null);
        setLocationRelativeTo(null);
        setTitle("Plot");
        addWindowListener(new WindowAdapter(){
            public void windowClosed(WindowEvent evnt){
                System.exit(0);
            }
        }); 
        setResizable(false);    

        JLabel lblStanFunc = new JLabel("Die Funktionssyntax lautet:");
        lblStanFunc.setBounds(800, 40, 300, 30);
        lblStanFunc.setVisible(true);
        add(lblStanFunc);

    }

}

The main-method is in another class:

   package plot1;

   public class Main {
       public static void main(String[] args){

           new Gui().setVisible(true);;

       }
   }

The frame pops up as expected, but the JLabel is missing. Thanks for all helpful comments.

Hurafhev
  • 9
  • 1
  • 2
  • I can't reproduce this. I can see the label very well. It's on the top right of the frame. Is your screen not large enough to show the whole JFrame? – Sweeper May 24 '17 at 16:23
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson May 24 '17 at 17:03
  • You might need a `pack` call and you do need to run the GUI on the Event Dispatch Thread, per the Java Tutorial on Swing. – Lew Bloch May 24 '17 at 18:07

2 Answers2

3

Just Add setVisible(true)

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class GUI extends JFrame {
     private JPanel contentPane;

        public GUI(){

            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);    

            setSize(1200, 600);
            setLayout(null);
            setLocationRelativeTo(null);
            setTitle("Plot");
            addWindowListener(new WindowAdapter(){
                public void windowClosed(WindowEvent evnt){
                    System.exit(0);
                }
            }); 
            setResizable(false); 
            setVisible(true);

            JLabel lblStanFunc = new JLabel("Die Funktionssyntax lautet:");
            lblStanFunc.setBounds(800, 40, 300, 30);
            lblStanFunc.setVisible(true);
            add(lblStanFunc);

        }

        public static void main(String[] args) {
            new GUI();
        }
}
Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34
0

I can't reproduce it either. As sweeper mentioned, if your screen is not large enough, change the frame size to setSize(800, 600) and label location to lblStanFunc.setBounds(300, 250, 300, 30). The label should be in the center of the screen.

Ganesh Hariharan
  • 23
  • 1
  • 1
  • 5