按住說話的java代碼 按住說話的java代碼
急需一個java編程實現的簡單聊天窗口代碼
import java.awt.*;
創新互聯建站基于分布式IDC數據中心構建的平臺為眾多戶提供雅安服務器托管 四川大帶寬租用 成都機柜租用 成都服務器租用。
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); //滾動窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("發送");
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("發送失敗");
}
}
}
}
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); //文本域,第一個參數為行數,第二個參數為列數
ta.setEditable(false); //文本域只讀
JScrollPane sp=new JScrollPane(ta); //滾動窗格
JTextField tf=new JTextField(20);
JButton b=new JButton("發送");
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(); //獲取文本框中的內容
if(!message.equals("")){
tf.setText(""); //清空文本框
ta.append("BB:"+message+"\n"); //添加到文本域并換行
try{
bos.write(message.getBytes());
bos.flush();
}catch(Exception ex){
System.out.println("發送失敗!");
}
}
}
}
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();
}
}
}
Myeclipse如何打開別人的.Java源代碼
點擊菜單欄File——Import——然后再點擊General文件夾下的Existing?Projects?into?Workspace
——最后點擊Browse選擇你所需要導入的項目再finish即可。。
使用java編寫代碼如下要求
NewPhone類
package?com.baidu.question;
public?class?NewPhone?extends?Phone?{
private?boolean?mute?=?true;
@Override
public?void?call()?{
if(mute){
super.call();
}else{
System.out.println("語音已關閉");
}
}
//這里是直接設置
public?void?setMute(boolean?mute){
this.mute=mute;
}
//擔心你的題目是要求兩種方法,寫的第二種,下面兩個方法負責開關
public?void?openMute(){
this.mute=true;
/*
*?也可以這樣寫
*?setMute(true);
*?下邊的方法一樣
*?*/
}
public?void?closeMute(){
this.mute?=?false;
}
}
Phone類
package?com.baidu.question;
public?class?Phone?{
public?void?call(){
System.out.println("打電話");
}
}
測試類
package?com.baidu.question;
public?class?PhoneTest?{
public?static?void?main(String[]?args)?{
Phone?phone?=?new?Phone();
phone.call();
NewPhone?newPhone?=?new?NewPhone();
newPhone.call();
newPhone.setMute(false);
newPhone.call();
newPhone.openMute();
newPhone.call();
newPhone.closeMute();
newPhone.call();
}
}
測試結果
打電話
打電話
語音已關閉
打電話
語音已關閉
java如何實現按下一個鍵和按住一個鍵?????????
new Robot() .
keyPress(int keycode)
按下給定的鍵。
keyRelease(int keycode)
釋放給定的鍵。
本文題目:按住說話的java代碼 按住說話的java代碼
本文鏈接:http://www.xueling.net.cn/article/dojegjh.html