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

📄 clientframe.java

📁 一个服务器,客户端程序.客户端发送查询信息,服务端返回查询结果
💻 JAVA
字号:
package homework;

import javax.swing.*;
import java.net.Socket;

import java.awt.event.ActionEvent;
import java.io.*;
import java.awt.*;

/**
 * <p>Title: java source file filter</p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author Jesse
 * @version 1.0
 */
public class ClientFrame extends JFrame {
    JTextField textField;
    JTextArea textArea;
    JButton button;
    Socket soc;
    BufferedReader reader;
    PrintWriter writer;
    String IP;
    public ClientFrame() {
        super("Client");

        textField = new JTextField(20);
        button = new JButton(new sendAction());
        textArea = new JTextArea(10,30);
        this.setLayout(new FlowLayout());
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setPreferredSize(new Dimension(300, 300));
        JPanel cp = (JPanel) getContentPane();
        cp.add(textField);
        cp.add(button);
        cp.add(scrollPane);
        IP=JOptionPane.showInputDialog(this,"Input host IP","127.0.0.1");
        pack();
        this.setSize(400, 400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        try {
            soc = new Socket("127.0.0.1", 888);
            reader = new BufferedReader(new InputStreamReader(soc.
                    getInputStream()));
            writer = new PrintWriter(soc.getOutputStream());
            JOptionPane.showConfirmDialog(this,
                                          "Link to the server successfully!!",
                                          "Log in", JOptionPane.PLAIN_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showConfirmDialog(this, "Can not link to the server");
        }



    }

    class sendAction extends AbstractAction {
        sendAction() {
            super("Send");
            setEnabled(true);
        }

        public void actionPerformed(ActionEvent e) {
            String t = textField.getText();
            writer.println(t);
            writer.flush();
            try {
                t = reader.readLine();
                textArea.append(t + "\n");
            } catch (Exception f) {
                System.out.println(f);
            }

        }

    }


    public static void main(String[] args) {
        ClientFrame clientframe = new ClientFrame();
    }
}

⌨️ 快捷键说明

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