首页 > 解决方案 > 如何在冒泡排序算法中应用 Swing 动画?

问题描述

我试图可视化数组索引之间的实际交换,但我无法使其可视化变慢。想在交换中暂停一下,这样我就可以真正想象它了。

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;
import javax.swing.*;

class test extends JFrame implements ActionListener {
     private static final int TIMER_DELAY = 1000;
    private Timer timer;
    final JLabel lb = new JLabel();
    final JTextField txt = new JTextField();
    final JTextField[] val = new JTextField[5];
    final JTextField[] val1 = new JTextField[5];
    final JButton btn = new JButton();
    final JButton sort = new JButton();
    final JButton show = new JButton();
    int n;
    Container con;

    void Inint() {

        con = this.getContentPane();
        con.setLayout(null);

        for (int i = 0; i < 5; i++) {
            val[i] = new JTextField();
        }
        lb.setText("Enter the size of aray ");
        lb.setBounds(100, 30, 150, 40);
        con.add(lb);

        txt.setBounds(100, 60, 50, 30);
        con.add(txt);
        btn.setText("OK");
        btn.setBounds(100, 100, 80, 30);
        btn.addActionListener(this);
        con.add(btn);

        sort.setText("Sort");
        sort.setBounds(100, 300, 80, 30);
        sort.addActionListener(this);

        show.setText("Show sorting");
        show.setBounds(200, 300, 80, 30);
        show.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent ae) {

        if (ae.getSource() == btn) {

            n = Integer.parseInt(txt.getText());
            int k = 80;
            int j = Integer.parseInt(txt.getText());
            for (int i = 0; i < n; i++) {
                con.revalidate();
                val[i].setBounds(k, 160, 50, 25);

                con.add(val[i]);
                k = k + 50;
                con.add(sort);

            }
        }
        if (ae.getSource() == sort) {
            int a, b;
       
            for (int i = 0; i < n - 1; i++) {

                con.revalidate();

                for (int j = 0; j < n - i - 1; j++) {

                    int temp = 0;
                    a = Integer.parseInt(val[j].getText());
                    b = Integer.parseInt(val[j + 1].getText());

                    if (a > b) 
                    {

                        temp = a;
                        a = b;
                        b = temp;
                        String val1 = Integer.toString(b);
                        String val2 = Integer.toString(a);

                        val[j].setText(val2);  
                        val[j + 1].setText(val1);

这是关于 2 行的 b/w,我想稍作停顿,请帮助我是 java 新手,我试图想象盒子是如何被交换的!

                    }
                }
            }
            con.add(show);
        }
        if (ae.getSource() == show) {
            int k = 100;
            for (int i = 0; i < n; i++) {
                val1[i] = new JTextField();
                String srt = val[i].getText();
                val1[i].setText(srt);

                val1[i].setBounds(k, 250, 50, 25);
                con.add(val1[i]);
                k = k + 50;
            }
        }

//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

Traffic班级

public class Traffic {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        test tst = new test();
        tst.setVisible(true);
        tst.setBounds(200, 220, 400, 500);
        tst.Inint();
        tst.setDefaultCloseOperation(tst.EXIT_ON_CLOSE);
    }
}

标签: javaswingjtextfieldtimingpause

解决方案


推荐阅读