?? gui.java
字號:
import javax.swing.*;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
//import second.WindowDestroyer;
import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class GUI extends JFrame implements ActionListener
{
Icon banner = new ImageIcon("E:\\圖片\\ditu.jpg");
JLabel ditu =new JLabel();
JTextField[] field=new JTextField[2];
JTextArea result=new JTextArea(24,30);
private Mistake me;
public static final int WIDTH=700;
public static final int HEIGHT=460;
{
try
{
InputStream in = new FileInputStream("E:\\音樂視頻\\歌曲\\今天.wav");
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
}
catch(FileNotFoundException e)
{
System.out.print("FileNotFoundException ");
}
catch(IOException e)
{
System.out.print("有錯誤!");
}
ditu.setIcon(banner);
setResizable(false);
setLocation(400, 50);
setTitle("GPS定位系統");
setSize(WIDTH,HEIGHT);
Container content=getContentPane();
content.setBackground(Color.WHITE);
content.setLayout(new BorderLayout());//建立窗體及容器
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowDestroyer());
JPanel map=new JPanel();
JPanel button=new JPanel();
JTextArea grap=new JTextArea(24,30);
button.setLayout(new FlowLayout());
result.setEditable(false);
field[0]=new JTextField(10);
field[1]=new JTextField(10);
JLabel lb[]=new JLabel[2];
Font font=new Font("隸書",Font.BOLD,25);
lb[0]=new JLabel("始點 ");
lb[1]=new JLabel("終點 ");
JButton []b=new JButton[3];
b[0]=new JButton("線路查詢");
b[1]=new JButton("地點管理");
b[2]=new JButton(" 退出 ");
for (int i=0;i<2;i++)
{
lb[i].setFont(font);
button.add(lb[i]);
button.add(field[i]);
}
for(int i=0;i<3;i++)
{
b[i].addActionListener(this);//監聽器
button.add(b[i]);
}
for(int i=0;i<2;i++)
{
field[i].addActionListener(this);//監聽器
}
grap.setBackground(Color.orange);
result.setBackground(Color.magenta);
map.add(ditu,BorderLayout.WEST);
map.add(result,BorderLayout.EAST);
content.add(map,BorderLayout.CENTER);
content.add(button,BorderLayout.SOUTH);//添加按鈕槽
setVisible(true);
}
//
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("線路查詢"))
{
try
{
if(field[0].getText().equals("")||field[1].getText().equals("")){
JOptionPane.showMessageDialog(null,"請輸入完整信息!","無效操作",1);
try
{
InputStream in = new FileInputStream("E:\\圖片\\ku.wav");
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
}
catch(FileNotFoundException a)
{
System.out.print("FileNotFoundException ");
}
catch(IOException a)
{
System.out.print("有錯誤!");
} }
else if(field[0].getText().equals(field[1].getText()))
result.setText("始點與終點相同 \n 總長 0 千米");
else
{
Map m=new Map();
int s=m.getnumber(field[0].getText());
int t=m.getnumber(field[1].getText());
int n=m.getamount();
int d[]=new int[n+1], p[]=new int[n+1];
String r[]=new String[n+1];
m.Search(s,t,d,p);
if(m.Search(s,t,d,p)==0)
{
try
{
InputStream in = new FileInputStream("E:\\圖片\\ku.wav");
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
}
catch(FileNotFoundException a)
{
System.out.print("FileNotFoundException ");
}
catch(IOException a)
{
System.out.print("有錯誤!");
}
result.setText("從"+ field[0].getText() +"到"+field[1].getText() +"無路抵達");
}
else
{
int temp=t;int i=0;
while(temp!=0)
{
r[i]=m.getname(temp);
temp=p[temp];
i++;
}
String rote="此結束";
for( i=0;i<=n;i++)
if(r[i]!=null)
rote=r[i]+" 到 "+rote;
try
{
InputStream in = new FileInputStream("E:\\圖片\\huanhu.wav");
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);
}
catch(FileNotFoundException a)
{
System.out.print("FileNotFoundException ");
}
catch(IOException a)
{
System.out.print("有錯誤!");
}
result.setText("最短路徑為: \n "+ rote +"\n總長為: \n "+d[t]+" 千米");
}
}
}
catch(Exception ee)
{
me=new Mistake("地名輸入錯誤!");
}
}
else if(e.getActionCommand().equals("地點管理"))
{//
myframe1 frame1 =new myframe1();
}
else if(e.getActionCommand().equals(" 退出 "))
{
int result = JOptionPane.showConfirmDialog(null,"您確定要退出嗎?","退出",0);
if( result == 0 )System.exit(0);
}
}
public static void main(String[]args)
{
new GUI();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -