?? mainframe.java
字號:
package RTPTransmit;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.net.InetAddress;import javax.swing.filechooser.FileFilter;import javax.media.format.*;import javax.media.*;import java.util.*;// 主界面類public class MainFrame extends Frame{ private String fileName = null; // 獲取要傳輸?shù)奈募?/span> private RTPTransmit rtpTransmit = null; // RTP傳輸類的對象 Label labelIP = new Label(); TextField textIPAdd1 = new TextField(); // IP地址編輯框 TextField textIPAdd2 = new TextField(); TextField textIPAdd3 = new TextField(); TextField textIPAdd4 = new TextField(); Label labelPort = new Label(); TextField textPort = new TextField(); // 端口編輯框 JLabel jLabelIP = new JLabel(); Label labelFile = new Label(); CheckboxGroup checkboxGroupFiles = new CheckboxGroup(); Checkbox checkboxMov = new Checkbox(); // 選擇QuickTime文件(Mov)單選框 Checkbox checkboxAudio = new Checkbox(); // 選擇Audio文件單選框 Checkbox checkboxMPEG = new Checkbox(); // 選擇MPEG文件單選框 Button buttonFile = new Button(); // “瀏覽”文件按鈕 TextField textFile = new TextField(); // 顯示文件名編輯框 JLabel jLabelFile = new JLabel(); Button buttonBeginTransmit = new Button(); // “傳輸”按鈕 Button buttonStopTransmit = new Button(); // “停止”按鈕 // 設(shè)置界面和添加事件的監(jiān)聽 private void jbInit() throws Exception { this.setLayout(null); this.setBackground(Color.lightGray); labelIP.setText("IP地址:"); labelIP.setBounds(new Rectangle(50, 50, 50, 20)); textIPAdd1.setBounds(new Rectangle(125, 50, 40, 20)); textIPAdd2.setBounds(new Rectangle(175, 50, 40, 20)); textIPAdd3.setBounds(new Rectangle(225, 50, 40, 20)); textIPAdd4.setBounds(new Rectangle(275, 50, 40, 20)); labelPort.setText("端口號:"); labelPort.setBounds(new Rectangle(50, 90, 50, 20)); textPort.setBounds(new Rectangle(125, 90, 40, 20)); jLabelIP.setBorder(BorderFactory.createEtchedBorder()); jLabelIP.setBounds(new Rectangle(29, 33, 313, 91)); labelFile.setText("文件類型:"); labelFile.setBounds(new Rectangle(50, 180, 70, 20)); checkboxMov.setLabel("QuickTime Files"); checkboxMov.setBounds(new Rectangle(125, 160, 120, 15)); checkboxMov.setCheckboxGroup(checkboxGroupFiles); checkboxAudio.setLabel("Audio Files"); checkboxAudio.setBounds(new Rectangle(125, 180, 120, 15)); checkboxAudio.setCheckboxGroup(checkboxGroupFiles); checkboxMPEG.setLabel("MPEG Files"); checkboxMPEG.setBounds(new Rectangle(125, 200, 120, 15)); checkboxMPEG.setCheckboxGroup(checkboxGroupFiles); checkboxGroupFiles.setSelectedCheckbox(checkboxMov); buttonFile.setLabel("瀏覽"); buttonFile.setBounds(new Rectangle(50, 240, 58, 20)); buttonFile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { buttonFile_actionPerformed(e); } }); textFile.setBounds(new Rectangle(125, 240, 190, 20)); jLabelFile.setBorder(BorderFactory.createEtchedBorder()); jLabelFile.setBounds(new Rectangle(29, 147, 314, 127)); buttonBeginTransmit.setLabel("傳輸"); buttonBeginTransmit.setBounds(new Rectangle(94, 296, 58, 20)); buttonBeginTransmit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { buttonBeginTransmit_actionPerformed(e); } }); buttonStopTransmit.setLabel("停止"); buttonStopTransmit.setBounds(new Rectangle(214, 297, 58, 20)); buttonStopTransmit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { buttonStopTransmit_actionPerformed(e); } }); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) { this_windowClosing(e); } }); this.add(buttonStopTransmit, null); this.add(buttonBeginTransmit, null); this.add(checkboxMov, null); this.add(labelIP, null); this.add(textIPAdd1, null); this.add(textIPAdd2, null); this.add(textIPAdd3, null); this.add(textIPAdd4, null); this.add(labelPort, null); this.add(textPort, null); this.add(jLabelIP, null); this.add(labelFile, null); this.add(checkboxAudio, null); this.add(checkboxMPEG, null); this.add(buttonFile, null); this.add(textFile, null); this.add(jLabelFile, null); this.setSize(new Dimension(371, 335)); this.setTitle("RTP Transmit"); // 設(shè)置框架標(biāo)題 this.setVisible(true); // 顯示出框架 } // 構(gòu)造函數(shù) public MainFrame() { try { jbInit(); // 顯示出界面 } catch(Exception e) { e.printStackTrace(); } } // 得到所需傳輸文件的類型 int getFileType() { int indexTypeFile = 0; if(checkboxGroupFiles.getSelectedCheckbox() == checkboxMov) // QuickTime文件 indexTypeFile = 0; if(checkboxGroupFiles.getSelectedCheckbox() == checkboxAudio) // 音頻文件 indexTypeFile = 1; if(checkboxGroupFiles.getSelectedCheckbox() == checkboxMPEG) // MPEG文件 indexTypeFile = 2; return indexTypeFile; } // 響應(yīng)“瀏覽”按鈕的點(diǎn)擊消息 void buttonFile_actionPerformed(ActionEvent e) { JFileChooser fileChooser = new JFileChooser("D:"); // 選擇文件的默認(rèn)路徑是“D:”盤 ExampleFileFilter filter = new ExampleFileFilter(); // 實(shí)例化一個文件過濾器 int iTypeFile = getFileType(); // 得到所需傳輸文件的類型 switch(iTypeFile) { case 0: // QuickTime文件 filter.addExtension("mov"); // 設(shè)置文件擴(kuò)展名 filter.setDescription("QuickTime Files"); // 設(shè)置文件的類型描述 break; case 1: // 音頻文件 filter.addExtension("au"); filter.addExtension("wav"); filter.setDescription("Audio Files"); break; case 2: // MPEG文件 filter.addExtension("mpg"); filter.addExtension("mpeg"); filter.setDescription("MPEG Files"); break; } fileChooser.setFileFilter(filter); int retVal = fileChooser.showOpenDialog(this); // 打開文件選擇對話框 if(retVal == JFileChooser.APPROVE_OPTION){ fileName = fileChooser.getSelectedFile().getAbsolutePath(); // 得到所選文件 textFile.setText(fileName); // 將文件名顯示到界面上 } } // 響應(yīng)“傳輸”按鈕的點(diǎn)擊消息,開始傳輸數(shù)據(jù) void buttonBeginTransmit_actionPerformed(ActionEvent e) { String strIPAddr = textIPAdd1.getText()+"."+textIPAdd2.getText()+"."+textIPAdd3.getText()+"."+textIPAdd4.getText(); // 組合得到完整的IP地址 String strPort = textPort.getText(); // 得到端口地址 fileName = textFile.getText(); // 得到文件名 fileName = "file:/" + fileName; // 加上文件標(biāo)識,以便媒體定位器確認(rèn)數(shù)據(jù)類型 MediaLocator medLoc = new MediaLocator(fileName); // 用本機(jī)的一個磁盤文件作為待傳輸?shù)拿襟w數(shù)據(jù) Format fmt = null; rtpTransmit = new RTPTransmit(medLoc,strIPAddr,strPort,fmt); String result = rtpTransmit.start(); // 開始傳輸 if (result != null) { // 顯示傳輸錯誤 System.out.println("Error : " + result); } else { System.out.println("Start transmission ..."); } } // 響應(yīng)“停止”按鈕的點(diǎn)擊消息,停止發(fā)送數(shù)據(jù) void buttonStopTransmit_actionPerformed(ActionEvent e) { if(rtpTransmit == null) return; rtpTransmit.stop(); // 停止傳輸 System.out.println("...transmission ended."); } // 關(guān)閉窗口,退出程序 void this_windowClosing(WindowEvent e) { System.exit(0); } // 主函數(shù) public static void main(String [] args) { new MainFrame(); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -