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

📄 exercise20_2client.java

📁 这是一个基于JAVA的网络编程的例子
💻 JAVA
字号:
// Exerise20_2Client.javaimport java.io.*;import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;public class Exercise20_2Client extends JFrame implements ActionListener {  // Text fields  private JTextField jtfName = new JTextField(32);  private JTextField jtfStreet = new JTextField(32);  private JTextField jtfCity = new JTextField(20);  private JTextField jtfState = new JTextField(2);  private JTextField jtfZip = new JTextField(5);  // Buttons  private JButton jbtRegister = new JButton("Register");  private JButton jbtView = new JButton("View");  private Socket socket;  private BufferedReader in;  private PrintWriter out;  public Exercise20_2Client() {    // Panel p1 for holding labels Name, Street, and City    JPanel p1 = new JPanel();    p1.setLayout(new GridLayout(3, 1));    p1.add(new JLabel("Name"));    p1.add(new JLabel("Street"));    p1.add(new JLabel("City"));    // Panel jpState for holding state    JPanel jpState = new JPanel();    jpState.setLayout(new BorderLayout());    jpState.add(new JLabel("State"), BorderLayout.WEST);    jpState.add(jtfState, BorderLayout.CENTER);    // Panel jpZip for holding zip    JPanel jpZip = new JPanel();    jpZip.setLayout(new BorderLayout());    jpZip.add(new JLabel("Zip"), BorderLayout.WEST);    jpZip.add(jtfZip, BorderLayout.CENTER);    // Panel p2 for holding jpState and jpZip    JPanel p2 = new JPanel();    p2.setLayout(new BorderLayout());    p2.add(jpState, BorderLayout.WEST);    p2.add(jpZip, BorderLayout.CENTER);    // Panel p3 for holding jtfCity and p2    JPanel p3 = new JPanel();    p3.setLayout(new BorderLayout());    p3.add(jtfCity, BorderLayout.CENTER);    p3.add(p2, BorderLayout.EAST);    // Panel p4 for holding jtfName, jtfStreet, and p3    JPanel p4 = new JPanel();    p4.setLayout(new GridLayout(3, 1));    p4.add(jtfName);    p4.add(jtfStreet);    p4.add(p3);    // Place p1 and p4 into jpAddress    JPanel jpAddress = new JPanel(new BorderLayout());    jpAddress.add(p1, BorderLayout.WEST);    jpAddress.add(p4, BorderLayout.CENTER);    // Set the panel with line border    jpAddress.setBorder(new BevelBorder(BevelBorder.RAISED));    // Add buttons to a panel    JPanel jpButton = new JPanel();    jpButton.add(jbtRegister);    jpButton.add(jbtView);    // Add jpAddress and jpButton to the frame    getContentPane().add(jpAddress, BorderLayout.CENTER);    getContentPane().add(jpButton, BorderLayout.SOUTH);    jbtRegister.addActionListener(this);    jbtView.addActionListener(this);	try {	  socket = new Socket("127.0.0.1", 8000);      in = new BufferedReader(new InputStreamReader(socket.getInputStream()));      out = new PrintWriter(socket.getOutputStream(), true);    }    catch(IOException ex) {      System.out.println("Error " + ex);    }  }  /** Handle button actions */  public void actionPerformed(ActionEvent e) {    try {      if (e.getSource() == jbtRegister) {	    out.println("Register");		out.println(jtfName.getText().trim());		out.println(jtfStreet.getText().trim());		out.println(jtfCity.getText().trim());        out.println(jtfState.getText().trim());        out.println(jtfZip.getText().trim());      }      else if (e.getSource() == jbtView) {	    out.println("View");		out.println(jtfName.getText().trim());		String command = in.readLine();		System.out.println(command);		if(command.equals("not found")){	      jtfName.setText(command);	    }	    else{		  jtfStreet.setText(in.readLine());          jtfCity.setText(in.readLine());          jtfState.setText(in.readLine());          jtfZip.setText(in.readLine());	    }      }    }    catch(IOException ex) {      System.out.print("Error: " + ex);    }  }  public static void main(String[] args) {    Exercise20_2Client frame = new Exercise20_2Client();    frame.pack();    frame.setTitle("Exercise20_2Client");    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    frame.setVisible(true);  }}

⌨️ 快捷键说明

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