如何让JLabel显示一个JButton? [英] How do I get a JLabel to show over a JButton?

查看:259
本文介绍了如何让JLabel显示一个JButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JLabel是JButton的顶端,但它不会显示在顶部。当JButton的代码被注释掉时,JLabel被显示出来,这意味着它在那里,但是在底部。有没有办法显示JButton的JLabel?



任何帮助都会令人敬畏。谢谢!

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

public class TestingLabelsOverButtons extends JFrame
{
public static void main(String [] args)
{
new TestingLabelsOverButtons();
}

public TestingLabelsOverButtons()
{
super();
setSize(500,500);
容器c = getContentPane();
c.setLayout(null);
c.setBackground(Color.white);

JButton button = new JButton(Button);
button.setBounds(0,0,500,500);
c.add(button);

JLabel label = new JLabel(Label);
label.setBounds(0,0,500,500);
c.add(label);

setVisible(true);
}
}

编辑: p>

为了澄清,我需要这个游戏,当按钮被点击时,JLabel将显示在按钮的顶部,以显示添加的分数。在我的游戏中,JLabel将比JButton小一点,所以JButton仍然需要可见。

解决方案

基于组件的ZOrder的涂料组分。基本上添加的最后一个组件是首先绘制的。



所以你可以通过这样做来实现你想要的:

  c.add(按钮); 
c.add(label);

虽然,我同意评论。你想做什么?可能有更好的解决方案。使用setBounds()来定位和调整组件几乎是个好主意。


我需要这个,按钮被点击,JLabel将显示在按钮的顶部,显示添加的分数。


如果你是要强制用户单击按钮显示分数,那么您应该使用 JOptionPane 来显示标签。否则标签将如何消失?强制用户点击相同的按钮不是一个非常好的UI。


I have a JLabel that is ontop of a JButton, but it will not show up ontop. When the code for the JButton is commented out, the JLabel is shown, meaning that it is there but is on the bottom. Is there a way to show the JLabel ontop of the JButton?

Any help would be awesome. Thank you!

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

    public class TestingLabelsOverButtons extends JFrame
    {
        public static void main (String []args)
        {   
            new TestingLabelsOverButtons();
        }

        public TestingLabelsOverButtons()
        {
            super();
            setSize(500,500);
            Container c = getContentPane();
            c.setLayout(null);
            c.setBackground(Color.white);

            JButton button = new JButton("Button");
            button.setBounds(0,0,500,500);
            c.add(button);

            JLabel label = new JLabel("Label");
            label.setBounds(0,0,500,500);
            c.add(label);

          setVisible(true);
        }
    }

EDIT:

For clarification, I need this for my game where when the button is clicked, a JLabel will be shown ontop of the button to display a "score" that is added. In my game, the JLabel will be a smaller square than the JButton, so the JButton still needs to be visible.

解决方案

Swing paint components based on the ZOrder of the component. Basically the last component added is painted first.

So you could achieve what you want by doing:

c.add(button);
c.add(label);

Although, I agree with the comments. What are you trying to do? There is probably a better solution. It is almost never a good idea to be using setBounds() to position and size a component.

I need this for my game where when the button is clicked, a JLabel will be shown ontop of the button to display a "score" that is added.

If you are going to force the user to click a button to display the score then you should probably be using a JOptionPane to display the label. Otherwise how will the label disappear? Forcing the user to click on the same button is not a very good UI.

这篇关于如何让JLabel显示一个JButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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