package one;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
public class main extends JFrame implements ActionListener {
Mycanvas canvas;
JComboBox cb1;
JComboBox cb2;
JTextArea text;
String[] citysname = new String[] { "怀化", "长沙", "张家界", "常德", "岳阳", "益阳",
"吉首", "娄底", "邵阳", "永州", "郴州", "湘潭", "株洲", "衡阳" };
public static void main(String[] args) {
main f = new main();
}
public main() {
JFrame A = new JFrame();
A.setSize(500, 650);
A.setLocationRelativeTo(null);
A.setResizable(false);
A.setTitle("最短路径查询系统(周倜 1406405045)");
// 布局
// 顶部
Container p = A.getContentPane();
JLabel lb1 = new JLabel("湖南省路径查询器");
JPanel pp = new JPanel();
pp.setLayout(new FlowLayout());
pp.add(lb1);
pp.setBackground(new Color(113, 191, 234));
p.add(pp, BorderLayout.NORTH);
// 中部
canvas = new Mycanvas();
canvas.setBackground(Color.WHITE);
p.add(canvas, BorderLayout.CENTER);
// 底部
JButton bt = new JButton("查询");
JLabel lb2 = new JLabel("选择:");
JLabel lb3 = new JLabel("---->>");
cb1 = new JComboBox(citysname);
cb2 = new JComboBox(citysname);
cb1.setMaximumRowCount(5);
cb2.setMaximumRowCount(5);
JPanel pl = new JPanel();
pl.setLayout(new FlowLayout());
pl.add(lb2);
pl.add(cb1);
pl.add(lb3);
pl.add(cb2);
pl.add(bt);
pl.setBackground(new Color(113, 191, 234));
JPanel pl2 = new JPanel();// /pl2放置选择和输出框
text = new JTextArea(10, 65);
text.setBackground(new Color(113, 191, 234));
text.setDisabledTextColor(Color.black);
text.setSelectedTextColor(Color.blue);
text.setEnabled(false);
text.setFont(new Font("宋体", Font.PLAIN, 13));
JTextArea text2=new JTextArea(12,65);
text2.setBackground(Color.WHITE);
pl2.add(pl, BorderLayout.NORTH);
pl2.add(text, BorderLayout.CENTER);
pl2.add(text2,BorderLayout.SOUTH);
p.add(pl2, BorderLayout.SOUTH);
bt.addActionListener(this);
A.setVisible(true);
A.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
static int s;
static int ee;
public void actionPerformed(ActionEvent e) {
s = cb1.getSelectedIndex();
ee = cb2.getSelectedIndex();
if (s != ee) {
MGraph.dijkstra(s, ee);
int ss = s;
text.setText(citysname[s] + "-->>" + citysname[ee] + "最短路程:"
+ MGraph.d[ee] + "km" + "\r\n");
int[] a = MGraph.road;
if (s != ee) {
text.append("路程为:" + "\r\n");
int i = a.length - 1;
while (i >= 0) {
if (a[i] == -1 || s == a[i]) {
i--;
continue;
}
text.append(" " + citysname[s] + "-->>" + citysname[a[i]]
+ " 该距离为:" + MGraph.edges[s][a[i]] + "km" + "\r\n");
s = a[i--];
}
}
s = ss;
canvas.repaint();
repaint();
}
}
}