📄 testform.java
字号:
jScrollPane1,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(
jScrollPane2,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(
org.jdesktop.layout.LayoutStyle.RELATED)
.add(
layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING)
.add(btnRefGroup).add(
btnRefUsers))
.addPreferredGap(
org.jdesktop.layout.LayoutStyle.RELATED)
.add(
layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING)
.add(
layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.BASELINE)
.add(
jLabel5)
.add(
txtGroupName,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(btnJoinGroup))
.addPreferredGap(
org.jdesktop.layout.LayoutStyle.RELATED)
.add(
layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING)
.add(
layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.BASELINE)
.add(
jLabel6)
.add(
lblGroupName))
.add(btnExitGroup))
.addPreferredGap(
org.jdesktop.layout.LayoutStyle.RELATED)
.add(
layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING)
.add(jLabel10)
.add(
jScrollPane3,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(
layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.LEADING)
.add(
layout
.createSequentialGroup()
.addPreferredGap(
org.jdesktop.layout.LayoutStyle.RELATED,
18,
Short.MAX_VALUE)
.add(
jLabel11)
.add(
63,
63,
63))
.add(
layout
.createSequentialGroup()
.add(
18,
18,
18)
.add(
layout
.createParallelGroup(
org.jdesktop.layout.GroupLayout.BASELINE)
.add(
txtSendData,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(
btnSend))
.addContainerGap()))));
jLabel11.getAccessibleContext().setAccessibleName("");
pack();
}// </editor-fold>
// GEN-END:initComponents
private void lstGroupValueChanged(javax.swing.event.ListSelectionEvent evt) {
txtGroupName.setText(lstGroup.getSelectedValue().toString());
}
private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {
try {
out.write(txtSendData.getText().getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
private void btnExitGroupActionPerformed(java.awt.event.ActionEvent evt) {
byte[] data = new byte[] { Byte.MIN_VALUE, Byte.MAX_VALUE, 0x02 };
try {
out.write(data, 0, data.length);
} catch (IOException e) {
e.printStackTrace();
}
}
private Byte currentGroupId;
private void btnJoinGroupActionPerformed(java.awt.event.ActionEvent evt) {
Byte id = null;
if (group != null) {
id = group.get(txtGroupName.getText());
}
if (id == null) {
id = -0x01;
}
byte[] data = new byte[] { Byte.MIN_VALUE, Byte.MAX_VALUE, 0x01, id };
ByteArrayOutputStream bo = new ByteArrayOutputStream();
try {
bo.write(data);
bo.write(txtGroupName.getText().getBytes());
out.write(bo.toByteArray());
currentGroupId = id;
lblGroupName.setText(txtGroupName.getText());
} catch (IOException e) {
e.printStackTrace();
}
}
private void btnRefUsersActionPerformed(java.awt.event.ActionEvent evt) {
if (currentGroupId != null) {
Object key = lstGroup.getSelectedValue();
Byte id = group.get(key);
if (id == null || id == 0) {
id = currentGroupId;
}
byte[] data = new byte[] { Byte.MIN_VALUE, Byte.MAX_VALUE, 0x04, id };
try {
out.write(data);
} catch (IOException e) {
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(this, "未加入组");
}
}
private void btnRefGroupActionPerformed(java.awt.event.ActionEvent evt) {
byte[] data = new byte[] { Byte.MIN_VALUE, Byte.MAX_VALUE, 0x03 };
try {
out.write(data);
} catch (IOException e) {
e.printStackTrace();
}
}
private void btnLinkActionPerformed(java.awt.event.ActionEvent evt) {
if (linked) {
try {
byte[] data = new byte[] { Byte.MIN_VALUE, Byte.MAX_VALUE, 0x05 };
out.write(data);
revicer.stop();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
socket = new Socket(txtServer.getText(), Integer
.parseInt(txtPort.getText()));
out = socket.getOutputStream();
revicer = new Revicer(this);
int id = Integer.parseInt(txtID.getText());
byte[] data = new byte[] { Byte.MIN_VALUE, Byte.MAX_VALUE,
0x00, (byte) (id / Byte.MAX_VALUE),
(byte) (id % Byte.MAX_VALUE) };
ByteArrayOutputStream bo = new ByteArrayOutputStream();
bo.write(data, 0, data.length);
bo.write(txtName.getText().getBytes());
out.write(bo.toByteArray());
revicer.start();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
linked = !linked;
setTexts();
} else {
JOptionPane.showMessageDialog(this, "无法连接服务器");
}
}
private void setTexts() {
txtServer.setEnabled(!linked);
txtPort.setEnabled(!linked);
txtID.setEnabled(!linked);
txtName.setEnabled(!linked);
if (!linked) {
lstGroup.removeAll();
lstUser.removeAll();
}
lstGroup.setEnabled(linked);
lstUser.setEnabled(linked);
btnRefGroup.setEnabled(linked);
btnRefUsers.setEnabled(linked);
txtGroupName.setEnabled(linked);
btnJoinGroup.setEnabled(linked);
btnExitGroup.setEnabled(linked);
txtReviceData.setEnabled(linked);
txtSendData.setEnabled(linked);
btnSend.setEnabled(linked);
if (linked) {
btnLink.setText("断开");
} else {
btnLink.setText("连接");
}
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestForm().setVisible(true);
}
});
}
// GEN-BEGIN:variables
// Variables declaration - do not modify
private javax.swing.JButton btnExitGroup;
private javax.swing.JButton btnJoinGroup;
private javax.swing.JButton btnLink;
private javax.swing.JButton btnRefGroup;
private javax.swing.JButton btnRefUsers;
private javax.swing.JButton btnSend;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JLabel lblGroupName;
private javax.swing.JList lstGroup;
private javax.swing.JList lstUser;
private javax.swing.JTextField txtGroupName;
private javax.swing.JTextField txtID;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtPort;
private javax.swing.JTextArea txtReviceData;
private javax.swing.JTextField txtSendData;
private javax.swing.JTextField txtServer;
// End of variables declaration//GEN-END:variables
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
private Map<String, Byte> group = null;
private Map<String, Integer> users = null;
public void reviceData(byte[] data) {
System.out.println("收到数据" + Arrays.toString(data));
if (data[0] == Byte.MIN_VALUE && data[1] == Byte.MAX_VALUE) {
switch (data[2]) {
case 0x03:
group = new HashMap<String, Byte>();
final List<String> lstName = new ArrayList<String>();
for (int i = 3; i < data.length; i++) {
List<Byte> lst = new ArrayList<Byte>();
while (data[i] != 0x10) {
lst.add(data[i]);
i++;
}
byte id = lst.get(0);
lst.remove(0);
byte[] b = new byte[lst.size()];
for (int j = 0; j < lst.size(); j++) {
b[j] = lst.get(j);
}
String name = new String(b);
lstName.add(name);
group.put(name, id);
}
ListModel lstModel = new javax.swing.AbstractListModel() {
private static final long serialVersionUID = 1L;
public int getSize() {
return lstName.size();
}
public Object getElementAt(int i) {
return lstName.get(i);
}
};
lstGroup.setModel(lstModel);
break;
case 0x04:
users = new HashMap<String, Integer>();
final List<String> lstName2 = new ArrayList<String>();
for (int i = 3; i < data.length; i++) {
List<Byte> lst = new ArrayList<Byte>();
while (data[i] != 0x10) {
lst.add(data[i]);
i++;
}
int id = lst.get(0) * Byte.MAX_VALUE + lst.get(1);
lst.remove(0);
lst.remove(1);
byte[] b = new byte[lst.size()];
for (int j = 0; j < lst.size(); j++) {
b[j] = lst.get(j);
}
String name = new String(b);
lstName2.add(name);
users.put(name, id);
}
ListModel lstModel2 = new javax.swing.AbstractListModel() {
private static final long serialVersionUID = 1L;
public int getSize() {
return lstName2.size();
}
public Object getElementAt(int i) {
return lstName2.get(i);
}
};
lstUser.setModel(lstModel2);
break;
default:
System.out.println("未知功能");
break;
}
} else {
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String now = format.format(new Date());
txtReviceData.append(now + ":" + new String(data) + "\n");
}
}
public static int indexOf(byte[] data, int begin, byte key) {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
bo.write(data, begin, data.length - begin);
return Arrays.binarySearch(bo.toByteArray(), key);
}
}
class Revicer implements Runnable {
private TestForm frame;
private Thread thread;
public Revicer(TestForm frame) {
this.frame = frame;
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void stop() {
thread = null;
}
public void run() {
Socket socket = frame.getSocket();
try {
InputStream in = socket.getInputStream();
byte[] buffer = new byte[1024];
while (thread != null) {
int reviceCount = in.read(buffer);
ByteArrayOutputStream bo = new ByteArrayOutputStream();
bo.write(buffer, 0, reviceCount);
frame.reviceData(bo.toByteArray());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -