[英]JLabel doesn't show up
I'm working on a program but my JLabel doesn't show up. 我正在开发一个程序,但是我的JLabel没有出现。 My JButton works perfectly (it appears) but for some reason the JLabel does not appear. 我的JButton可以正常工作(显示),但是由于某种原因,JLabel没有出现。 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);
}
}
I have checked on internet but I Haven't found anything. 我已经检查过互联网,但没有找到任何东西。
JFrame is visible before JComponents ( frame.add(content);
) are added / created 在添加/创建JComponents( frame.add(content);
)之前,JFrame是可见的
move code line frame.setVisible(true);
移动代码行frame.setVisible(true);
(better everything about JFrame) to the end of constuctor (最好是关于JFrame的一切)到构造器的末尾
Use layouts. 使用布局。 FlowLayout should be fine in this case. 在这种情况下, FlowLayout应该很好。 Do not call setBounds()
and do not set layout as a null
. 不要调用setBounds()
,也不setBounds()
layout设置为null
。
Add label and button on JPanel
在JPanel
上添加标签和按钮
Then add JPanel
on JFrame
然后在JFrame
上添加JPanel
Call pack()
instead of setSize()
调用pack()
而不是setSize()
Call setVisible(true)
in the end. 最后调用setVisible(true)
。
Good luck! 祝好运!
You are making setLayout null
. 您正在使setLayout为null
。
JPanel loginpanel = new JPanel();
loginpanel.setLayout(null);
Use this, 用这个,
JPanel loginpanel = new JPanel();
loginpanel.setLayout(new BorderLayout());
Run the UI on the EDT
instead of running on the main thread. 在EDT
上运行UI,而不是在主线程上运行。 Read this post . 阅读这篇文章 。
Example: 例:
public static void main(String [] args)
{
Runnable r = new Runnable() {
@Override
public void run() {
createWindow();
}
};
EventQueue.invokeLater(r);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.