⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplecomm.java

📁 源码为科学出版社出版的英文<java设计模式>(影印版)所用的所有例子程序
💻 JAVA
字号:
import java.awt.*;import java.io.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.event.*;import java.util.*;import javax.comm.*;public class SimpleComm extends JxFrame     implements ListSelectionListener{     CommPortIdentifier portId;     Enumeration portEnum;     Vector ports;     JawtList status;     JList portList;     JScrollPane psw;     int lastIndex;          public SimpleComm() {          super ("Comm Singleton");     JPanel jp = new JPanel();     getContentPane().add(jp);     jp.setLayout (new BorderLayout());          ports = new Vector();     portList = new JList(ports);     psw = new JScrollPane();     psw.setBorder(new BevelBorder(BevelBorder.LOWERED));     psw.getViewport().setView (portList);     jp.add("West", psw);     portList.addListSelectionListener (this);     lastIndex = -1;       //index of last selected com port     //now add status list on right     status =  new JawtList(20);     jp.add("Center", status);     loadPorts();    //load into list box     setSize(new Dimension(300,200));     setVisible(true);  } //------------------------------------  public void valueChanged(ListSelectionEvent ls) {          int index = portList.getSelectedIndex();     if(index != lastIndex) {      lastIndex = index;      //debounces mouse unclick      //get port name back from list box      String portName = (String)ports.elementAt (index);      try {         //try to get port ownership         portId = CommPortIdentifier.getPortIdentifier(portName);         //if successful, open the port         CommPort cp = portId.open("SimpleComm",100);         //report success         status.add("Simplecom has: "+portName);      }      catch(NoSuchPortException e){        status.add("No such port:"+portName);      }       catch(PortInUseException e){           status.add (portName+" in use:");       }     }  } //------------------------------------  private void loadPorts() {     portEnum = CommPortIdentifier.getPortIdentifiers();        while (portEnum.hasMoreElements()) {            portId = (CommPortIdentifier) portEnum.nextElement();            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)                {               ports.addElement (portId.getName());               }        }        //add bogus port        ports.addElement("COM5");        psw.doLayout ();        psw.validate();  }  //------------------------------------  public static void main(String[] args) {         new SimpleComm();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -