import javax.swing.ImageIcon
import javax.swing.JFrame
import javax.swing.JPanel
import java.awt.Image
import java.awt.Color
import java.awt.Graphics
class ShowPicture extends JFrame{
public ShowPicture(){
// 读取一个图片
ImageIcon icon = new ImageIcon("C:\\Users\\OVEA\\OneDrive\\图片\\本机照片\\Picture\\1022971.jpg");
Image img = icon.getImage();
JPanel base = new JPanel() {
// 显示和面板,同时将图片显示出来
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
super.paint(g);
}
};
// 设置背景透明
base.setBackground(null);
// 设置控件透明
base.setOpaque(false);
// 不使用任何布局
base.setLayout(null);
// 创建并设置窗体
this.setTitle("图片显示");
// 设置内容显示
this.setContentPane(base);
// 设置窗体大小
this.setSize(500, 500);
// 设置位置
this.setLocation(800, 300);
// 设置点击关闭按钮的默认动作
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 锁定窗体
this.setResizable(false);
// 设置背景色(虽然没啥用)
this.setBackground(Color.WHITE);
// 设置是否可见
this.setVisible(true);
// 设置居中
this.setLocationRelativeTo(null);
}
}
new ShowPicture()
以下是该代码运行的效果图:
话说这个代码风格有点像JavaScript和Python的混合体……