java如何在画布上添加图片
程序面板如下,我先添加了一个容器,存放左边的五个按钮,右边灰色的是画布,我现在想在画布上添加一个图片,请问怎么做?有具体代码最好,谢谢面板设计代码如下JButtontj,...
程序面板如下,我先添加了一个容器,存放左边的五个按钮,右边灰色的是画布,我现在想在画布上添加一个图片,请问怎么做?有具体代码最好,谢谢
面板设计代码如下
JButton tj,sc,qk,cz,exit;
Canvas cav;
super("通讯录系统");
setVisible(true);
setSize(300,300);
setLocation(400,400);
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
getContentPane().add(pan1,"West");
getContentPane().add(pan2,"East");
pan1.setLayout(new GridLayout(5,1));
pan2.setSize(150,200);
cav=new Canvas();
cav.setBackground(Color.gray);
cav.setSize(150,200);
tj=new JButton("添加");
sc=new JButton("删除");
cz=new JButton("查找");
qk=new JButton("清空");
exit=new JButton("退出");
pan1.add(tj);
pan1.add(sc);
pan1.add(cz);
pan1.add(qk);
pan1.add(exit);
pan2.add(cav); 展开
面板设计代码如下
JButton tj,sc,qk,cz,exit;
Canvas cav;
super("通讯录系统");
setVisible(true);
setSize(300,300);
setLocation(400,400);
JPanel pan1=new JPanel();
JPanel pan2=new JPanel();
getContentPane().add(pan1,"West");
getContentPane().add(pan2,"East");
pan1.setLayout(new GridLayout(5,1));
pan2.setSize(150,200);
cav=new Canvas();
cav.setBackground(Color.gray);
cav.setSize(150,200);
tj=new JButton("添加");
sc=new JButton("删除");
cz=new JButton("查找");
qk=new JButton("清空");
exit=new JButton("退出");
pan1.add(tj);
pan1.add(sc);
pan1.add(cz);
pan1.add(qk);
pan1.add(exit);
pan2.add(cav); 展开
3个回答
展开全部
如果容器是一个JFrame,名称为frame;
Canvas canvas=new Canvas();
frame.getContentPane.add(canvas);
Canvas canvas=new Canvas();
frame.getContentPane.add(canvas);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class CCC extends Canvas{
public Image img = null;
public void paint(Graphics g){
super.paint(g);
if(img != null){
g.drawImage(img, 0,0,this.getWidth(),this.getHeight(),null);
}
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
repaint();
}
public static void main(String[] args) {
CCC c = new CCC();
c.setBackground(Color.BLACK);
c.setBounds(0, 0, 300, 300);
JFrame jf = new JFrame();
jf.add(c);
//设置图片
try {
c.setImg(ImageIO.read(new File(System.getProperty("user.dir")+"/src/bcd.png")));
} catch (IOException e) {
e.printStackTrace();
}
jf.setSize(640, 480);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
public Image img = null;
public void paint(Graphics g){
super.paint(g);
if(img != null){
g.drawImage(img, 0,0,this.getWidth(),this.getHeight(),null);
}
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
repaint();
}
public static void main(String[] args) {
CCC c = new CCC();
c.setBackground(Color.BLACK);
c.setBounds(0, 0, 300, 300);
JFrame jf = new JFrame();
jf.add(c);
//设置图片
try {
c.setImg(ImageIO.read(new File(System.getProperty("user.dir")+"/src/bcd.png")));
} catch (IOException e) {
e.printStackTrace();
}
jf.setSize(640, 480);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询