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

📄 database_client.java

📁 书中的例题
💻 JAVA
字号:
import java.net.*;import java.io.*;
import java.awt.*;import java.awt.event.*;
import java.applet.*;
public class Database_client extends Applet 
implements Runnable,ActionListener
{ Button 查询;
  TextField 商品名称_text,商品价格_text;
  Socket socket=null;
  DataInputStream in=null;
  DataOutputStream out=null;
  Thread thread; 
 public void init()
 {查询=new Button("查询");
  商品名称_text=new TextField(10);
  商品价格_text=new TextField(10);
  add(new Label("查询的商品名称"));
  add(商品名称_text);
  add(new Label("商品价格:"));
  add(商品价格_text);
  add(查询);
  查询.addActionListener(this);
 }
 public void start()
 {
   try{
      socket = new Socket(this.getCodeBase().getHost(), 4331);
      in = new DataInputStream(socket.getInputStream());
      out = new DataOutputStream(socket.getOutputStream());
     } 
   catch (IOException e){System.out.println(e.getMessage());}
   if (thread == null) {
      thread = new Thread(this);
      thread.setPriority(Thread.MIN_PRIORITY);
      thread.start();
     }
 }
 public void stop()
 {
	try{out.writeUTF("客户离开");}
    catch(IOException e1){System.out.println(e1.getMessage());} 
 } 
 public void destroy()
 {
	try{out.writeUTF("客户离开");}
    catch(IOException e1){System.out.println(e1.getMessage());} 
 } 
 public void run()
  { String s=null;
    while(true)
     { try{s=in.readUTF();  }
       catch (IOException e)
          {  商品价格_text.setText("与服务器已断开");
	         break;
          }
       商品价格_text.setText(s);
     }
  }
 public void actionPerformed(ActionEvent e)
 {if (e.getSource()==查询)
     { String s=商品名称_text.getText();
       if(s!=null)		      
        { try{out.writeUTF(s);}
          catch(IOException e1){System.out.println(e1.getMessage());} 
        }               
     }
 }
}

⌨️ 快捷键说明

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