how to add a buffered image as back ground of JFrame then add a panel on this image?(如何添加缓冲图像作为 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 的背景,然后在该图
基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01