Home > Software design >  Java Gui label does not show up in the desired location on screen
Java Gui label does not show up in the desired location on screen

Time:08-03

I have created a small GUI in Java Swing but I am facing a tiny issue with the location of the label. I need to show the label at the top center of the frame but in my code even if I add set bounds, it still shows up in the wrong place. How do I display the label in the center?

The panel also does not get displayed on my screen. Not sure why.

My Code

public class GuiInterface {
    public void GUI()
    {

    // Frame    
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Fault Localization");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    //Fonts
    Font  f2  = new Font(Font.SANS_SERIF,  Font.BOLD, 20);
    Font  f3  = new Font(Font.SANS_SERIF,  Font.PLAIN, 15);
    
    //Components 
    
     
    JPanel mPanel=new JPanel();       
    mPanel.setBackground(Color.lightGray);
    mPanel.setLayout(new BorderLayout());
    
    JButton jb1 = new JButton("Here");
    
    
    // Text Area
    JTextArea fTextArea=new JTextArea();
    //fTextArea.setBounds(60,150, 400,400); 
    fTextArea.setMargin(new Insets(3,3,3,3));
    fTextArea.setEditable ( false ); // set textArea non-editable
    JScrollPane scroll = new JScrollPane(fTextArea);
    
    JLabel tittle= new JLabel("Fault");
    // tittle.setBounds(30,30, 400,20);
    tittle.setFont(f2);
    
    //Adding the components to the panel
    mPanel.add(jb1, BorderLayout.SOUTH);
    
    
    // Frame Settings
    frame.add(mPanel);
    frame.add(tittle);
    frame.pack();
    frame.setVisible(true);
    frame.setSize(800,800); 
    
}
}

Updated Version of my code

Label appears along with the panel but the components added within the panel does not show up.

public class GuiInterface {
    public void GUI()
    {

    // Frame    
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("Fault");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    
    //Fonts
    Font  f2  = new Font(Font.SANS_SERIF,  Font.BOLD, 20);
    Font  f3  = new Font(Font.SANS_SERIF,  Font.PLAIN, 15);

    JPanel fPanel=new JPanel();       
    fPanel.setBackground(Color.BLUE);
    fPanel.setLayout(new BorderLayout());
    
  
    JButton jb1 = new JButton("Here");
    
    
    // Text Area
    JTextArea fTextArea=new JTextArea();
    //fTextArea.setBounds(60,150, 400,400); 
    fTextArea.setMargin(new Insets(3,3,3,3));
    fTextArea.setEditable ( false ); // set textArea non-editable
    JScrollPane scroll = new JScrollPane(fTextArea);
    
    JLabel tittle= new JLabel("Fault Localization",JLabel.CENTER);
    tittle.setFont(f2);

    
    
    
    //Adding the components to the panel
    fPanel.add(jb1, BorderLayout.NORTH);
    fPanel.add(fTextArea, BorderLayout.SOUTH);

    
    // Frame Settings
    frame.add(fPanel,BorderLayout.CENTER);
    frame.add(tittle,BorderLayout.NORTH);
    frame.setVisible(true);
    frame.setSize(800,800); 
    
}
}

CodePudding user response:

I don't know what the result should look like. But you don't assign a specific Layout to the JFrame and just add the Label to the JFrame.

The default Layout for JFrame is the BorderLayout. If you want a Component to appear at a specific position (for Example at the top), you have to specify the area. The BorderLayout got 5 different areas: NORTH, EAST, SOUTH, WEST and CENTER.

So what you should probably do is JFrame.add(JLabel, BorderLayout.NORTH) to assign you label at the top of the JFrame.

By the way, you should not use both JFrame.pack and JFrame.setSize(), and JFrame.setVisible() should always be your last method call. In your example it makes no difference, but it would, if you decided to add Components after the method call.

CodePudding user response:

you must add this 2 line after declaration tittle :

    tittle.setHorizontalAlignment(SwingConstants.CENTER);
    tittle.setVerticalAlignment(SwingConstants.TOP);

enter image description here

  • Related