欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

数据结构排序功能(菜单用GUI实现)Java版

程序员文章站 2022-09-28 22:10:05
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、程序的总体思路二、使用步骤1.引入库2.读入数据总结前言小编由于期末考试,太忙了,所以最近没有更新,希望谅解。本次针对小编结课实训写的一个简单排序程序作为讲解,主要是排序的五种方式和Java图形界面的运用,适合初学者理解排序算法、GUI、事件处理、以及面向对象的思想。一、程序的总体思路该城西二、使用步骤1.引入库代码如下(示例):import numpy as npimport pandas as...


前言

小编由于期末考试,太忙了,所以最近没有更新,希望谅解。本次针对小编结课实训写的一个简单排序程序作为讲解,主要是排序的五种方式和Java图形界面的运用,适合初学者理解排序算法、GUI、事件处理、以及面向象的编程思想。

一、程序的总体思路

数据结构排序功能(菜单用GUI实现)Java版

这里简单介绍一下该程序的主体框架,主要分为两部分,第一部分是排序算法,第二部分是菜单的图形的实现。下面具体介绍:

二、五种排序算法

1.冒泡排序算法

  • 算法的实现思路
    • 比较大小
    • 交换位置
  • 代码实现
public int[] bubbleSort(int[] array) {
		// 如果为数组为空返回原数组
		if (array.length == 0)
			return array;
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array.length - 1 - i; j++)
				// 找到大的值交换
				if (array[j + 1] < array[j]) {
					swap(array, j, j + 1);
				}
			Display(array);
		}
		return array;
	}

2.简单选择排序

  • 算法实现思路
    • 假设一个最小值
    • 依次比较找到最小值记录下标
    • 交换待排数与最小值
  • 代码实现
public int[] selectionSort(int[] array) {
		if (array.length == 0)
			return array;
		for (int i = 0; i < array.length; i++) {
			int minIndex = i;
			for (int j = i; j < array.length; j++) {
				if (array[j] < array[minIndex]) // 找到最小的数
					minIndex = j; // 将最小数的索引保存
			}
			swap(array, minIndex, i);
			Display(array);
		}
		return array;
	}

3.直接插入排序

  • 算法实现思路
    • 寻找插入位置
    • 将待插入位置及后面的依次向后移动
    • 将待排元素插入
  • 代码实现
public int[] insertionSort(int[] array) {
		if (array.length == 0)
			return array;
		int current;
		for (int i = 0; i < array.length - 1; i++) {
			// 记录待排的数
			current = array[i + 1];
			int preIndex = i;
			while (preIndex >= 0 && current < array[preIndex]) {
				array[preIndex + 1] = array[preIndex];
				preIndex--;
			}
			array[preIndex + 1] = current;
			Display(array);
		}
		return array;
	}

4.折半插入排序

  • 算法实现思路
    • 记录上、下、及待排坐标位置
    • 循环比较待排数与中间位置元素的大小缩小查找范围
    • 找到插入位置,插入待排元素
  • 代码实现
public int[] binaryInsertSort(int[] array) {
		for (int i = 1; i < array.length; i++) {
			if (array[i] < array[i - 1]) {
				// 缓存i处的元素值
				int tmp = array[i];
				// 记录搜索范围的左边界下标
				int low = 0;
				// 记录搜索范围的右边界下标
				int high = i - 1;
				while (low <= high) {
					// 记录中间位置下标
					int mid = (low + high) / 2;
					// 比较中间位置数据和i处数据大小,以缩小搜索范围
					if (array[mid] < tmp) {
						low = mid + 1;
					} else {
						high = mid - 1;
					}

				}
				// 将low~i处数据整体向后移动1位
				for (int j = i; j > low; j--) {
					array[j] = array[j - 1];
				}
				// 将待排数插入
				array[low] = tmp;
				Display(array);
			}
		}
		return array;
	}

5.希尔排序

  • 算法实现思路
    • 确定步长
    • 中间开始外层循环向后,内层循环向前
    • 按大小交换位置
    • 缩小步长
  • 代码实现
public int[] ShellSort(int[] array) {
		int len = array.length;
		int temp, gap = len / 2;// 步长每次减半
		while (gap > 0) {
			//从中间开始
			for (int i = gap; i < len; i++) {
				temp = array[i];
				//记录前一个步长位置
				int preIndex = i - gap;
				while (preIndex >= 0 && array[preIndex] > temp) {
					array[preIndex + gap] = array[preIndex];
					preIndex -= gap;
				}
				array[preIndex + gap] = temp;
			}
			gap /= 2;
			Display(array);
		}
		return array;
	}

三、菜单界面实现

