📄 frame1.java~1~
字号:
package udpclient;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.borland.jbcl.layout.*;import java.net.*;import java.io.*;/** * <p>Title: an example</p> * <p>Description:an no</p> * <p>Copyright: Copyright (c) 2002</p> * <p>Company: home</p> * @author liujun * @version 1.0 */public class Frame1 extends JFrame implements Runnable{ private JPanel contentPane; private TextArea textArea1 = new TextArea(); private XYLayout xYLayout1 = new XYLayout(); private Label label1 = new Label(); private TextField textField1 = new TextField(); private Button button1 = new Button(); private Button button2 = new Button(); private Button button3 = new Button(); DatagramSocket socket = null; BufferedReader in = null; PrintWriter out = null; //Construct the frame public Frame1() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]"))); contentPane = (JPanel) this.getContentPane(); textArea1.setText(" "); contentPane.setLayout(xYLayout1); this.setSize(new Dimension(400, 239)); this.setTitle("Frame Title"); label1.setText("消息:"); textField1.setText(" "); textField1.addKeyListener(new java.awt.event.KeyAdapter() { public void keyPressed(KeyEvent e) { textField1_keyPressed(e); } }); button1.setLabel("开始发送"); button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button1_actionPerformed(e); } }); button2.setLabel("连接"); button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button2_actionPerformed(e); } }); button3.setLabel("退出"); button3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { button3_actionPerformed(e); } }); contentPane.add(textArea1, new XYConstraints(8, 5, 382, 147)); contentPane.add(textField1, new XYConstraints(83, 157, 302, 23)); contentPane.add(label1, new XYConstraints(20, 158, 49, 23)); contentPane.add(button1, new XYConstraints(18, 188, 85, 23)); contentPane.add(button2, new XYConstraints(202, 188, 85, 23)); contentPane.add(button3, new XYConstraints(304, 188, 85, 23)); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { //添加代码 try{ out.println("client exit!"); out.flush(); }catch(Exception ex){} finally{ System.exit(0); } } } void button1_actionPerformed(ActionEvent e) { out.println(textField1.getText()); out.flush(); textArea1.append("client information:"+textField1.getText()+"\n"); textField1.setText(""); } void button3_actionPerformed(ActionEvent e) { try{ out.println("client exit!"); out.flush(); }catch(Exception e2){} finally{ System.exit(0); } } void button2_actionPerformed(ActionEvent e) { Thread thread = new Thread(this); thread.start(); } void textField1_keyPressed(KeyEvent e) { int j=e.getKeyCode(); if(j==e.VK_ENTER){ out.println(textField1.getText()); out.flush(); textArea1.append("client information:"+textField1.getText()+"\n"); textField1.setText(""); } } public void run() { try{ //在5438端口上打开到liujun的连接,说明它其实就是你的本地计算机名 //如果你的计算机的名称不是“liujun”,请修改为你的计算机名称 //创建数据报,获取一个数据报套接字 socket = new DatagramSocket(); //发送请求 byte[] buf = new byte[256]; InetAddress address = InetAddress.getByName("liujun"); DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 1800); socket.send(packet); in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream()); button1.setEnabled(true); receiver r = new receiver(); Thread t = new Thread(r); t.start(); textArea1.append("system information:have joined to server!\n"); button2.setEnabled(false); }catch(Exception e){ textArea1.append(e.toString()+"\n"); } } private class receiver implements Runnable{ public void run(){ String s1 = null; try{ s1 = in.readLine(); while(s1!= "client exit!"){ textArea1.append("client information: "+s1+"\n"); s1 = in.readLine(); } in.close(); out.close(); socket.close(); }catch(Exception e){} button1.setEnabled(false); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -