Java Swing程序设计实验

一、实验目的

运用和掌握Swing程序设计的绝大部分技术

二、实验内容

1、创建一个名称为MedleyGame的类,该类继承了JFrame类;
2、然后在该类中分别声明一个面板对象和一个按钮对象,面板对象用来添加拼图按钮,按钮对象为当前显示空白图片的按钮
3、最后为该类编写一个main()方法和一个构造方法MedleyGame(),并在构造方法中设置窗体的相关属性,如窗体的标题、显示位置、大小等。

三、实验环境

OS:Windows 10、编译环境:eclipse

四、实验过程描述

下面展示一些 内联代码片

Project:MedleyGame
// MedleyGame.java(源代码):
import java.awt.BorderLayout;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;




public class MedleyGame extends JFrame {
    
    
	private JPanel centerPanel;// 拼图按钮面板
	private JButton emptyButton;// 空白按钮对象

	class ImgButtonAction implements ActionListener {
    
    // 拼图按钮监听器
		public void actionPerformed(ActionEvent e) {
    
    
			String emptyName = emptyButton.getName();// 获得空白按钮的名称
			char emptyRow = emptyName.charAt(0);// 获得空白按钮所在的行
			char emptyCol = emptyName.charAt(1);// 获得空白按钮所在的列
			JButton clickButton = (JButton) e.getSource();// 获得被单点按钮对象
			String clickName = clickButton.getName();// 获得被单击按钮的名称
			char clickRow = clickName.charAt(0);// 获得被单击按钮所在的行
			char clickCol = clickName.charAt(1);// 获得被单击按钮所在的行
			// 判断被单击按钮与空白按钮是否相邻
			if (Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) {
    
    
				// 将被单击按钮的图片移动到空白按钮上
				emptyButton.setIcon(clickButton.getIcon());
				// 设置被单击的按钮显示空白图片
				clickButton.setIcon(new ImageIcon("src/22.jpg"));
				emptyButton = clickButton;// 将被单击的按钮设置为空白按钮
			}
		}
	}

	class StartButtonAction implements ActionListener {
    
    // 下一局按钮监听器
		public void actionPerformed(ActionEvent e) {
    
    
			String[][] stochasticOrder = reorder();// 获得网格图片的随机摆放顺序
			int i = 0;// 拼图按钮在拼图按钮面板中的索引
			for (int row = 0; row < 3; row++) {
    
    // 遍历行
				for (int col = 0; col < 3; col++) {
    
    // 遍历列
					JButton button = (JButton) centerPanel.getComponent(i++);
					button.setIcon(new ImageIcon(stochasticOrder[row][col]));
					if (stochasticOrder[row][col].equals("src/22.jpg"))
						emptyButton = button;
				}
			}
		}
	}

	public static void main(String[] args) {
    
    
		try {
    
    
			MedleyGame frame = new MedleyGame();// 创建本类对象
			frame.setVisible(true);// 设置窗体为可见
//			frame.Cut();
		} catch (Exception e) {
    
    
			e.printStackTrace();
		}

	}


	private static String[][] reorder() {
    
    // 用来获取网格图片的随机摆放顺序
		String[][] exactnessOrder = new String[3][3];// 网格图片的正确摆放顺序
		for (int row = 0; row < 3; row++) {
    
    // 遍历行
			for (int col = 0; col < 3; col++) {
    
    // 遍历列
				exactnessOrder[row][col] = "src/" + row + col + ".jpg";
			}
		}
		String[][] stochasticOrder = new String[3][3];// 网格图片的随机摆放顺序
		for (int row = 0; row < 3; row++) {
    
    // 遍历行
			for (int col = 0; col < 3; col++) {
    
    // 遍历列
				while (stochasticOrder[row][col] == null) {
    
    // 随机摆放顺序的指定网格为空
					int r = (int) (Math.random() * 3);// 取随机行
					int c = (int) (Math.random() * 3);// 取随机列
					if (exactnessOrder[r][c] != null) {
    
    // 正确摆放顺序的指定网格不为空
						stochasticOrder[row][col] = exactnessOrder[r][c];
						exactnessOrder[r][c] = null;
					}
				}
			}
		}
		return stochasticOrder;
	}
	
	public void Cut() throws IOException {
    
    
		int pw=centerPanel.getWidth();//获取面板宽高
		int ph=centerPanel.getHeight();
        BufferedImage bufImage = ImageIO.read(new File("C:\\Users\\mingjie\\eclipse-workspace\\MedleyGame\\src\\1.jpg"));
        for(int x=0;x<=200;x+=100)
    		for(int y=0;y<=200;y+=100) {
    
    
    			BufferedImage cutImage;
    			cutImage=bufImage.getSubimage (x, y,pw/3, ph/3);
    			ImageIO.write(cutImage, "JPEG", new File("C:\\Users\\mingjie\\eclipse-workspace\\MedleyGame\\src\\"+x/100+y/100+".jpg"));
    		}
	}

	public MedleyGame() {
    
    
		super();// 继承JFrame类的构造方法
		setResizable(false);// 窗体大小不可改变
		setTitle("拼图游戏");// 设置窗体的标题
		setBounds(300, 100, 314,439 );// 设置窗体的显示位置及大小
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置关闭窗体时退出程序
		
		
		// 创建面板对象
		final JPanel topPanel = new JPanel();
		topPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION,
				null, null));// 为面板添加边框
		topPanel.setLayout(new BorderLayout());// 设置面板采用边界布局
		getContentPane().add(topPanel, BorderLayout.NORTH);// 将面板添加到窗体顶部
		final JLabel modelLabel = new JLabel();// 创建显示参考图片的标签对象
		modelLabel.setBounds(0, 0, 100, 100);
		modelLabel.setIcon(new ImageIcon("src/3.jpg"));// 设置标签显示的参考图片
		topPanel.add(modelLabel, BorderLayout.WEST);// 将标签添加到面板的左侧
		
		
		// 创建“下一句”按钮对象
		final JButton startButton = new JButton();// 创建“下一句”按钮对象
		startButton.setText("下一局");// 设置按钮的标签文本
		startButton.addActionListener(new StartButtonAction());// 为按钮添加监听器
		topPanel.add(startButton, BorderLayout.EAST);// 将按钮添加到面板的中间
		
		
		// 创建拼图按钮面板对象
		centerPanel = new JPanel();
		centerPanel.setBorder(new TitledBorder(null, "", TitledBorder.DEFAULT_JUSTIFICATION,
				TitledBorder.DEFAULT_POSITION, null, null));// 为面板添加边框
		centerPanel.setLayout(new GridLayout(0, 3));// 设置拼图按钮面板采用3列的网格布局
		getContentPane().add(centerPanel, BorderLayout.CENTER);// 将面板添加到窗体的中间
		String[][] stochasticOrder = reorder();// 获得网格图片的随机摆放顺序
		for (int row = 0; row < 3; row++) {
    
    // 遍历行
			for (int col = 0; col < 3; col++) {
    
    // 遍历列
				final JButton button = new JButton();// 创建拼图按钮对象
				button.setName(row + "" + col);// 设置按钮的名称
				button.setIcon(new ImageIcon(stochasticOrder[row][col]));// 为按钮设置图片
				if (stochasticOrder[row][col].equals("src/22.jpg"))// 判断是否为空白按钮
					emptyButton = button;
				button.addActionListener(new ImgButtonAction());// 为拼图按钮添加监听器
				centerPanel.add(button);// 为按钮添加到拼图按钮面板中
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43372169/article/details/110927858