急求2个JAVA程序[好的追加加200分]

上了一个学期的课JAVA学的不算太好现在有个任务谁有现成的150行以上的程序什么都行只要网上不多就行现在急求两个谢谢各位达人帮忙!!!2楼的再给出一个分就是你的了最好带上... 上了一个学期的课 JAVA学的不算太好 现在有个任务
谁有现成的150行以上的程序 什么都行 只要网上不多就行 现在急求两个 谢谢各位达人帮忙!!!
2楼的 再给出一个分就是你的了 最好带上解释
如果太多了的话请发到dorinmao@163.com

程序最好在150行以上
展开
 我来答
println
2007-06-18 · TA获得超过191个赞
知道小有建树答主
回答量:165
采纳率:0%
帮助的人:219万
展开全部
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class Randomtree extends JFrame {

private JTree tree;
public static String[] school = { "初中课程", "高中课程", "大学课程" };
public static String[] color = { "颜色", "运动", "食物" };
public static String[] plant = { "植物", "动物", "人" };

public static String[][] school2= {
{ "初中一年级", "初中二年级", "初中三年级"}, {"高中一年级", "高中二年级",
"高中三年级"}, {"大学一年级", "大学二年级", "大学三年级", "大学四年级"} };
public static String[][] color2 = {
{ "绿色", "白色", "红色"}, {"足球", "篮球",
"羽毛球"}, {"面包", "牛奶", "披萨", "热狗"} };
public static String[][] plant2 = {
{ "玫瑰花", "月季花", "海棠花"}, {"猪", "狗",
"猫"}, {"黄种人", "黑种人", "白种人", } };

public static void main(String[] args) {
// TODO 自动生成方法存根
new Randomtree();
}
public Randomtree() {
super();
final Random random=new Random();
setVisible(true);
setSize(300,400);
tree = new JTree();
final JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(0, 40));
getContentPane().add(panel, BorderLayout.NORTH);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setPreferredSize(new Dimension(300, 350));
getContentPane().add(scrollPane, BorderLayout.CENTER);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int k=random.nextInt(3);
tree=getTree(k);
scrollPane.setViewportView(tree);
}
});

scrollPane.setViewportView(null);
button.setText("随机生成树");
panel.add(button);
pack();

}
protected JTree getTree(int n) {
String[] second=null;
String[][] three=null;
if(n==0){second=school; three=school2;}
if(n==1){second=color; three=color2;}
if(n==2){second=plant; three=plant2;}

DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
for(int i=0;i<second.length;i++){
DefaultMutableTreeNode secondNode=new DefaultMutableTreeNode(second[i]);
for (int j=0;j<three[i].length;j++){
DefaultMutableTreeNode threetNode=new DefaultMutableTreeNode(three[i][j]);
secondNode.add(threetNode);
}
root.add(secondNode);
}
JTree tree=new JTree(root);
tree.expandRow(1);
tree.expandRow(5);
tree.expandRow(9);
return tree;
}

}

第二个
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client extends JFrame implements Runnable,ActionListener{
private JTextField txtServer = null;
private JTextField txtPort = null;
private JTextField txtUser = null;
private JTextField txtChat = null;
private JButton btnConnect = null;
private JButton btnSend = null;
private JButton btnClose = null;
private JTextArea ta = null;
private Socket client = null;
private BufferedReader in = null;
private DataOutputStream out = null;
private Thread curThread = null;

public Client() {
super("岔吧客户端");
txtServer = new JTextField(10);
txtPort = new JTextField(6);
txtUser = new JTextField(5);
txtChat = new JTextField(38);

btnConnect = new JButton("连接");
btnSend = new JButton("发送");
btnClose = new JButton("关闭");

Container cp = this.getContentPane();
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.add(new JLabel("服务器:"));
p.add(txtServer);
txtServer.setText("127.0.0.1");
p.add(new JLabel("端口:"));
p.add(txtPort);
txtPort.setText("5000");
p.add(new JLabel("用户名:"));
p.add(txtUser);
txtUser.setText("按时的");
p.add(btnConnect);
p.add(btnClose);
cp.add(p , BorderLayout.NORTH);

p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.add(new JLabel("发言:"));
p.add(txtChat);
p.add(btnSend);
cp.add(p , BorderLayout.SOUTH);

ta = new JTextArea(6 , 35);
JScrollPane sp = new JScrollPane(ta);
cp.add(sp , BorderLayout.CENTER);

btnConnect.addActionListener(this);
btnClose.addActionListener(this);
btnSend.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnConnect) {
String strServer , strPort , strUser ;
strServer = txtServer.getText().trim();
strPort = txtPort.getText().trim();
strUser = txtUser.getText().trim();

if (strServer.equals("") || strPort.equals("") || strUser.equals("")) {
return;
}
if (client != null ){
JOptionPane.showMessageDialog(this , "请先断开连接!" ,
"连接错误" , JOptionPane.PLAIN_MESSAGE);
return;
}
try {
client = new Socket(strServer , Integer.parseInt(strPort));
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new DataOutputStream(client.getOutputStream());
byte[] b=strUser.getBytes("GB2312");
out.write(b,0,b.length);
out.writeBytes(strUser + "\n");
curThread = new Thread(this);
curThread.start();
}catch(Exception ce) {
JOptionPane.showMessageDialog(this , "服务器和端口设置错误!" ,
"连接错误" , JOptionPane.PLAIN_MESSAGE);
}
}

if (e.getSource() == btnClose) {
close();
}

if (e.getSource() == btnSend) {
String strChat = txtChat.getText().trim();
txtChat.setText("");
if (strChat.equals("")) {
return;
}
if (strChat.equals("Quit")) {
close();
}else {
sendMsg(strChat);
}
}
}

