java.awt.frame 设置背景图片,不是用JFrame 直接用的Frame 建立的窗口?
packagemyFrame;importjava.awt.*;importjava.awt.event.*;importjava.awt.image.*;importj...
package myFrame;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class myFrame extends Frame {
public myFrame() {
this.setBounds(300, 300, 300, 200);
// this.setBackground(Color.red);
this.setVisible(true);
}
public static void drawImage(BufferedImage image, int i, int j,
Object object) {
}
}
class m_method{
public static void main(String[] args) {
myFrame yFrame = new myFrame();
yFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
try {
BufferedImage image = ImageIO.read(new File("/Users/feifei/Desktop/1.png"));
myFrame.drawImage(image,0,0,null);
} catch (Exception e) {
e.printStackTrace();
}
}
} 展开
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class myFrame extends Frame {
public myFrame() {
this.setBounds(300, 300, 300, 200);
// this.setBackground(Color.red);
this.setVisible(true);
}
public static void drawImage(BufferedImage image, int i, int j,
Object object) {
}
}
class m_method{
public static void main(String[] args) {
myFrame yFrame = new myFrame();
yFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
try {
BufferedImage image = ImageIO.read(new File("/Users/feifei/Desktop/1.png"));
myFrame.drawImage(image,0,0,null);
} catch (Exception e) {
e.printStackTrace();
}
}
} 展开
2个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
class myFrame extends Frame {
Button bt;
public myFrame() {
bt= new Button("按钮");
JPanel jp = new JPanel();
jp.add(bt);
jp.setOpaque(false);
this.add(jp,BorderLayout.SOUTH);
this.setBounds(300, 300, 300, 200);
// this.setBackground(Color.red);
this.setVisible(true);
}
//重写paint方法,在这里绘制背景
public void paint(Graphics g) {
super.paint(g);
BufferedImage image;
try {
image = ImageIO.read(new File("C:\\Users\\XXX\\Desktop\\12.jpg"));
g.drawImage(image, 0, 0, null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class m_method {
public static void main(String[] args) {
myFrame yFrame = new myFrame();
yFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
展开全部
试了好多都显示不出来,要么是显示出来了图片却覆盖掉了其他的,所以最后将背景图当成一张普通的图进行加载(在其他控件加载前加上下面的背景图语句):
Image image = Toolkit.getDefaultToolkit().getImage(CommonWall.class.getResource("Images/screen_1.jpg"));
g.drawImage(image, 0, 0, Fram_width, Fram_length, this);
Image image = Toolkit.getDefaultToolkit().getImage(CommonWall.class.getResource("Images/screen_1.jpg"));
g.drawImage(image, 0, 0, Fram_width, Fram_length, this);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询