java - 如何在冒泡排序算法中应用 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);
}
}
解决方案
推荐阅读
- java - LazyInitializationException:无法初始化代理 - 没有会话我没有会话
- wordpress - url 末尾的 ?fireglass_rsn=true 是什么意思?
- python - 如何访问已在谷歌分析中获得权限的服务帐户的视图 ID?
- wordpress - 如何在没有插件的情况下在 Wordpress 中设置粘性事件?
- python - Tkinter Hover on Button by Brus Espinal (youtube)
- r - ggplot2 中的误差线和线条不匹配
- spring-data-cassandra - Spring Boot 应用程序连接到 Amazon Keyspaces
- c# - 如何阻止 ExecuteSqlInterpolated 用单引号包装字符串参数?
- r - R chisq 指定函数因推断失败
- javascript - 带有复选框的 jQuery 过滤器