《Java课程实习》日志(周三)

 Java程序设计《猜猜看》游戏   进程…


做一个那个GUI那个,有一个按钮(JButton)和一个标签(JLabel),当点击 按钮后,能够显示下一张图片。


import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
public class ImgDemo extends JFrame {
    private JPanel contentPane;
    private int idImg = 1;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ImgDemo frame = new ImgDemo();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public ImgDemo() {
        setTitle("\u56FE\u7247\u663E\u793A");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 576, 404);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setBackground(Color.YELLOW);
        lblNewLabel.setForeground(Color.PINK);
        lblNewLabel.setBounds(48, 86, 467, 248);
        contentPane.add(lblNewLabel);
        JButton btnNewButton = new JButton("\u663E\u793A\u4E0B\u4E00\u5F20\u56FE\u7247");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ImageIcon icon;
                try {
                    String strImgFile = "E:/demoPhoto/";
                    strImgFile = strImgFile + String.valueOf(idImg) + ".jpg";
                    icon = new ImageIcon(ImageIO.read(new File(strImgFile)));
                    lblNewLabel.setBounds(48, 86, icon.getIconWidth(), icon.getIconHeight());   
                    lblNewLabel.setIcon(icon);
                    idImg = idImg+1;
                    if(idImg==10)
                        idImg = 1;
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        btnNewButton.setBounds(194, 37, 153, 28);
        contentPane.add(btnNewButton);
    }
}







增加图像缩放

import java.awt.EventQueue;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
public class ImgDemo extends JFrame {
    private JPanel contentPane;
    private int idImg = 1;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ImgDemo frame = new ImgDemo();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the frame.
     */
    public ImgDemo() {
        setTitle("\u56FE\u7247\u663E\u793A");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 576, 404);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JLabel lblNewLabel = new JLabel("New label");
        lblNewLabel.setBackground(Color.YELLOW);
        lblNewLabel.setForeground(Color.PINK);
        lblNewLabel.setBounds(48, 86, 400, 400);
        contentPane.add(lblNewLabel);
        JButton btnNewButton = new JButton("\u663E\u793A\u4E0B\u4E00\u5F20\u56FE\u7247");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ImageIcon icon;
                try {
                    String strImgFile = "E:/demoPhoto/";
                    strImgFile = strImgFile + String.valueOf(idImg) + ".jpg";
                    icon = new ImageIcon(ImageIO.read(new File(strImgFile)));
                    
                    Image image = icon.getImage(); 
                   Image smallImage = image.getScaledInstance(400,400,Image.SCALE_FAST);
                    
                    icon = new ImageIcon(smallImage);
                    lblNewLabel.setIcon(icon);
                    idImg = idImg+1;
                    if(idImg==10)
                        idImg = 1;
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        });
        btnNewButton.setBounds(194, 37, 153, 28);
        contentPane.add(btnNewButton);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值