JLabel没有出现 [英] JLabel doesn't show up

查看:118
本文介绍了JLabel没有出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个节目,但我的JLabel没有出现。我的JButton工作得很好(看起来好像),但由于某些原因,JLabel没有出现。我已经在互联网上查了但是我没有找到任何东西。

  package com.hinx.client; 

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

公共类Main {

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

static void createWindow()
{

//创建面板
JPanel content = new JPanel();
content.setLayout(null);

//构建框架
JFrame框架=新JFrame(Hinx - 应用程序市场 - 客户端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);

//添加登录按钮
JButton login = new JButton(Login);
login.setBounds(0,342,150,30);

//创建登录标签
JLabel loginlabel = new JLabel(登录区);

//创建登录面板
JPanel loginpanel = new JPanel();
loginpanel.setLayout(null);
loginpanel.setBounds(0,0,150,400);
loginpanel.setBackground(Color.gray);
loginpanel.add(登录);
loginpanel.add(loginlabel);

content.add(loginpanel);
}
}


解决方案

设置您的面板布局。例如:

  loginpanel.setLayout(new BorderLayout()); 

您可以了解有关布局的更多信息


I'm working on a program but my JLabel doesn't show up. My JButton works perfectly (it appears) but for some reason the JLabel does not appear. I have checked on internet but I Haven't found anything.

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

解决方案

Set a layout for your panel. Per example :

loginpanel.setLayout(new BorderLayout());

You can learn more about layouts here.

Here's what I get :

这篇关于JLabel没有出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