欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

聊天框java代碼 java文本框代碼

關(guān)于仿QQ聊天對(duì)話框的JAVA代碼

1、swing的界面可以直接用netbeans畫出來嘛。

成都創(chuàng)新互聯(lián)專注于做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站開發(fā)。公司秉持“客戶至上,用心服務(wù)”的宗旨,從客戶的利益和觀點(diǎn)出發(fā),讓客戶在網(wǎng)絡(luò)營(yíng)銷中找到自己的駐足之地。尊重和關(guān)懷每一位客戶,用嚴(yán)謹(jǐn)?shù)膽B(tài)度對(duì)待客戶,用專業(yè)的服務(wù)創(chuàng)造價(jià)值,成為客戶值得信賴的朋友,為客戶解除后顧之憂。

2、可以把輸出的聊天內(nèi)容都放在一個(gè)StringBuffer里,每打出一句話,就把這句話追加在StringBuffer,然后把StringBuffer里的內(nèi)容輸出到Textarea中。

3、好友列表可以用JList

急需一個(gè)java編程實(shí)現(xiàn)的簡(jiǎn)單聊天窗口代碼

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ClientDemo01 {

public static void main(String[] args){

JFrame f=new JFrame("AA");

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JTextArea ta=new JTextArea(15,30);

ta.setEditable(false); //文本域只讀

JScrollPane sp=new JScrollPane(ta); //滾動(dòng)窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("發(fā)送");

p1.add(sp);

p2.add(tf);

p2.add(b);

f.add(p1,"Center");

f.add(p2,"South");

f.setBounds(300,300,360,300);

f.setVisible(true);

f.setResizable(false);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Socket socket=null;

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try{

socket=new Socket("192.168.0.4",5000);

bis=new BufferedInputStream(socket.getInputStream());

bos=new BufferedOutputStream(socket.getOutputStream());

MyThread01 mt=new MyThread01(bis,ta);

mt.start();

}catch(Exception e){

e.printStackTrace();

}

b.addActionListener(new ButtonActionListener01(tf,ta,bos));

}

}

class ButtonActionListener01 implements ActionListener{

JTextField tf;

JTextArea ta;

BufferedOutputStream bos;

public ButtonActionListener01(JTextField tf,JTextArea ta,BufferedOutputStream bos){

this.tf=tf;

this.ta=ta;

this.bos=bos;

}

public void actionPerformed(ActionEvent e){

String message=tf.getText();

if(!message.equals("")){

tf.setText(""); //清空文本框

ta.append("AA:"+message+"\n"); //添加到文本域并換行

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("發(fā)送失敗");

}

}

}

}

