import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class BackgroundImage extends JPanel{
Image img;
public BackgroundImage() {
try {
img = ImageIO.read(new File("image/server.JPG"));
}catch(Exception e) {
e.printStackTrace();
}
}
//重绘三要素:1.repaint 2.改变Panel大小 3.主程序启动
public void paint(Graphics g) {
super.paint(g);
g.drawImage(img, 0, 0, 400, 400, this);
//重绘子组件,不然会出现异常“有个按钮需要鼠标移动到它上边才显示”
super.paintChildren(g);
}
}
public class ServerStartStop extends JFrame implements ActionListener{
BackgroundImage bgi = null;
JButton jbStartUp = null,jbStop = null;
public ServerStartStop() {
//设置标题栏图标
ImageIcon titltIcon = new ImageIcon("image/server.JPG");
this.setI
JPanel设置背景图片(重绘实现)+按钮异常显示解决办法
最新推荐文章于 2021-03-19 18:57:48 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)