Java实现图形界面,java开发图形界面用什么技术
户端 try { dataOutputStream.writeUTF(str); jTextArea.append("服务器:"+str + "n"); jTextField.setText(""); } catch (IOException ioException) { ioException.printStackTrace(); } } } } public static void main(String[] args) { new ServerDemo01(); }}ClientDemo01.java
import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;public class ClientDemo01 implements ActionListener { private Socket socket; private DataInputStream dataInputStream; private DataOutputStream dataOutputStream; private JFrame jFrame; private JScrollPane jScrollPane; private JTextArea jTextArea; private JPanel jPanel; private JTextField jTextField; private JButton jButton; public ClientDemo01() { //创建组件 jFrame = new JFrame("客户端"); jTextArea = new JTextArea(); jScrollPane = new JScrollPane(jTextArea); jPanel = new JPanel(); jTextField = new JTextField(20); jButton = new JButton("发送"); //添加组件 jFrame.add(jScrollPane, BorderLayout.CENTER); jFrame.add(jPanel,BorderLayout.SOUTH); jPanel.add(jTextField); jPanel.add(jButton); //设置窗体 jFrame.setBounds(610,200,400,300); jFrame.setVisible(true); jFrame.setResizable(false); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //添加监听 jButton.addActionListener(this); //TCP网络通信 try { socket = new Socket("127.0.0.1",9999); dataInputStream = new DataInputStream(socket.getInputStream()); dataOutputStream = new DataOutputStream(socket.getOutputStream()); //接收服务器的消息 while (true) { String strServer = dataInputStream.readUTF(); jTextArea.append("服务器:"+strServer + "n"); } } catch (IOException e) { e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent e) { if(e.getSource()==jButton){ String str = jTextField.getText(); if(str.length()==0) JOptionPane.showMessageDialog(jTextField,"发送内容不能为空"); else{ try { dataOutputStream.writeUTF(str); jTextArea.append("客户端:"+str + "n"); jTextField.setText(""); } catch (IOException ioException) { ioException.printStackTrace(); } } } } public static void main(String[] args) { new ClientDemo01(); }}
运行截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。