日期:2014-05-20 浏览次数:20741 次
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.math.*;
import java.util.*;
public class TSP extends JFrame {
/**
* @param args
*/
// 定义面板及面板上的控件
public ShowPanel jp1;
public JPanel jp2;
public JTextField jtf1, jtf2, jtf3, jtf4;
public static void main(String[] args) {
// TODO Auto-generated method stub
TSP tsp = new TSP();
}
public TSP() {
// 初始化面板及控件
jp2 = new JPanel();
jtf1 = new JTextField(10);
jtf2 = new JTextField(10);
jtf3 = new JTextField(10);
jtf4 = new JTextField(10);
// 设置面板2的布局,采用只有一列的网格布局
jp2.setLayout(new GridLayout(14, 1, 0, 10));
jp2.add(jtf1);
jp2.add(jtf2);
jp2.add(jtf2);
jp2.add(jtf4);
jp1 = new ShowPanel(this);
// 线程
Thread t = new Thread(jp1);
t.start();
// 设置框架的布局
this.setLayout(new BorderLayout());
this.add(jp1, BorderLayout.CENTER);
this.add(jp2, BorderLayout.EAST);
this.setSize(800, 600);
this.setTitle("用遗传算法解决旅行商问题");
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
}
}
class ShowPanel extends JPanel implements Runnable {
int times;
TSP tsp;
public ShowPanel(TSP tsp) {
times = Integer.parseInt(tsp.jtf2.getText());
Individual individual = new Individual();
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.fillRect(0, 0, 660, 600);
}
@Override
public void run() {
// TODO Auto-generated method stub
while (times != 0) {
try {
Thread.sleep(100);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
individual.print_result();
times--;
this.repaint();
}
}
}