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

📄 com.txt

📁 java 实现用串口收发短信的图形界面程序
💻 TXT
字号:
package mp;


import java.io.*;
import java.util.*;
import javax.comm.*;
import java.awt.*;
import java.util.Properties;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Rectangle;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.sql.*;


public class com extends JFrame implements ActionListener,Runnable,SerialPortEventListener
{
    static Enumeration portList;
    static CommPortIdentifier portId;
    static SerialPort serialPort;
    static OutputStream outputStream;
    static InputStream inputStream;
    static CommDriver driver=null;
    static Properties props = new Properties();
    static boolean use=false;
    private static  String databaseConnection;
    static Icon [] icon={
    new ImageIcon("D:\\123.gif"),
    new ImageIcon("D:\\1.gif"),
    };
   



    Thread readThread;
    JScrollPane p1;
    JTextField Text=new JTextField(10);
    JButton ok=new JButton("确定",icon[1]);
    JTextArea area=new JTextArea(15,10);
    JLabel l1=new JLabel("Message:");
    com() throws ClassNotFoundException, SQLException {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String   url="jdbc:odbc:db1";


       Connection   con=DriverManager.getConnection(url,"1234","1234");
       Statement statement1=con.createStatement();

        setTitle("Com");
        setVisible(true);
        setLayout(null);
        p1=new JScrollPane(area);
        add(l1);l1.setBounds(35,30,100,25);
        add(Text);Text.setBounds(120,30,150,25);
        add(ok);ok.setBounds(290,30,80,23);
        add(p1);p1.setBounds(35,70,340,250);
        ok.addActionListener(this);
        setSize(400,400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setResizable(true);
        setVisible(true);
        Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((screen.width-400)/2,(screen.height-400)/2);
        addWindowListener
        (
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(1);
                }
            }
        );
        try {
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==ok)
        {
              String key="Driver";
              String driverName="com.sun.comm.Win32Driver";
              try
              {
                driver =(CommDriver) Class.forName(driverName).newInstance();
                driver.initialize();
              }
            catch(Exception a){JOptionPane.showMessageDialog(null,a);}
            String message=Text.getText();
            boolean portFound=false;
            String defaultPort="COM1";
            portList=CommPortIdentifier.getPortIdentifiers();
            System.out.println(portList.hasMoreElements());
            while(portList.hasMoreElements())
            {
                portId=(CommPortIdentifier)portList.nextElement();
                System.out.println("portId:"+portId.getName());
                if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)
                {
                    if(portId.getName().equals(defaultPort))
                    {
                        portFound=true;
                        if(!use)
                        {
                            try
                            {
                                serialPort=(SerialPort)portId.open("COM1",2000);
                            }
                            catch(PortInUseException a){JOptionPane.showMessageDialog(null,"端口正在使用");continue;}
                        }
                        try
                        {
                            outputStream=serialPort.getOutputStream();
                        }
                        catch(IOException a){}
                        try
                        {
                            serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
                        }
                        catch(UnsupportedCommOperationException a){}
                        try
                        {
                            serialPort.notifyOnOutputEmpty(true);
                        }
                        catch(Exception a){}
                        try
                        {
                            outputStream.write(message.getBytes());
                            area.append("\nsend to port is sucess:  "+message);
                            System.out.println(message);
                            use=true;
                        }
                        catch(IOException a){}
                    }
                }
            }
            if(!portFound)
            {
                JOptionPane.showMessageDialog(null,"can't find the port");
            }
            try
            {
                inputStream=serialPort.getInputStream();
            }
            catch(IOException a){}
            try
            {
                serialPort.addEventListener(this);
            }
            catch(TooManyListenersException a){}
            serialPort.notifyOnDataAvailable(true);
            readThread=new Thread(this);
            readThread.start();
        }
    }
    public void run()
    {
        try
        {
            Thread.sleep(2000);
        }
        catch(InterruptedException e){}
    }
    public static void main(String[] args)throws ClassNotFoundException,SQLException
    {
 

        new com();
    }
    public void serialEvent(SerialPortEvent event)
    {
         switch(event.getEventType())
         {
            case SerialPortEvent.BI:
            case SerialPortEvent.OE:
             case SerialPortEvent.FE:
             case SerialPortEvent.PE:
             case SerialPortEvent.CD:
             case SerialPortEvent.CTS:
             case SerialPortEvent.DSR:
             case SerialPortEvent.RI:
             case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
             break;
             case SerialPortEvent.DATA_AVAILABLE:
             byte[] readBuffer=new byte[20];
             try
             {
                 while(inputStream.available()>0)
                 {
                      int numBytes=inputStream.read(readBuffer);
                      area.setLineWrap(true);
                      area.append(new String(readBuffer));
                 }
             }
             catch(IOException e){}
            break;
        }
    }

    private void jbInit() throws Exception {
        ok.addActionListener(new com_ok_actionAdapter(this));
        ok.addAncestorListener(new com_ok_ancestorAdapter(this));
        this.getContentPane().setBackground(UIManager.getColor(
                "FormattedTextField.selectionBackground"));
        l1.setBackground(SystemColor.control);
    }

    public void ok_actionPerformed(ActionEvent e) {

    }

    public void ok_ancestorAdded(AncestorEvent event) {

    }

}


class com_ok_ancestorAdapter implements AncestorListener {
    private com adaptee;
    com_ok_ancestorAdapter(com adaptee) {
        this.adaptee = adaptee;
    }

    public void ancestorAdded(AncestorEvent event) {
        adaptee.ok_ancestorAdded(event);
    }

    public void ancestorRemoved(AncestorEvent event) {
    }

    public void ancestorMoved(AncestorEvent event) {
    }
}


class com_ok_actionAdapter implements ActionListener {
    private com adaptee;
    com_ok_actionAdapter(com adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.ok_actionPerformed(e);
    }
}

⌨️ 快捷键说明

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