「java简单聊天界面」聊天的界面
今天给各位分享java简单聊天界面的知识,其中也会对聊天的界面进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
用JAVA 编写简单网络聊天程序
/**
* 基于UDP协议的聊天程序
*
* 2007.9.18
* */
//导入包
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.net.*;
public class Chat extends JFrame implements ActionListener
{
//广播地址或者对方的地址
public static final String sendIP = "172.18.8.255";
//发送端口9527
public static final int sendPort = 9527;
JPanel p = new JPanel();
List lst = new List(); //消息显示
JTextField txtIP = new JTextField(18); //填写IP地址
JTextField txtMSG = new JTextField(20); //填写发送消息
JLabel lblIP = new JLabel("IP地址:");
JLabel lblMSG = new JLabel("消息:");
JButton btnSend = new JButton("发送");
byte [] buf;
//定义DatagramSocket的对象必须进行异常处理
//发送和接收数据报包的套接字
DatagramSocket ds = null;
//=============构造函数=====================
public Chat()
{
CreateInterFace();
//注册消息框监听器
txtMSG.addActionListener(this);
btnSend.addActionListener(this);
try
{
//端口:9527
ds =new DatagramSocket(sendPort);
}
catch(Exception ex)
{
ex.printStackTrace();
}
//============接受消息============
//匿名类
new Thread(new Runnable()
{
public void run()
{
byte buf[] = new byte[1024];
//表示接受数据报包
while(true)
{
try
{
DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort);
ds.receive(dp);
lst.add("【消息来自】◆" + dp.getAddress().getHostAddress() + "◆"+"【说】:" + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0);
}
catch(Exception e)
{
if(ds.isClosed())
{
e.printStackTrace();
}
}
}
}
}).start();
//关闭窗体事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
System.out.println("test");
int n=JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
dispose();
System.exit(0);
ds.close();//关闭ds对象//关闭数据报套接字
}
}
});
}
//界面设计布局
public void CreateInterFace()
{
this.add(lst,BorderLayout.CENTER);
this.add(p,BorderLayout.SOUTH);
p.add(lblIP);
p.add(txtIP);
p.add(lblMSG);
p.add(txtMSG);
p.add(btnSend);
txtIP.setText(sendIP);
//背景颜色
lst.setBackground(Color.yellow);
//JAVA默认风格
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setSize(600,500);
this.setTitle("〓聊天室〓");
this.setResizable(false);//不能改变窗体大小
this.setLocationRelativeTo(null);//窗体居中
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setVisible(true);
txtMSG.requestFocus();//消息框得到焦点
}
//===============================Main函数===============================
public static void main(String[]args)
{
new Chat();
}
//================================发送消息===============================
//消息框回车发送消息事件
public void actionPerformed(ActionEvent e)
{
//得到文本内容
buf = txtMSG.getText().getBytes();
//判断消息框是否为空
if (txtMSG.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"发送消息不能为空","提示",JOptionPane.WARNING_MESSAGE);
}
else{
try
{
InetAddress address = InetAddress.getByName(sendIP);
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);
ds.send(dp);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
txtMSG.setText("");//清空消息框
//点发送按钮发送消息事件
if(e.getSource()==btnSend)
{
buf = txtMSG.getText().getBytes();
try
{
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);
}
catch(Exception ex)
{
ex.printStackTrace();
}
txtMSG.setText("");//清空消息框
txtMSG.requestFocus();
}
}
}
java聊天室界面如何做
JAVA聊天室要用到:
Swing图形用户界面。JAVA中数据库的操作,以及JAVA中网络的连接
当把这些知识学好,做一个聊天室应该是不成问题的。
Swing图形用户界面:实现窗口的显示。
数据库的操作实现用户登录,聊天记录存储等功能。
网络连接实现不同客户端聊天。
==
你好,请问怎么用Java做一个简单类似qq的聊天界面.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ChatRoom extends JFrame implements MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
new ChatRoom();
}
private JFrame frame;
private JTextArea viewArea;
private JTextField viewField;
private JButton button1;
private JButton button2;
private JLabel jlable;
private JTextField MyName;
public ChatRoom(){
frame = new JFrame("Chat Room");
viewArea = new JTextArea(10, 50);
viewField = new JTextField(50);
jlable= new JLabel();
jlable.setText("在线");
button1 = new JButton("Send");
button2 = new JButton("Quit");
MyName = new JTextField();
MyName.setColumns(9);
MyName.setText("飞翔的企鹅 ");
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(8,1));
panel.add(jlable);
panel.add(MyName);
panel.add(button1);
panel.add(button2);
JScrollPane sp = new JScrollPane(viewArea);
sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add("Center",sp);
frame.add("East",panel);
frame.add("South",viewField);
frame.setSize(500,250);
frame.setVisible(true);
button1.addMouseListener((MouseListener) this);
button2.addMouseListener((MouseListener) this);
}
public void mouseClicked(MouseEvent evt){
String message = "";
message=MyName.getText()+viewField.getText();
if(evt.getSource()==button1){
viewArea.setText(viewArea.getText()+message+ "\n") ;
}
if(evt.getSource()==button2){
message = "退出";
viewArea.setText(message);
viewField.setText("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public void mousePressed(MouseEvent evt){ }
public void mouseReleased(MouseEvent evt){ }
public void mouseEntered(MouseEvent e){ }
public void mouseExited(MouseEvent e){ }
}
怎么用JAVA做个聊天工具
先要做个登录界面, 再做聊天界面 ,服务器端, 再连数据库, 我这刚刚写过这个程序,
这只是一个登录了
import java.awt.* ;
import javax.swing.* ;
import java.awt.event.*;
import java.net.* ;
public class Login extends JFrame implements ActionListener {
JTextField t_username = new JTextField() ;
JPasswordField t_password = new JPasswordField() ;
Login() {
//设置窗体属性
this.setSize(250 , 150) ;
this.setTitle("QQ登录") ;
int width = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth() ;
int height = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight() ;
this.setLocation((width-250)/2,(height-150)/2) ;
//new一大堆组件
JLabel l_username = new JLabel("用户名") ;
JLabel l_password = new JLabel("密码") ;
JButton b_login = new JButton("登录") ;
JButton b_cancel = new JButton("取消") ;
JButton b_reg = new JButton("注册") ;
//注册事件监听
b_login.addActionListener(this) ;
b_cancel.addActionListener(this) ;
b_reg.addActionListener(this) ;
//布置输入面板
JPanel p_input = new JPanel() ;
p_input.setLayout(new GridLayout(2 ,2 )) ;
p_input.add(l_username) ;
p_input.add(t_username) ;
p_input.add(l_password) ;
p_input.add(t_password) ;
//布置按钮面板
JPanel p_button = new JPanel() ;
p_button.setLayout(new FlowLayout()) ;
p_button.add(b_login) ;
p_button.add(b_cancel) ;
p_button.add(b_reg) ;
//布置窗体
this.setLayout(new BorderLayout()) ;
this.add(p_input , BorderLayout.CENTER) ;
this.add(p_button , BorderLayout.SOUTH) ;
}
public static void main(String args[]){
Login w = new Login() ;
w.setVisible(true) ;
}
/**
* Method actionPerformed
*
*
* @param e
*
*/
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("登录")){
//将用户名和密码发送到服务器
try {
Socket s = new Socket("127.0.0.1" , 8000) ;
MyNet mn = new MyNet(s) ;
mn.sender(t_username.getText()+"%"+t_password.getText()) ;
//接收服务器发送来的确认信息
if(mn.receive().equals("ok")){
Main w = new Main(t_username.getText()) ;
w.setMyNet(mn) ;
w.setVisible(true) ;
this.setVisible(false) ;
}
}
catch (Exception ex) {
}
}
if(e.getActionCommand().equals("取消")){
System.exit(0) ;
}
if(e.getActionCommand().equals("注册")){
}
}
}
JAVA编程编写一个聊天窗口界面
刚学java的时候写过有界面,服务端和客户端在一起
一个类就可以搞定,写的有点死,你自己改下
邮箱发过来
java简单聊天界面的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于聊天的界面、java简单聊天界面的信息别忘了在本站进行查找喔。
发布于:2022-11-27,除非注明,否则均为
原创文章,转载请注明出处。