使用Swing与基于UDP的Socket程序实现一个存钱系统
1.使用Swing实现客户端的图形界面,包括单选框(5元、10元、20元3种选项)和存入按钮。
2.选中相应的钱数点击存入按钮后,把钱数提交给服务器。
3.服务器对所有存入的钱数进行累积,并把当前累积的钱数返回给客户端。
4.客户端在存入按钮下面显示当前总钱数。
1.使用Swing实现客户端的图形界面,包括单选框(5元、10元、20元3种选项)和存入按钮。
2.选中相应的钱数点击存入按钮后,把钱数提交给服务器。
3.服务器对所有存入的钱数进行累积,并把当前累积的钱数返回给客户端。
4.客户端在存入按钮下面显示当前总钱数。
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
/**
*
* @author Administrator
*/
public class Client extends javax.swing.JFrame {
private final ButtonGroup g;
private final DatagramSocket socket;
private java.awt.Button button;
private javax.swing.JLabel jLabel1;
private javax.swing.JRadioButton jRadioButton1;
private javax.swing.JRadioButton jRadioButton2;
private javax.swing.JRadioButton jRadioButton3;
private static final long serialVersionUID = 1L;
/**
* Creates new form SS
*/
public Client() throws SocketException {
this.socket = new DatagramSocket();
socket.setSoTimeout(8000);
this.g = new ButtonGroup();
initComponents();
g.add(this.jRadioButton1);
g.add(this.jRadioButton2);
g.add(this.jRadioButton3);
byte[] b = new byte[4];
try {
final DatagramPacket packet = new DatagramPacket(b,0,b.length,java.net.InetAddress.getByName("127.0.0.1"),12345);
socket.send(packet);
new Thread(() -> {
while(true){
try {
packet.setData(new byte[4]);
socket.receive(packet);
byte[] sum = packet.getData();
int res = (sum[0] & 0xff )<<24 | (sum[1] & 0xff)<< 16 | (sum[2] & 0xff) << 8 | (sum[3] & 0xff);
this.jLabel1.setText("余额:"+res);
} catch (IOException ex) {
packet.setData(new byte[]{0});
try {
socket.send(packet);
} catch (IOException ex1) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex1);
}
}
}
}).start();
} catch (UnknownHostException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
} private void initComponents() { jRadioButton1 = new javax.swing.JRadioButton();
jRadioButton2 = new javax.swing.JRadioButton();
jRadioButton3 = new javax.swing.JRadioButton();
button = new java.awt.Button();
jLabel1 = new javax.swing.JLabel("余额",JLabel.CENTER); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jRadioButton1.setText("5元"); jRadioButton2.setText("10元"); jRadioButton3.setText("30元"); button.setLabel("SaveMoney");
button.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
buttonMouseClicked(evt);
}
}); jLabel1.setText("未连接服务器,正在重连……"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(200, 200, 200)
.addComponent(button, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addGap(100, 100, 100)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jRadioButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE)
.addComponent(jRadioButton2)
.addGap(128, 128, 128)
.addComponent(jRadioButton3)
.addGap(90, 90, 90))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jRadioButton1)
.addComponent(jRadioButton2)
.addComponent(jRadioButton3))
.addGap(67, 67, 67)
.addComponent(button, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(51, 51, 51)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(62, Short.MAX_VALUE))
); jRadioButton3.setSelected(true); pack();
}// </editor-fold>
private void buttonMouseClicked(java.awt.event.MouseEvent evt) {
byte flag = 0;
if(this.jRadioButton1.isSelected()){
flag = 5;
}else if(this.jRadioButton2.isSelected()){
flag = 10;
}else{
flag = 30;
}
byte[] b = {flag};
try {
DatagramPacket packet = new DatagramPacket(b,0,b.length,java.net.InetAddress.getByName("127.0.0.1"),12345);
this.socket.send(packet);
} catch (UnknownHostException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
} /**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Client.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} java.awt.EventQueue.invokeLater(() -> {
try {
new Client().setVisible(true);
} catch (SocketException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
}Serverpackage Test;import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;public class Server {
private static int sum = 0;
private static DatagramSocket socket;
public static void main(String[] args) throws IOException {
socket = new DatagramSocket(12345);
byte[] b = new byte[1];
DatagramPacket packet = new DatagramPacket(b, b.length);
while(true) {
socket.receive(packet);
sum += packet.getData()[0];
byte[] send = {(byte) (sum>>24),(byte) (sum>>16),(byte) (sum>>8),(byte) sum};
packet.setData(send);
socket.send(packet);
}
}}