主函数:
package com.mywork; import java.awt.Color; import java.awt.Image; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class Main{ public static void main(String args[]){ JFrame f = new JFrame("ImageShow"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(500, 500); f.setLayout(null); ImageIcon image= new ImageIcon(System.getProperty("user.dir")+"/res/1.png") ; Image im = image.getImage(); ImageIcon ik = new ImageIcon(im.getScaledInstance(10, 10,Image.SCALE_FAST)); JLabel jmap[][] = new JLabel[50][50]; JLabel lab = new JLabel(image); lab.setBounds(500, 500, 500, 500); f.add(lab); for(int i=0;i<50;i++) { for(int j=0;j<50;j++) { jmap[i][j] = new JLabel(); jmap[i][j].setOpaque(true); jmap[i][j].setBounds(i*10, j*10, 10, 10); jmap[i][j].setIcon(ik); f.add(jmap[i][j]); } } f.setVisible(true); } }
主类里面也有加载图片的代码,
第二类中就是继承component的组件进行加载图片
package com.mywork; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class JL extends Component { BufferedImage img; public void paint(Graphics g) { // Graphics2D g2 = (Graphics2D)g; g.drawImage(img, 500, 0, null); } public JL() { this.setBounds(0, 0, 500, 500); try { img = ImageIO.read(new File(System.getProperty("user.dir")+"/res/1.png")); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }
Image是个抽象类,切记
版权声明:本文为博主原创文章,未经博主允许不得转载。