Java Swing actionPerformed跳过可视化

问题描述

我正在使用Java和swing开发第一个排序可视化程序。如果我选择了硬编码,则可视化工具将正常工作。当我将actionPerformed()方法添加到“排序” JButton时,它开始出错。程序现在等待,直到完成排序,然后重新绘制。有办法解决吗?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SortingAlgorithmVisualizer implements ActionListener {

private static final int WINDOW_WIDTH = 1200;
private static final int WINDOW_HEIGHT = 600;
private static final String[] ALGORITHM_NAMES = { "Selection Sort","Insertion Sort","Quick Sort","Merge Sort" };

// JFrame to create GUI window
private JFrame window;
private Sort array;
private JButton sortButton;
private JComboBox<String> algorithmMenu;

// Constructor
public SortingAlgorithmVisualizer() {

    // Construct the JFrame and add components
    addComponents();

    // Set Basic JFrame Settings
    frameSetup();

    // Unsort the array
    array.unsort();
}

// Construct the JFrame and add components
public void addComponents() {
    window = new JFrame("Sorting Algorith Visualiser");
    array = new Sort();
    sortButton = new JButton("Sort");
    sortButton.addActionListener(this);
    algorithmMenu = new JComboBox<>(ALGORITHM_NAMES);
    // algorithmMenu.addActionListener(this);

    window.add(algorithmMenu,BorderLayout.PAGE_START);
    window.add(array,BorderLayout.CENTER);
    window.add(sortButton,BorderLayout.SOUTH);
}

// Set Basic JFrame Settings
public void frameSetup() {
    window.setLocationRelativeTo(null);
    window.setResizable(false);
    window.setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

@Override
public void actionPerformed(ActionEvent e) {
    if (e.getSource() == sortButton) {
        array.selectionSort();
    }
}

public static void main(String[] args) {
    SortingAlgorithmVisualizer vis = new SortingAlgorithmVisualizer();
}

}

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

public class Sort extends JPanel {

private static final long serialVersionUID = 1L;
private static final int ARRAY_SIZE = 25;
private static final int WINDOW_WIDTH = 1200;
private static final int WINDOW_HEIGHT = 600;
private static int barWidth = WINDOW_WIDTH / ARRAY_SIZE;
private int[] elementColor;

// Array to sort
private int[] array;

// Contructor
public Sort() {

    // DO SOMETHING WITH THIS!!!!
    array = new int[ARRAY_SIZE];
    elementColor = new int[ARRAY_SIZE];

    // Fill Array
    for (int i = 0; i < ARRAY_SIZE; i++) {
        array[i] = i + 1;
        elementColor[i] = 0;
    }
}

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLUE);

    for (int i = 0; i < ARRAY_SIZE; i++) {
        int height = (array[i] * 20 + 100);
        int x = i + (barWidth - 1) * i;
        int y = WINDOW_HEIGHT - height;
        g.fillRect(x,y,barWidth,height);
    }
}

public void unsort() {
    int shuffles = 100;
    for (int i = 0; i < shuffles; i++) {
        int rand1 = (int) (Math.random() * ARRAY_SIZE);
        int rand2 = (int) (Math.random() * ARRAY_SIZE);

        int temp = array[rand1];
        array[rand1] = array[rand2];
        array[rand2] = temp;
    }
}

public void selectionSort() {

    int n = ARRAY_SIZE;

    // Loop thought unsorted array
    for (int i = 0; i < n - 1; i++) {

        // Set Current Min
        int minIndex = i;

        for (int j = i + 1; j < n; j++) {
            if (array[j] < array[minIndex])
                minIndex = j;
        }

        // Swap the min element with the first element
        int temp = array[minIndex];
        array[minIndex] = array[i];
        array[i] = temp;

        // Repaints the Array after every swap
        repaint();
        wait(1000);
    }
}

public void insertionSort() {

}

public void quickSort() {

}

public void mergeSort() {

}

// Wait fuction for visualisation delay
public static void wait(int ms) {
    try {
        Thread.sleep(ms);
    }

    catch (InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
}
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...