java GUI顯示圖片

package com.wb.test;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import com.wb.exception.NoSuchPort;
import com.wb.exception.NotASerialPort;
import com.wb.exception.PortInUse;
import com.wb.exception.SendDataToSerialPortFailure;
import com.wb.exception.SerialPortOutputStreamCloseFailure;
import com.wb.exception.SerialPortParameterFailure;


/**
 * 顯示圖片
 * @author User
 *
 */
@SuppressWarnings("serial")
public class WriteDemo extends JFrame{
	
	public static void main(String[] args) throws SerialPortParameterFailure, NotASerialPort, NoSuchPort, PortInUse, SendDataToSerialPortFailure, SerialPortOutputStreamCloseFailure {
		
		WriteDemo d = new WriteDemo();
	    d.GUI();
		
	}
	
	 public void GUI() {
        setTitle("图像测试");
        Panel panel = new Panel();
        JLabel label = new JLabel();
        ImageIcon img = new ImageIcon("up/1.png");// 创建图片对象---圖片的鏈接  "/"就不用转义; "\"需要转义
        label.setIcon(img);
        panel.add(label);
        panel.setBackground(Color.BLACK);
        add(panel);
        setExtendedState(JFrame.MAXIMIZED_BOTH);// JFrame最大化
        setDefaultCloseOperation(EXIT_ON_CLOSE);//改變按鈕觸發
        setVisible(true);// 显示JFrame
    }
 
}

效果

猜你喜欢

转载自blog.csdn.net/baidu_35975930/article/details/83508869