问题描述
我试图可视化数组索引之间的实际交换,但是我无法使其可视化变慢。希望在交换时暂停一下,以便我可以实际看到它。
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,30);
sort.addActionListener(this);
show.setText("Show sorting");
show.setBounds(200,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,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);
这里是黑白两行,我想稍作停顿,请帮助我成为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,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);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)