class MyThread01 extends Thread{

BufferedInputStream bis;

JTextArea ta;

public MyThread01(BufferedInputStream bis,JTextArea ta){

this.bis=bis;

this.ta=ta;

}

public void run(){

try{

while(true){

byte[] b=new byte[100];

int length=bis.read(b);

String message=new String(b,0,length);

ta.append("BB:"+message+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

} import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.net.*;

import java.io.*;

public class ServerDemo01{

public static void main(String[] args){

JFrame f=new JFrame("BB");

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JTextArea ta=new JTextArea(12,30); //文本域,第一個(gè)參數(shù)為行數(shù),第二個(gè)參數(shù)為列數(shù)

ta.setEditable(false); //文本域只讀

JScrollPane sp=new JScrollPane(ta); //滾動(dòng)窗格

JTextField tf=new JTextField(20);

JButton b=new JButton("發(fā)送");

p1.add(sp);

p2.add(tf);

p2.add(b);

f.add(p1,"Center");

f.add(p2,"South");

f.setBounds(300,300,360,300);

f.setVisible(true);

f.setResizable(false);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ServerSocket server=null;

Socket socket=null;

BufferedInputStream bis=null;

BufferedOutputStream bos=null;

try{

server=new ServerSocket(5000);

//ta.append("等待AA連接...\n");

socket=server.accept();

//ta.append("AA已連接\n");

bis=new BufferedInputStream(socket.getInputStream());

bos=new BufferedOutputStream(socket.getOutputStream());

MyThread1 mt=new MyThread1(bis,ta);

mt.start();

}catch(Exception e){

e.printStackTrace();

}

b.addActionListener(new ButtonActionListener1(tf,ta,bos));

}

}

class ButtonActionListener1 implements ActionListener{

JTextField tf;

JTextArea ta;

BufferedOutputStream bos;

public ButtonActionListener1(JTextField tf,JTextArea ta,BufferedOutputStream bos){

this.tf=tf;

this.ta=ta;

this.bos=bos;

}

public void actionPerformed(ActionEvent e){

String message=tf.getText(); //獲取文本框中的內(nèi)容

if(!message.equals("")){

tf.setText(""); //清空文本框

ta.append("BB:"+message+"\n"); //添加到文本域并換行

try{

bos.write(message.getBytes());

bos.flush();

}catch(Exception ex){

System.out.println("發(fā)送失敗!");

}

}

}

}

class MyThread1 extends Thread{

BufferedInputStream bis;

JTextArea ta;

public MyThread1(BufferedInputStream bis,JTextArea ta){

this.bis=bis;

this.ta=ta;

}

public void run(){

try{

while(true){

byte[] b=new byte[100];

int length=bis.read(b);

String message=new String(b,0,length);

ta.append("AA:"+message+"\n");

}

}catch(Exception e){

e.printStackTrace();

}

}

}

java怎么創(chuàng)建消息對(duì)話框

復(fù)雜的對(duì)話消息框可以參考JDialog

說明: JDialog的寫法和JFrame基本類似. 可以自由添加組件等,代碼量偏多.

簡(jiǎn)單的消息對(duì)話框可以使用JOptionPane

說明: 功能較少, 可拓展性不強(qiáng),但是代碼非常簡(jiǎn)潔. 適合大多數(shù)的應(yīng)用場(chǎng)景.

效果圖

舉例:

public?class?Demo?{

public?static?void?main(String[]?args)?{

JOptionPane.showMessageDialog(null,?"提示:今天天氣不錯(cuò)喲~");??

JOptionPane.showMessageDialog(null,?"提示:?6/0出錯(cuò),?被除數(shù)不能為0!?",?"警告",JOptionPane.ERROR_MESSAGE);??

}

}

關(guān)于觸發(fā)的舉例

效果圖

參考代碼

import?java.awt.*;

import?java.awt.event.*;

import?javax.swing.*;

//該窗口繼承自JFrame.?

public?class?DemoFrame?extends?JFrame?implements?ActionListener{

JTextField?jtf;

JButton?jb;

public?DemoFrame()?{

jtf?=?new?JTextField(8);

jtf.setText("Hello?~");

jb?=?new?JButton("顯示文本框的內(nèi)容");

jb.addActionListener(this);

JPanel?jp?=?new?JPanel();

jp.add(jtf);

jp.add(jb);

add(jp);

setTitle("窗口");//?窗口標(biāo)題

setSize(380,?185);//?窗口大小

setLocationRelativeTo(null);//?窗口居中

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//?通常添加這行代碼,點(diǎn)擊窗口右下角的關(guān)閉時(shí)會(huì)結(jié)束程序

setVisible(true);

}

//?main方法

public?static?void?main(String[]?args)?{

new?DemoFrame();

}

@Override

public?void?actionPerformed(ActionEvent?e)?{

JButton?jb1?=?(JButton)?e.getSource();

if(jb==jb1)?{

JOptionPane.showMessageDialog(null,?"文本框的內(nèi)容是:"+jtf.getText());

}

}

}

拓展:

更多的關(guān)于JDialog和JOptionPane兩個(gè)組件的使用方法, 可以查看java API文檔

建議經(jīng)常查看java的 API文檔, 網(wǎng)上有很多的中文版. 不熟悉的類和方法,就看看, 是學(xué)習(xí)的利器~

標(biāo)題名稱:聊天框java代碼 java文本框代碼
文章分享:http://www.chinadenli.net/article12/doogpdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)ChatGPT動(dòng)態(tài)網(wǎng)站響應(yīng)式網(wǎng)站App開發(fā)微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化