public void close() {
curThread = null;
if (client != null) {
try {
sendMsg("Quit");
in.close();
out.close();
client.close();
}catch(Exception xe) {
xe.printStackTrace();
}
client = null;
}else {
JOptionPane.showMessageDialog(this , "未连接到服务器!" ,
"错误" , JOptionPane.PLAIN_MESSAGE);
}
}

public void sendMsg(String msg) {
if (out != null && client != null) {
try {
byte[] b=msg.getBytes("GB2312");
out.write(b,0,b.length);
out.writeBytes(msg + "\n");
}catch(IOException e) {
e.printStackTrace();
}
}
}

public void run() {
Thread t = Thread.currentThread();
while (t == curThread && client != null) {
try {
String str = in.readLine();
ta.append(str + "\n");
}catch(IOException e) {
e.printStackTrace();
}
}
}

public static void main(String[]args) {
Client c = new Client();
c.setSize(540,300);

c.setDefaultCloseOperation(EXIT_ON_CLOSE);
c.setVisible(true);
c.setResizable(false);
}
}

第三个

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame implements Runnable{
private JTextField txtPort = null;
private JButton btnStart = null;
private JButton btnStop = null;
private JTextArea txtLog = null;
private Thread srvThread = null;
private ServerSocket server = null;
private ThreadSocket [] clients = null;
private int LISTEN_PORT ;

class ThreadSocket extends Thread {
private Server server = null;
private Socket s = null;
public String name = null;
DataOutputStream out = null;
BufferedReader in = null;

public ThreadSocket(Server server , Socket s) {
this.server = server;
this.s = s;
}

public void run() {
while (s != null)
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new DataOutputStream(s.getOutputStream());
String str = null;
name = in.readLine();
server.addUser(name);
str = in.readLine();
while(!str.equals("Quit")) {
server.addChat(name + " : " + str);
str = in.readLine();
}
server.delUser(name);
}catch(Exception e) {
e.printStackTrace();
}
}

public void sendMsg(String msg) {
try {
byte[] b=msg.getBytes("GB2312");
out.write(b,0,b.length);
out.writeBytes("\n");
}catch(Exception e) {
e.printStackTrace();
}
}

public void close() {
try {
in.close();
s.close();
server = null;
s = null;
}catch(IOException e) {}
}
}

public Server() {
super("岔吧服务端");

txtPort = new JTextField(6);
txtPort.setText("5000");
btnStart = new JButton("启动");
btnStop = new JButton("断开");

txtLog = new JTextArea(8,35);

Container cp = this.getContentPane();

Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.add(new JLabel("端口:"));
p.add(txtPort);
p.add(btnStart);
p.add(btnStop);
cp.add(p,BorderLayout.NORTH);

p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.add(new JLabel("信息:"));
JScrollPane sp = new JScrollPane(txtLog);
txtLog.setEditable(false);
p.add(sp);
cp.add(p , BorderLayout.CENTER);

btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
start();
}
});

btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stop();
}
});
}

public void run() {
Thread cur = Thread.currentThread();
while (cur == srvThread) {
try {
Socket ts = server.accept();
int i;
for( i = 0; i < clients.length ; i++) {
if (clients[i] == null) {
clients[i] = new ThreadSocket(this , ts);
clients[i].start();
break;
}
}
if (i >= clients.length) {
ts.close();
ts = null;
}
}catch(Exception e) {
e.printStackTrace();
}
}
}

public void start() {
try {
LISTEN_PORT = Integer.parseInt(txtPort.getText());
server = new ServerSocket(LISTEN_PORT);
clients = new ThreadSocket[20];
txtLog.append("服务器监听端口:" + LISTEN_PORT + "\n");
}catch(Exception e) {
JOptionPane.showMessageDialog(this , e.toString() ,
"错误" , JOptionPane.PLAIN_MESSAGE);
return;
}
srvThread = new Thread(this);
srvThread.start();
}

public void stop() {
if (srvThread == null) {
JOptionPane.showMessageDialog(this , "服务器未启动!" ,
"错误" , JOptionPane.PLAIN_MESSAGE);
return;
}
try {
srvThread = null;
for (int i = 0 ; i < clients.length ; i++) {
if (clients[i] != null) {
clients[i].close();
clients[i] = null;
}
}
clients = null;
server.close();
server = null;
txtLog.append("服务器断开!\n");
}catch(IOException e) {
txtLog.append("服务器断开错误!\n");
e.printStackTrace();
}
}

public void addUser(String name) {
String str = name + "进来了!";

txtLog.append(str + "\n");
try {
for (int i = 0 ; i < clients.length ; i++) {

if (clients[i] != null) {
clients[i].sendMsg(str);
}
}
}catch(Exception e) {
e.printStackTrace();
}
}

public void delUser(String name) {
String str = name + "离开了";
txtLog.append(str + "\n");
for (int i = 0 ; i < clients.length ; i++) {
if (clients[i] != null) {
clients[i].sendMsg(str);
if (clients[i].name.equals(name)) {
clients[i].close();
clients[i] = null;
}
}
}
}

public void addChat(String chat) {
for (int i = 0 ; i < clients.length ; i ++) {
if (clients[i] != null) {
clients[i].sendMsg(chat);
}
}
}

public static void main(String[]args) {
Server s = new Server();
s.setSize(400,260);
s.setLocation(240,120);
s.setResizable(false);
s.setDefaultCloseOperation(EXIT_ON_CLOSE);
s.setVisible(true);
}
}
tiyuyi
2007-06-18 · 超过76用户采纳过TA的回答
知道小有建树答主
回答量:303
采纳率:0%
帮助的人:0
展开全部
....
可惜我在网吧了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式