总体思路很简单,菜单实现类继承JFrame类,设置窗体属性,添加设置好的面板。为更好的体现面向对象的思想,将面板也建立一个单独的类,在类体内实现各个面板并添加个组件。给组件设置监听器,为事件响应也建立一个单独的类,实现ActionListener接口,设置相对应的响应事件。


简单看一下效果,背景图片是我们宿舍楼。数据结构排序功能(菜单用GUI实现)Java版

1.面板类

  • 按钮组件

以“冒泡排序”按钮为例

		Button bubbleSort = new Button("冒泡排序");
		//设置字体 字号
		bubbleSort.setFont(new Font("宋体", 0, 13));
  • 文本框组件
		//创建文本框
		JTextField inputTextField = new JTextField(20);
  • 标签组件
JLabel label = new JLabel("输入数字以逗号分开");

2.窗口事件

主要实现WindowListener接口,对对应的事件响应进行编辑。

  • 正在关闭事件
  • 关闭事件
  • 移出窗口事件

3.动作事件

  • 按钮动作
  • 文本框动作

分为两步,第一步对相应的组件添加监听器,第二步在动作响应类内编辑对应功能模块。


四、具体代码

菜单界面

package 排序.test1;

import java.awt.BorderLayout;

import javax.swing.JFrame;

public class Menu extends JFrame {

	/**
	 * 视图窗口
	 */
	public Menu() {
		this.setTitle("贾振宇的排序选择菜单");
		// 设置窗体大小
		this.setSize(450, 400);
		// this.setLocationByPlatform(true);
		// 显示窗口位置
		this.setLocationRelativeTo(null);
		//this.setLocation(300, 200);
		// 窗口不能更改
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		//设置为边界布局
		this.setLayout(new BorderLayout());
		//下边界
		this.add(PanelAll.southPanel(), BorderLayout.SOUTH);
		//上边界
		this.add(PanelAll.northPanel(), BorderLayout.NORTH);
		//正中间
		this.add(PanelAll.cenPanel(), BorderLayout.CENTER);
		//显示窗口
		this.setVisible(true);
	}

}

面板类

package 排序.test1;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

public class PanelAll {
	public static Button bubbleSort, selectSort, insertionSort, shellSort, binaryInsertSort;
	static JButton jButton;
	public static JTextField inputTextField;
	public static JTextArea showArea;

	/**
	 * 中间面板
	 * 
	 * @return
	 */
	public static JPanel cenPanel() {
		//背景图片
		ImageIcon image = new ImageIcon("images/qg.jpg");
		//new 一个Jlabel存图片
		JLabel jLabel = new JLabel(image);
		//图片位置
		jLabel.setBounds(0, 0, image.getIconWidth(), image.getIconHeight());
		//新建一个面板
		JPanel cenJPanel = new JPanel();
		//边界布局
		cenJPanel.setLayout(new BorderLayout());
		//文本框
		showArea = new JTextArea(1, 20);
		showArea.setEditable(false);
		//新建展示结果面板
		JPanel showJPanel = new JPanel();
		showJPanel.setLayout(new FlowLayout());
		JLabel label = new JLabel("排序结果:");
		jButton = new JButton("显示排序结果");
		//设置监听器
		jButton.addActionListener(new MyActionlistener());
		//添加
		showJPanel.add(label);
		showJPanel.add(showArea);
		showJPanel.add(jButton);
		//滚动面板
		JScrollPane jScrollPane = new JScrollPane();
		//显示方式
		jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		jScrollPane.setViewportView(showJPanel);
		//添加位置
		cenJPanel.add(jScrollPane, BorderLayout.SOUTH);
		cenJPanel.add(jLabel, BorderLayout.NORTH);
		return cenJPanel;
	}

	/**
	 * 下方面板
	 * 
	 * @return
	 */

	public static JPanel southPanel() {
		//创建文本框
		inputTextField = new JTextField(20);
		//创建面板
		JPanel southPanel = new JPanel();
		southPanel.setLayout(new FlowLayout());
		JLabel label = new JLabel("输入数字以逗号分开");
		JButton btnButton = new JButton("确定");
		//设置监听器
		btnButton.addActionListener(new MyActionlistener());
		southPanel.add(label);
		southPanel.add(inputTextField);
		southPanel.add(btnButton);
		return southPanel;
	}

	/**
	 * 上方面板
	 * 
	 * @return
	 */

	public static JPanel northPanel() {
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new FlowLayout());
		//建立按钮 与监听
		bubbleSort = new Button("冒泡排序");
		//设置字体 字号
		bubbleSort.setFont(new Font("宋体", 0, 13));
		bubbleSort.addActionListener(new MyActionlistener());
		selectSort = new Button("简单选择排序");
		selectSort.addActionListener(new MyActionlistener());
		insertionSort = new Button("直接插入排序");
		insertionSort.addActionListener(new MyActionlistener());
		shellSort = new Button("希尔排序");
		shellSort.addActionListener(new MyActionlistener());
		binaryInsertSort = new Button("折半插入排序");
		binaryInsertSort.addActionListener(new MyActionlistener());
		northPanel.add(bubbleSort);
		northPanel.add(selectSort);
		northPanel.add(insertionSort);
		northPanel.add(binaryInsertSort);
		northPanel.add(shellSort);
		return northPanel;
	}

}

