求大神帮忙啊啊啊啊!!!~~
动态演示出“直接选择排序(Straight Selection Sort)”的过程,要求用图形界面。本学期的课程设计,平时不努力,现在才着急啊~希望高手们帮帮忙~~~
要求实现的功能:输入10个数字,作出从小到大的排序,要演示出排序的动态过程,使用图形界面。
c++/java都行 --------------------编程问答--------------------
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
public class StraightSelectSorting extends JFrame{
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new StraightSelectSorting();
}
private JTextField txtInputNum;
private int sortNum;
private JTextField[] txtInputs;
private boolean isInput;
private boolean isSort;
public StraightSelectSorting(){
setSize(new Dimension(400,400));
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
init();
}
/**增加组件**/
private void init(){
setLayout(new FlowLayout());
/**排序数数量对应组件与事件**/
JPanel pnl = new JPanel();
pnl.setPreferredSize(new Dimension(390,50));
pnl.setLayout(new FlowLayout(FlowLayout.CENTER,5,10));
JLabel lbl = new JLabel("需要排序数的数量:");
txtInputNum = new JTextField(10);
JButton btnOk = new JButton("确定");
pnl.add(lbl);
pnl.add(txtInputNum);
pnl.add(new Label(" "));
pnl.add(btnOk);
add(pnl);
/**每个数的输入**/
final JPanel pnlSorNum = new JPanel();
pnlSorNum.setPreferredSize(new Dimension(390,185));
pnlSorNum.setBorder(new TitledBorder("请在对应输入框里输入对应的数"));
add(pnlSorNum);
/**开始排序按钮**/
JButton btn = new JButton("开始排序");
add(btn);
/**排序演示**/
final JPanel pnlSortShow = new JPanel();
pnlSortShow.setPreferredSize(new Dimension(390,80));
pnlSortShow.setBorder(new TitledBorder("排序演示"));
add(pnlSortShow);
/**事件**/
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
float nums[] = new float[sortNum];
for(int i = 0 ; i < sortNum ; i++){
try{
nums[i] = Float.parseFloat(txtInputs[i].getText());
}catch (Exception ex) {
JOptionPane.showMessageDialog(null, "错误:文本框内部输入的不是数字。。。");
isSort = false;
return;
}
}
if(!isSort){
if(pnlSortShow != null)
pnlSortShow.removeAll();
isSort = true;
new SortShow(nums,pnlSortShow);
}
}
});
btnOk.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
try{
sortNum = Integer.parseInt(txtInputNum.getText());
if(!isInput){
if(pnlSorNum != null)
pnlSorNum.removeAll();
initPnlSornum(pnlSorNum);
isInput = true;
}
}catch (Exception e) {
JOptionPane.showMessageDialog(null, "排序数量应该是一个纯正整数字");
txtInputNum.setText("");
return;
}
}
});
}
/****/
private void initPnlSornum(JPanel pnlSorNum){
pnlSorNum.setLayout(new GridLayout(1, 1));
JPanel pnlBorder = new JPanel();
pnlBorder.setPreferredSize(new Dimension(380,1000));
JScrollPane scollPnl = new JScrollPane(pnlBorder,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED ,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );
pnlSorNum.add(scollPnl);
pnlSorNum.updateUI();
pnlSorNum.invalidate();
pnlSorNum.repaint();
pnlBorder.setLayout(new FlowLayout(FlowLayout.CENTER,5,10));
txtInputs = new JTextField[sortNum];
for(int i = 0 ; i < sortNum ; i++){
JTextField txt = new JTextField(7);
pnlBorder.add(txt);
txtInputs[i] = txt;
pnlBorder.updateUI();
pnlBorder.invalidate();
pnlBorder.repaint();
}
}
/**
* 书写一个排序演示的线程类
* @author Administrator
*
*/
private class SortShow implements Runnable{
private float[] nums;
private JPanel pnl;
public SortShow(float[] nums,JPanel pnl){
this.nums = nums;
this.pnl = pnl;
new Thread(this).start();
}
/**刷新界面**/
private void updatePnl(){
pnl.removeAll();
for(int i = 0 ; i < nums.length ; i++){
JLabel lbl = new JLabel(nums[i]+"");
lbl.setBorder(null);
pnl.add(lbl);
pnl.updateUI();
pnl.invalidate();
pnl.repaint();
}
}
/**刷新界面(增加背景色表示交换位置的2个数)**/
private void updatePnl(int a, int b){
pnl.removeAll();
for(int i = 0 ; i < nums.length ; i++){
JLabel lbl = new JLabel(nums[i]+"");
if(i == a || i == b)
lbl.setBorder(new LineBorder(Color.red));
pnl.add(lbl);
pnl.updateUI();
pnl.invalidate();
pnl.repaint();
}
}
@Override
public void run() {
/**将原始数据放入组件里面**/
pnl.setBorder(new TitledBorder("初始化每个数字...."));
updatePnl();
pnl.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
updatePnl();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
}
pnl.setBorder(new TitledBorder("开始排序...."));
updatePnl();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
float minNum;
int minIndex = 0;
for(int i = 0 ; i < nums.length - 1 ; i++){
minNum = nums[i]; //把最小值赋值为每次判断的第一个下标的数
pnl.setBorder(new TitledBorder("开始对第"+(i+1)+"组进行排序...."));
updatePnl();
for(int j = i ; j < nums.length - 1 ; j++){
if(nums[j] < nums[j+1] && minNum > nums[j]){
minNum = nums[j];
minIndex = j;
}else if(nums[j] > nums[j+1] && minNum > nums[j+1]){
minNum = nums[j+1];
minIndex = j+1;
}
}
pnl.setBorder(new TitledBorder("第"+(i+1)+"组排序结果,得到一个最小值是:"+minNum));
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
}
pnl.setBorder(new TitledBorder("要交换2个位置的数(已经用border标明)"));
updatePnl(i,minIndex);
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
}
/**交换2个数**/
if(nums[i] != minNum){
nums[i] = nums[i] + nums[minIndex];
nums[minIndex] = nums[i] - nums[minIndex];
nums[i] = nums[i] - nums[minIndex];
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
pnl.setBorder(new TitledBorder("交换后2个位置的数(已经用border标明)"));
updatePnl(i,minIndex);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
}
pnl.setBorder(new TitledBorder("排序结束"));
updatePnl();
}
}
}
补充:Java , Java SE