JSWING小工具

项目中需要一个发送指令到指定服务端,取得设备ID及检测数据的小工具,就利用jswing开发了一个简单小组件,最终效果如下:

代码很简单,具体片段如下:

Window.java

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

/**
 * @author 
 * @todo 主窗体
 */
@SuppressWarnings("serial")
public class Window extends JFrame {
    public Window() {
        super("工具");
        setSize(500, 300);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        add(new Panels().set());
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

}

class Panels {
    public JPanel set() {
        // 设置面板
        JPanel panels = new JPanel();// 外层面板
        JPanel panel1 = new JPanel();// 端口指令面板
        JPanel panel2 = new JPanel();// 操作按钮面板
        JPanel panel3 = new JPanel();// 返回结果面板
        // 设置下拉
        // 设置文本
        JLabel label = new JLabel();// 指令
        JLabel label2 = new JLabel();// 结果

        // 端口
        final JComboBox combox = new JComboBox(new Integer[] { 30003, 30005,
                30006, 30008 });
        // 设置指令框
        final JTextField text = new JTextField();
        // 设置文本域
        final JTextArea textarea = new JTextArea();
        // 设置按钮
        JButton button = new JButton("测试");
        JButton button2 = new JButton("清空");
        JScrollPane scrollpane = new JScrollPane(textarea);
        scrollpane.setBorder(javax.swing.BorderFactory
                .createLineBorder(Color.GRAY));//设置边框颜色

        combox.setPreferredSize(new Dimension(80, 20));//设置下拉长宽

        label.setText("指令:");
        text.setColumns(27);
        text.setBorder(javax.swing.BorderFactory.createLineBorder(Color.GRAY));

        textarea.setEditable(false);
        textarea.setRows(9);
        textarea.setColumns(35);
        label2.setText("结果:");

        button.setHorizontalTextPosition(SwingConstants.CENTER);//设置横向位置
        button2.setHorizontalTextPosition(SwingConstants.CENTER);//设置横向位置
        // 设置按钮事件
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent args) {
                String textval = text.getText();
                Integer port = Integer.valueOf(combox.getSelectedItem()
                        .toString());
                if ("".equals(textval.trim())) {
                    Toolkit.getDefaultToolkit().beep();
                    JOptionPane.showMessageDialog(null, "请输入指令!", null,
                            JOptionPane.WARNING_MESSAGE);
                    return;
                }
                textarea.append("请求:" + textval + "\n");
                textarea.append(new Connect().doConnection(port, textval)
                        + "\n");
            }
        });
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent args) {
                textarea.setText("");
            }
        });
        // 添加至面板
        panel1.add(label);
        panel1.add(combox);
        panel1.add(text);
        panel2.add(button);
        panel2.add(button2);
        panel3.add(label2);

        scrollpane
                .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);//设置文本域不显示横向滚动条
        panel3.add(scrollpane);
        panels.add(panel1);
        panels.add(panel2);
        panels.add(panel3);
        return panels;
    }
}

Connect.java

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;

/**
 * @author 
 * @todo 发送指令 获取设备信息及数据
 */
public class Connect {
    public String doConnection(int port, String command) {
        String result;
        ServerSocket server = null;
        Socket socket = null;
        try {
            server = new ServerSocket(port);

            server.setSoTimeout(5000);
            socket = server.accept();

            InputStream is = socket.getInputStream();
            OutputStream os = socket.getOutputStream();

            os.write(command.getBytes());
            os.flush();

            byte[] response = getResponse();
            is.read(new byte[1024]);
            String valuestr = new String(response);
            result = "成功:" + valuestr;

        } catch (SocketTimeoutException e) {
            result = "失败:连接超时!";
        } catch (IOException e) {
            result = "失败:读写异常!";
        }
        try {
            if (socket != null) {
                socket.close();
            }
            if (server != null) {
                server.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }


    public byte[] getResponse() {
        return new byte[1024];
    }
}

Console.java

/**
 * @author 
 * @todo 窗体入口
 */
public class Console {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Window();
            }
        });
    }
}

 

posted @ 2015-08-11 13:49  雪卜  阅读(459)  评论(0编辑  收藏  举报