动作事件类

package 排序.test1;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

/**
 * 事件处理类
 * 
 * @author zhenyu
 *
 */

public class MyActionlistener implements ActionListener {
	// 静态数组用于储存数据
	static int array[];

	public void actionPerformed(ActionEvent e) {
		SortAll sortAll = new SortAll();
		if (e.getActionCommand().equals("确定")) {
			// 获取字符串
			String text = PanelAll.inputTextField.getText();
			// 解析字符串
			String[] split = text.split(",");
			Integer valueOf;
			array = new int[split.length];
			for (int i = 0; i < split.length; i++) {
				valueOf = Integer.valueOf(split[i]);
//				System.out.println(valueOf);
				array[i] = valueOf;
			}
			System.out.println("解析成功!");
			PanelAll.inputTextField.setText("");
		}
		// 五种排序按钮的触发事件

		if (e.getActionCommand().equals("冒泡排序")) {
			System.out.println("下面是冒泡排序");
			int[] copyArray = Arrays.copyOfRange(array, 0, array.length);
			long statr = System.currentTimeMillis();
			sortAll.bubbleSort(copyArray);
			long end = System.currentTimeMillis();
			System.out.println("冒泡排序用时:" + (end - statr) + "ms");
			System.out.println("========================================");
		}
		if (e.getActionCommand().equals("简单选择排序")) {
			System.out.println("下面是选择排序");
			int[] copyArray = Arrays.copyOfRange(array, 0, array.length);
			long statr = System.currentTimeMillis();
			sortAll.selectionSort(copyArray);
			long end = System.currentTimeMillis();
			System.out.println("简单选择排序用时:" + (end - statr) + "ms");
			System.out.println("========================================");
		}
		if (e.getActionCommand().equals("直接插入排序")) {
			System.out.println("下面是直接插入排序");
			int[] copyArray = Arrays.copyOfRange(array, 0, array.length);
			long statr = System.currentTimeMillis();
			sortAll.insertionSort(copyArray);
			long end = System.currentTimeMillis();
			System.out.println("直接插入排序用时:" + (end - statr) + "ms");
			System.out.println("========================================");

		}
		if (e.getActionCommand().equals("希尔排序")) {
			System.out.println("下面是希尔排序");
			int[] copyArray = Arrays.copyOfRange(array, 0, array.length);
			long statr = System.currentTimeMillis();
			sortAll.ShellSort(copyArray);
			long end = System.currentTimeMillis();
			System.out.println("希尔排序用时:" + (end - statr) + "ms");
			System.out.println("========================================");

		}
		if (e.getActionCommand().equals("折半插入排序")) {
			System.out.println("下面是折半插入");
			int[] copyArray = Arrays.copyOfRange(array, 0, array.length);
			long statr = System.currentTimeMillis();
			sortAll.binaryInsertSort(copyArray);
			long end = System.currentTimeMillis();
			System.out.println("折半排序用时:" + (end - statr) + "ms");
			System.out.println("========================================");
		}
		if (e.getActionCommand().equals("显示排序结果")) {
			PanelAll.showArea.setText("");
			int[] copyArray = Arrays.copyOfRange(array, 0, array.length);
			int[] sortF = sortAll.sortF(copyArray);
			for (int i = 0; i < sortF.length; i++) {
				PanelAll.showArea.append(String.valueOf(sortF[i]) + " ");
			}
			// PanelAll.jButton.setEnabled(false);
			System.out.println("显示完成!");
		}

	}

}

窗口事件

package 排序.test1;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JDialog;

public class MyWindowListener implements WindowListener {
	private Menu menu;

	public MyWindowListener(Menu menu2) {
		// TODO Auto-generated constructor stub
		menu = menu2;
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		System.out.println("欢迎来到这里!");
		System.out.println("下面是排序综合算法的设计与实现的展示:");

	}

	@Override
	public void windowClosing(WindowEvent e) {

		// TODO Auto-generated method stub
		close(this.menu);
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub

	}

	public void close(Menu menu) {
		JDialog jDialog = new JDialog(menu, "关闭", true);
		jDialog.setSize(200, 200);
		jDialog.setLocationRelativeTo(null);
		jDialog.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 20));
		jDialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
		Button yButton = new Button("是");
		Button nButton = new Button("否");
		jDialog.add(yButton);
		jDialog.add(nButton);

		yButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.out.println("确定关闭!");
				System.exit(0);
			}
		});
		nButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jDialog.setVisible(false);
				jDialog.dispose();
				System.out.println("误操作!");
			}
		});
		jDialog.setVisible(true);
	}

}

