大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
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();
}
}
}
点击菜单栏File——Import——然后再点击General文件夹下的Existing Projects into Workspace
——最后点击Browse选择你所需要导入的项目再finish即可。。
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();
}
}
测试结果
打电话
打电话
语音已关闭
打电话
语音已关闭
new Robot() .
keyPress(int keycode)
按下给定的键。
keyRelease(int keycode)
释放给定的键。