如何在图像上放置 JButton?
我正在尝试修复一个 JFrame,其中将有一个背景图像,并且图像上的 JButtons 将执行一些命令。我尝试在没有布局的情况下执行此操作,因为我想将小按钮放在 JFrame 上的某些特定位置,但每次执行此操作时,背景图像都会出现在前面,或者 JFrame 的大小等于 JFrame 的大小。通过以下代码,JButton 具有与 JFrame 相同的大小。我尝试更改 JButton 的大小和位置,但没有任何效果。你能帮我吗?
这是代码
public final class Test extends JComponent
{
private Image background;
private JFrame frame;
private Dimension dimension;
public Test()
{
dimension = new Dimension(15, 15);
frame = new JFrame("Iphone");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.setBounds(641, 0, 344, 655);
frame.setVisible(true);
test = displayButton("tigka");
frame.getContentPane().add(test);
}
public void update(Graphics g)
{
paint(g);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.drawImage(background, 0, 25, null); // draw background
// 标签();
test = displayButton("test");
}
public JButton displayButton(String name)
{
JButton button = new JButton(name);
button.setSize(100, 100);
button.setPreferredSize(dimension);
return button;
}
I am trying to fix a JFrame where there will be a background image and on the image JButtons which will do some commands. I try to do it without layout because i want to put small buttons in some specific locations on the JFrame but every time i do it, the background image comes to the front or the JFrame has size equal to the JFrame size. With the following code, the JButton has the same size to JFrame. I have tried to change the size and location of the JButton but nothing. Can you help me please?
here is the code
public final class Test extends JComponent
{
private Image background;
private JFrame frame;
private Dimension dimension;
public Test()
{
dimension = new Dimension(15, 15);
frame = new JFrame("Iphone");
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(this);
frame.setBounds(641, 0, 344, 655);
frame.setVisible(true);
test = displayButton("tigka");
frame.getContentPane().add(test);
}
public void update(Graphics g)
{
paint(g);
}
public void paintComponent(Graphics g)
{
super.paintComponents(g);
g.drawImage(background, 0, 25, null); // draw background
// label();
test = displayButton("test");
}
public JButton displayButton(String name)
{
JButton button = new JButton(name);
button.setSize(100, 100);
button.setPreferredSize(dimension);
return button;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要更改
内容窗格
才能获取框架
的背景。输出:
You need to change the
content pane
to get a background for yourFrame
.Output:
您是否尝试过在标签中使用带有 HTML 的 JLabel?像这样:
然后在标签顶部添加按钮?
Have you tried using a JLabel with HTML in the label? Something like this:
then on top of your label you can add your button?
您应该交换这两行:
因此应该是这样的顺序:
另外,请注意内容窗格的默认布局是 BorderLayout。因此,您可以将内容窗格的布局显式设置为 null。
You should swap those two lines:
Thus it should be this order:
Additionally, note that the content pane's default layout is BorderLayout. Thus you'd set the layout of your content pane to null explicitly.