how to add a buffered image as back ground of JFrame then add a panel on this image?(如何添加缓冲图像作为 JFrame 的背景,然后在该图像上添加面板?)
本文介绍了如何添加缓冲图像作为 JFrame 的背景,然后在该图像上添加面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在计算机工程的第二学期.
I'm in the second term of Computer engineering .
我的问题是如何在背景图片上添加JButton等,你知道我已经写了下面的代码,请帮我继续:正如我所说,我的 JBotton 无法在图像上显示,这就是问题所在.
my problem is how to add JButton etc... on the background image ,you know I have written the below code , please help me to continue: as I said my JBotton can't be shown on the image and here is the problem.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MyCalcFrame extends JFrame
{
private BufferedImage myImage;
private JPanel mypanel;
private JButton mybtn;
public MyCalcFrame()
{
this.setBounds(410, 110, 600, 450);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setAlwaysOnTop(true);
try
{
this.myImage=ImageIO.read(new File("D:\1.jpg"));
}//end try
catch(IOException e)
{
JOptionPane.showMessageDialog(null, "Image dose not exist.","NO Image found",JOptionPane.ERROR_MESSAGE);
}//end catch
this.mypanel=new JPanel();
this.mypanel.setBackground(Color.black);
this.setContentPane(new ImagePanel(myImage));
mybtn=new JButton("hello");
this.getContentPane().add(mybtn);
this.setVisible(true);
}//end MyCalcFrame constructor
class ImagePanel extends JComponent
{
private Image image;
public ImagePanel(Image image)
{
this.image = image;
}//end constructor
@Override
protected void paintComponent(Graphics g)
{
g.drawImage(image, 0, 0, null);
}//en paintComponent
}//end ImagePanel
//################ End constructor ########################
//public void paint(Graphics g)
//{
// g.drawImage(myImage, 0, 0, this);
/
沃梦达教程
本文标题为:如何添加缓冲图像作为 JFrame 的背景,然后在该图
猜你喜欢
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01