java登陆界面怎么加背景图会的我加你

2022-03-10 科技 60阅读
我直接给你一个ImagePanel类,你自己改改。
publicclassImagePanelextendsJPanel{

privatestaticfinallongserialVersionUID=1L;

protectedBufferedImageimage;
protectedRectanglerectangle=newRectangle();

publicImagePanel(){
setLayout(null);
installListeners();
}

privatevoidinstallListeners(){

}

publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
if(image==null){
return;
}
g.drawImage(image,0,0,null);
if(rectangle.width>=0&&rectangle.height>=0){
Colorc=g.getColor();
g.setColor(Color.RED);
g.drawRect(rectangle.x,rectangle.y,rectangle.width,rectangle.height);
g.setColor(c);
}
}

publicBufferedImagegetImage(){
returnimage;
}

publicvoidsetImage(BufferedImageimage){
this.setImage(image,true);
}

publicvoidsetImage(BufferedImageimage,booleanrepaint){
this.image=image;
if(image!=null){
Dimensionsize=newDimension(image.getWidth(null),image.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
}else{

}
rectangle=newRectangle();
if(repaint)
this.repaint();
}

publicbooleanisDirty(){
returnimage!=null;
}

publicvoidupdateZooe(Rectangler){
this.rectangle=r;
repaint();
}
}

核心代码就是重写paintComponent。再者你的代码习惯不好,布局管理器用的不科学。
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com