📄 myclass.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyClass extends WindowAdapter implements WindowListener,MouseListener,ItemListener, ActionListener
{
final static int IN=10; //常量定义,景点的个数
final static int LAN=10000; //最大的一个变量,象征无穷
static int[][] shortest=new int[IN][IN]; //数组定义最短路径
static int[][] cost=new int[IN][IN]; //边之间的权值
static int[][] path=new int[IN][IN]; //中间经过的路径
Frame f;
Dialog dt,df,ds,dh;
Panel p1,p2,p3;
Label l1,l2,l3,l4,l5,l6;
JLabel l0,ly0;
Label ll1,ll2,ll3;
JTextArea tf1;
TextField tf2,tf3;
PopupMenu pm1;
MenuBar mb1;
Menu m1,m2,m3;
Choice ch1,ch2,ch3;
Button bt;
public void display()
{
f=new Frame("济南大学导游系统"); //创建主窗口
f.setSize(850,650);
f.setLocation(100, 50);
f.setBackground(Color.lightGray);
f.addWindowListener(this);
f.addMouseListener(this);
f.setVisible(true);
ImageIcon img1=new ImageIcon("b.jpg"); //添加背景图片
ly0=new JLabel("",img1,SwingConstants.CENTER);
dt=new Dialog(f,"地图",true); //地图窗口创建
Font font=new Font("黑体",Font.PLAIN,17);
dt.setFont(font);
dt.setSize(850,600);
dt.setLocation(150,50);
dt.setBackground(Color.DARK_GRAY);
dt.addWindowListener(this);
df=new Dialog(f,"济南大学景点查询",true); //景点查询系统窗口实现
df.setSize(600,450);
df.setLocation(250, 150);
df.addWindowListener(this);
ds=new Dialog(f,"济南大学景点最短路径查询",true); //景点查询系统窗口实现
ds.setSize(600,450);
ds.setLocation(250, 150);
ds.addWindowListener(this);
dh=new Dialog(f,"版权所有",true); //景点查询系统窗口实现
dh.setSize(250,150);
dh.setLocation(200, 150);
dh.setBackground(Color.lightGray);
dh.setLayout(new BorderLayout());
dh.addWindowListener(this);
pm1=new PopupMenu("Popup"); //生成一个弹出式菜单对象
pm1.add(new MenuItem("旅游景点查询")); //加入菜单项
pm1.add(new MenuItem("景点最短路径"));
pm1.addActionListener(this);
p1=new Panel(); //面板的创建
p2=new Panel();
p2.setLayout(new FlowLayout());
p3=new Panel();
p3.setLayout(new FlowLayout());
ImageIcon img2=new ImageIcon("a.jpg"); //添加背景图片
l0=new JLabel("",img2,SwingConstants.CENTER);
l1=new Label("欢迎光临济南大学导游系统");
Font font1=new Font("黑体",Font.CENTER_BASELINE,30);
l1.setFont(font1);
l2=new Label("请选择景点:");
l3=new Label("请分别选择要查询最短路径的景点代号:");
l4=new Label("TO");
l5=new Label("作者:李继民");
l6=new Label("QQ:290512522");
Font font2=new Font("黑体",Font.CENTER_BASELINE,18);
l5.setFont(font2);
l6.setFont(font2);
tf1=new JTextArea(10,20);
tf1.setFont(new Font("Serif",Font.PLAIN,20));
tf1.setLineWrap(true); //自动换行
tf1.setWrapStyleWord(true);
tf2=new TextField(40);
tf3=new TextField(40);
ch1=new Choice(); //下拉式菜单创建
ch1.addItem("新西门");
ch1.addItem("老西门");
ch1.addItem("操场");
ch1.addItem("一食堂");
ch1.addItem("六教");
ch1.addItem("五教");
ch1.addItem("图书馆");
ch1.addItem("喷泉");
ch1.addItem("西南门");
ch1.addItem("新西南门");
ch1.addItemListener(this);
ch2=new Choice();
ch2.addItem("0新西门");
ch2.addItem("1老西门");
ch2.addItem("2操场");
ch2.addItem("3一食堂");
ch2.addItem("4六教");
ch2.addItem("5五教");
ch2.addItem("6图书馆");
ch2.addItem("7喷泉");
ch2.addItem("8西南门");
ch2.addItem("9新西南门");
ch2.addItemListener(this);
ch3=new Choice();
ch3.addItem("0新西门");
ch3.addItem("1老西门");
ch3.addItem("2操场");
ch3.addItem("3一食堂");
ch3.addItem("4六教");
ch3.addItem("5五教");
ch3.addItem("6图书馆");
ch3.addItem("7喷泉");
ch3.addItem("8西南门");
ch3.addItem("9新西南门");
ch3.addItemListener(this);
bt=new Button("查询");
bt.addActionListener(this);
f.add(pm1); //组件填加到窗口中
f.add(l0);
dt.add(ly0);
df.add(p1,BorderLayout.NORTH);
df.add(p2,BorderLayout.WEST);
ds.add(p3);
dh.add(l5,BorderLayout.NORTH);
dh.add(l6,BorderLayout.SOUTH);
p1.add(l1);
p2.add(l2);
p2.add(ch1);
p2.add(tf1);
addmyMenu();
ll3=new Label("欢迎光临济南大学导游系统");
ll3.setFont(font1);
p3.add(ll3);
p3.add(l3);
p3.add(ch2);
p3.add(l4);
p3.add(ch3);
p3.add(bt);
ll1=new Label("您要查询的最短距离是:");
ll2=new Label("您要查询的最短路径为:");
p3.add(ll1);
p3.add(tf2);
p3.add(ll2);
p3.add(tf3);
}
public void addmyMenu(){
mb1=new MenuBar(); //生成一个菜单
f.setMenuBar(mb1); //在f上添加菜单
m1=new Menu("地图信息");
m2=new Menu("地图操作");
m3=new Menu("帮助");
mb1.add(m1);
mb1.add(m2);
mb1.add(m3);
m1.add(new MenuItem("地图"));
m1.addSeparator();
m1.add(new MenuItem("退出"));
m2.add(new MenuItem("地图信息查询"));
m2.addSeparator();
m2.add(new MenuItem("最短路径查询"));
m3.add(new MenuItem("关于"));
m1.addActionListener(this);
m2.addActionListener(this);
m3.addActionListener(this);
}
public void floyed(){ //求最短路径的FLOYED算法
int i,j;
int k=0;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
shortest[i][j]=cost[i][j];
}
for(k=0;k<10;k++)
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(shortest[i][j]>(shortest[i][k]+shortest[k][j]))
{
shortest[i][j]=shortest[i][k]+shortest[k][j];
path[i][j]=k;
path[j][i]=k;
}
}
public int displaytwo(int i,int j){ //输出最短距离及最短路径
int a,b;
String str;
a=i;
b=j;
tf2.setText(a+"->"+b+"的最短距离是:"+shortest[a][b]+"米");
if(shortest[i][j]!=LAN)
{
if(i<j)
{
str="End"+b;
while(path[i][j]!=0)
{
str+="<-"+path[i][j];
if(i<j)
j=path[i][j];
else
i=path[i][j];
}
str+="<-"+a;
}
else
{
str=""+a;
while(path[i][j]!=0)
{
str+="->"+path[i][j];
if(i<j)
j=path[i][j];
else
i=path[i][j];
}
str+="->"+b+"End";
}
tf3.setText(""+str);
}
else
tf3.setText("输入错误,不存在此路!");
return 1;
}
public void windowClosing(WindowEvent e) //关闭窗口响应事件
{
if(e.getSource()==dt)
dt.setVisible(false);
else if(e.getSource()==df)
df.setVisible(false);
else if(e.getSource()==ds)
ds.setVisible(false);
else if(e.getSource()==dh)
dh.setVisible(false);
else
System.exit(0);
}
public void itemStateChanged(ItemEvent e) { //ch1下拉式菜单响应
if(ch1.getSelectedItem()=="新西门"){
tf1.setText("坐落在济南的西南方向,济微路106号,西面是102,104路电车的终点站,进入校门沿着大路可以穿过整个校园!");
}
else if(ch1.getSelectedItem()=="老西门"){
tf1.setText("老西门是济南大学的原来的一个校门,已经有很久远的历史!对面是信用合作社,方面同学存取款;进去校门两侧有树木耸立,十分惬意!充分体现了济南大学悠久的历史和丰富的文化底蕴!");
}
else if(ch1.getSelectedItem()=="操场"){
tf1.setText("济南大学操场是标准化的操场,跑道为塑胶场地,即使下雨天也不会存在积水;场地中央为人工养殖的草皮,这里曾经是我们山东女足的主场;场地的四周有可以供观赏的坐席.");
}
else if(ch1.getSelectedItem()=="新西南门"){
tf1.setText("新西南门是济南大学新的象征,最近几年刚刚建成,前面是一个大的花坛,象征了济南大学欣欣向荣的景象。踏入校门正对着的是济南大学的校徽和图书馆。");
}
else if(ch1.getSelectedItem()=="西南门"){
tf1.setText("西南门是通向南院宿舍的一个大门,南临104国道附线;走进大门,喷泉映入眼帘,接着是一个象征时间的大表,可以让每一个人清晰的知道现在是什么时间.");
}
else if(ch1.getSelectedItem()=="一食堂"){
tf1.setText("一食堂原来是我们大一经常光顾的食堂,旁边有风味和好莱芜餐厅,一般24小时提供饭菜,非常方便同学就餐!");
}
else if(ch1.getSelectedItem()=="五教"){
tf1.setText("五教现在名字改为了十教,一个环行的结构,一共五层,内部有很多自习室,以供同学们学习!");
}
else if(ch1.getSelectedItem()=="六教"){
tf1.setText("六教是一座阶梯教室,听说是济南最大的阶梯教室,一共六层,教室很多,是我们学校标志型建筑!");
}
else if(ch1.getSelectedItem()=="图书馆"){
tf1.setText("图书馆是我校最壮观的建筑,与计算机楼相连,内部有阅览室,自习室,是同学们掌握知识的一座书库!");
}
else if(ch1.getSelectedItem()=="喷泉"){
tf1.setText("喷泉在老一教的前面,夏天将展示出一道靓丽的风景!");
}
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseClicked(MouseEvent mec) { //单击鼠标右键响应
if(mec.getModifiers()==mec.BUTTON3_MASK)
pm1.show(f,mec.getX(),mec.getY());
}
public void actionPerformed(ActionEvent e) { //选择菜单时触发事件
if(e.getActionCommand()=="地图")
dt.setVisible(true);
if(e.getActionCommand()=="退出")
System.exit(0);
if(e.getActionCommand()=="地图信息查询")
df.setVisible(true);
if(e.getActionCommand()=="最短路径查询")
ds.setVisible(true);
if(e.getActionCommand()=="关于")
dh.setVisible(true);
if(e.getActionCommand()=="旅游景点查询")
df.setVisible(true);
if(e.getActionCommand()=="景点最短路径")
ds.setVisible(true);
if(e.getSource()==bt) //最短路径操作响应及显示
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
{
cost[i][j]=10000;
cost[0][1]=cost[1][0]=4;
cost[0][2]=cost[2][0]=5;
cost[2][3]=cost[3][2]=2;
cost[3][4]=cost[4][3]=15;
cost[3][5]=cost[5][3]=12;
cost[1][8]=cost[8][1]=3;
cost[7][8]=cost[8][7]=8;
cost[7][9]=cost[9][7]=10;
cost[5][6]=cost[6][5]=1;
cost[6][9]=cost[9][6]=2;
cost[5][9]=cost[9][5]=4;
cost[1][7]=cost[7][1]=2;
cost[8][9]=cost[9][8]=8;
cost[1][1]=cost[2][2]=cost[3][3]=cost[4][4]=cost[5][5]=0;
cost[6][6]=cost[7][7]=cost[8][8]=cost[9][9]=cost[0][0]=0;
}
floyed();
displaytwo(ch2.getSelectedIndex(),ch3.getSelectedIndex());
}
}
public static void main(String arg[])
{
(new MyClass()).display();
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -