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
    }
 
}

效果

你可以使用Java的Swing库来实现一个随机显示背景图片GUI。具体步骤如下: 1. 创建一个JFrame对象作为窗口。 2. 创建一个JPanel对象作为内容面板,并将其添加到JFrame中。 3. 将JPanel的布局管理器设置为空布局。 4. 使用File对象或资源路径获取所有的图片文件。 5. 将图片文件保存在一个数组中。 6. 使用Math类来生成一个随机数,用来选择数组中的图片。 7. 创建一个JLabel对象,并将其设置为选定的图片。 8. 将JLabel添加到JPanel中。 9. 将JPanel设置为JFrame的内容面板。 完整的Java代码示例如下: ```java import java.awt.BorderLayout; import java.awt.Image; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class RandomBackgroundImage { private JFrame mainFrame; public RandomBackgroundImage() { prepareGUI(); } public static void main(String[] args) { RandomBackgroundImage randomBackgroundImage = new RandomBackgroundImage(); randomBackgroundImage.show(); } private void prepareGUI() { mainFrame = new JFrame("Random Background Image Example"); mainFrame.setSize(400, 400); mainFrame.setLayout(new BorderLayout()); JPanel contentPanel = new JPanel(); contentPanel.setLayout(null); // 获取所有的图片文件 File imageDir = new File("image"); String[] imageNames = imageDir.list(); List<String> imageList = new ArrayList<>(Arrays.asList(imageNames)); // 随机选择一张图片 Random random = new Random(); int index = random.nextInt(imageList.size()); String imagePath = "image/" + imageList.get(index); // 创建并设置图片 ImageIcon imageIcon = new ImageIcon(imagePath); Image image = imageIcon.getImage(); Image scaledImage = image.getScaledInstance(400, 400, Image.SCALE_SMOOTH); ImageIcon scaledIcon = new ImageIcon(scaledImage); JLabel backgroundImageLabel = new JLabel(scaledIcon); backgroundImageLabel.setBounds(0, 0, 400, 400); contentPanel.add(backgroundImageLabel); mainFrame.add(contentPanel); mainFrame.setVisible(true); } private void show() { mainFrame.setVisible(true); } } ``` 在上述代码中,我将所有的图片文件保存在一个名为“image”的文件夹中。你需要将图片文件放在这个文件夹中,并根据实际情况修改图片文件夹的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值