五种排序

package 排序.test1;
public class SortAll {
	/**
	 * 交换数组内两个元素
	 * @param array
	 * @param i
	 * @param j
	 */
	public static void swap(int[] array, int i, int j) {
		int temp = array[i];
		array[i] = array[j];
		array[j] = temp;
	}

	/*
	 * 冒泡排序
	 */
	public int[] bubbleSort(int[] array) {
		// 如果为数组为空返回原数组
		if (array.length == 0)
			return array;
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array.length - 1 - i; j++)
				// 找到大的值交换
				if (array[j + 1] < array[j]) {
					swap(array, j, j + 1);
				}
			Display(array);
		}
		return array;
	}
	/*
	 * 简单选择排序
	 */
	public int[] selectionSort(int[] array) {
		if (array.length == 0)
			return array;
		for (int i = 0; i < array.length; i++) {
			int minIndex = i;
			for (int j = i; j < array.length; j++) {
				if (array[j] < array[minIndex]) // 找到最小的数
					minIndex = j; // 将最小数的索引保存
			}
			swap(array, minIndex, i);
			Display(array);
		}
		return array;
	}
	/*
	 * 直接插入
	 */
	public int[] insertionSort(int[] array) {
		if (array.length == 0)
			return array;
		int current;
		for (int i = 0; i < array.length - 1; i++) {
			// 记录待排的数
			current = array[i + 1];
			int preIndex = i;
			while (preIndex >= 0 && current < array[preIndex]) {
				array[preIndex + 1] = array[preIndex];
				preIndex--;
			}
			array[preIndex + 1] = current;
			Display(array);
		}
		return array;
	}

	/*
	 * 折半插入
	 */
	public int[] binaryInsertSort(int[] array) {
		for (int i = 1; i < array.length; i++) {
			if (array[i] < array[i - 1]) {
				// 缓存i处的元素值
				int tmp = array[i];
				// 记录搜索范围的左边界下标
				int low = 0;
				// 记录搜索范围的右边界下标
				int high = i - 1;
				while (low <= high) {
					// 记录中间位置下标
					int mid = (low + high) / 2;
					// 比较中间位置数据和i处数据大小,以缩小搜索范围
					if (array[mid] < tmp) {
						low = mid + 1;
					} else {
						high = mid - 1;
					}

				}
				// 将low~i处数据整体向后移动1位
				for (int j = i; j > low; j--) {
					array[j] = array[j - 1];
				}
				// 将待排数插入
				array[low] = tmp;
				Display(array);
			}
		}
		return array;
	}

	/*
	 * 希尔排序
	 */
	public int[] ShellSort(int[] array) {
		int len = array.length;
		int temp, gap = len / 2;// 步长每次减半
		while (gap > 0) {
			//从中间开始
			for (int i = gap; i < len; i++) {
				temp = array[i];
				//记录前一个步长位置
				int preIndex = i - gap;
				while (preIndex >= 0 && array[preIndex] > temp) {
					array[preIndex + gap] = array[preIndex];
					preIndex -= gap;
				}
				array[preIndex + gap] = temp;
			}
			gap /= 2;
			Display(array);
		}
		return array;
	}

	/*
	 * 显示数组
	 */
	public void Display(int[] array) {
		int i = 0;
		for (i = 0; i < array.length; i++) {
			System.out.print(array[i] + " ");
		}
		System.out.println();
	}

	public int[] sortF(int[] array) {
		if (array.length == 0)
			return array;
		for (int i = 0; i < array.length; i++) {
			int minIndex = i;
			for (int j = i; j < array.length; j++) {
				if (array[j] < array[minIndex]) // 找到最小的数
					minIndex = j; // 将最小数的索引保存
			}
			swap(array, minIndex, i);
		}
		return array;
	}

}

五、效果图

数据结构排序功能(菜单用GUI实现)Java版

数据结构排序功能(菜单用GUI实现)Java版
数据结构排序功能(菜单用GUI实现)Java版

数据结构排序功能(菜单用GUI实现)Java版

总结

本次主要是针对本学期所学的简单应用,主要涉及排序算法、GUI图形界面、字符串的处理、以及一些常用类的调用。面向对象的思想是需要我们自己去体会理解的,在这里我想提醒大家,主动去练习,多动手才可以更好的理解。文章看起来很长,主要是加了很多源代码,这也是我在查询资料时有时遇到的问题,不愿去下载东西,所以就附源码。
如果想要打包好的,请在下方留言。
欢迎大家点赞评论!

本文地址:https://blog.csdn.net/weixin_47547146/article/details/111658122