如何将带有图像的JButton放在另一个带有图像的JButton之上? [英] How to put a JButton with an image on top of another JButton with an image?

查看:149
本文介绍了如何将带有图像的JButton放在另一个带有图像的JButton之上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试将图像放在彼此上,因为我将这些图像存储在jbuttons上,我想知道是否有办法将jbuttons放在我现有的jbuttons上面。请帮忙。

So I am trying put images on to of each other and because I'm storing those images on jbuttons, I want to know if there is a way to put jbuttons on top on my already existing jbuttons. Any help please.

编辑:
所以假设我有一个

Jbutton b = new JButton()我设置了一个图像图标。
然后我有另一个
JButton x = new JButton()并将其设置为图像图标。

Edit : So let's say I have a
Jbutton b = new JButton() and I set it an image icon. then I have another JButton x = new JButton() and set it an image icon.

这些JButtons包含两个不同的图标。我希望图像相互重叠。我想要显示两个图像,底部按钮是较大的图像,顶部的按钮是较小的尺寸。不确定这是否清楚。

These JButtons contain two different icons. and I want the images to overlap each other. I want both images to show with the bottom button being the larger image and the button on top having a smaller size. Not sure if this is clear.

推荐答案

最简单的解决方案就是这个

The simplest solution is this

JButton j1=new JButton("a");
j1.setLayout(new BorderLayout());
j1.setBackground(Color.red);
add(j1);
JButton j2=new JButton("b");
j2.setBackground(Color.yellow);
j1.add("Center", j2);

-

虽然一般不会如果你想指定具体的位置和大小,那么null布局是一个解决方案:

Although generally not to be used, the null layout is a solution here if you want to specify specific location and size:

JButton j1=new JButton("a");
j1.setLayout(null);
j1.setBackground(Color.red);
JButton j2=new JButton("b");
j2.setBackground(Color.yellow);
j2.setBounds(100, 100, 50, 50);
j1.add(j2);
add(j1);

这篇关于如何将带有图像的JButton放在另一个带有图像的JButton之上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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