JLabel没有出现

Jam*_* L. 1 java swing window jlabel jbutton

我正在研究一个程序,但我的JLabel没有显示出来.我的JButton工作得很好(看起来好像),但由于某些原因,JLabel没有出现.我在互联网上查了但是我还没找到任何东西.

package com.hinx.client;

import java.awt.Color;    
import javax.swing.*;

public class Main {

    public static void main(String [] args) 
    {
        createWindow();
    }       

    static void createWindow()
    {           

        //Create panel
        JPanel content = new JPanel();
        content.setLayout(null);

        //Build the frame
        JFrame frame = new JFrame("Hinx - A marketplace for apps - Client ALPHA_0.0.1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(700, 400);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.add(content);
        frame.setVisible(true);

        //Add the login button
        JButton login = new JButton("Login");
        login.setBounds(0, 342, 150, 30);

        //Create login label
        JLabel loginlabel = new JLabel("Login Area");

        //Create login panel
        JPanel loginpanel = new JPanel();
        loginpanel.setLayout(null);
        loginpanel.setBounds(0, 0, 150, 400);
        loginpanel.setBackground(Color.gray);
        loginpanel.add(login);
        loginpanel.add(loginlabel);         

        content.add(loginpanel);
    }       
}
Run Code Online (Sandbox Code Playgroud)

Ale*_* C. 5

设置一个layout为您的panel.每个例子:

loginpanel.setLayout(new BorderLayout());
Run Code Online (Sandbox Code Playgroud)

您可以在此处了解有关布局的更多信息.

这是我得到的: 在此输入图像描述

  • *"即使使用setBounds(),我也无法改变JLabel的位置"*不要使用`setBounds()`! (2认同)

mKo*_*bel 5

我在互联网上查了但是我还没找到任何东西.

  • frame.add(content);添加/创建JComponents()之前,JFrame是可见的

  • 将代码行frame.setVisible(true);(更好的关于JFrame的所有内容)移动到constuctor的末尾