简体   繁体   English

Java Gui label 未显示在屏幕上的所需位置

[英]Java Gui label does not show up in the desired location on screen

I have created a small GUI in Java Swing but I am facing a tiny issue with the location of the label.我在 Java Swing 中创建了一个小 GUI,但我在 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.我需要在框架的顶部中心显示 label 但在我的代码中,即使我添加了设置边界,它仍然显示在错误的位置。 How do I display the label in the center?如何在中心显示 label?

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. Label 与面板一起出现,但面板中添加的组件不显示。

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

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.但是您没有为 JFrame 分配特定的布局,只需将 Label 添加到 JFrame 即可。

The default Layout for JFrame is the BorderLayout. JFrame 的默认布局是 BorderLayout。 If you want a Component to appear at a specific position (for Example at the top), you have to specify the area.如果您希望组件出现在特定的 position(例如顶部的示例),您必须指定区域。 The BorderLayout got 5 different areas: NORTH, EAST, SOUTH, WEST and CENTER. BorderLayout 有 5 个不同的区域:NORTH、EAST、SOUTH、WEST 和 CENTER。

So what you should probably do is JFrame.add(JLabel, BorderLayout.NORTH) to assign you label at the top of the JFrame.因此,您可能应该做的是 JFrame.add(JLabel, BorderLayout.NORTH) 将 label 分配在 JFrame 的顶部。

By the way, you should not use both JFrame.pack and JFrame.setSize(), and JFrame.setVisible() should always be your last method call.顺便说一句,您不应该同时使用 JFrame.pack 和 JFrame.setSize(),并且 JFrame.setVisible() 应该始终是您的最后一个方法调用。 In your example it makes no difference, but it would, if you decided to add Components after the method call.在您的示例中,它没有区别,但如果您决定在方法调用之后添加组件,它会。

you must add this 2 line after declaration tittle:您必须在声明标题后添加此 2 行:

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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