MyEclipse 如何用java 开发C/S程序,最好给个简单的例子 先谢谢了

 我来答
百度网友fbd6e6400
2013-04-07 · TA获得超过238个赞
知道小有建树答主
回答量:129
采纳率:0%
帮助的人:96.4万
展开全部
package com.java.MyNotepad;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.filechooser.FileNameExtensionFilter;
import com.java.find.FindString;
public class MyNotepad {
private JFrame f;
public static JTextArea ta;
private JMenuBar mb;
private JMenu fileMenu, edit, FColor, help;
private JMenuItem mOpenFile, mSave, mExit, mFind, fRed, fGreen, fBlack,
fGray, fBlue, mAbout;
private String[] fontTypeStr = { "宋体", "华文行楷", "隶书", "华文彩云", "华文新魏",
"华文琥珀", "幼圆", "楷书" };
private String fontStyleStr[] = { "常规", "倾斜", "粗体", "倾斜+粗体" };
private String fontSizeStr[] = { "13", "15", "20", "30", "40", "50", "60",
"70", "80", "100", "120", "150", "200", "300", "400", "500", "700",
"1000", "1500", "2000", "4000" };
private JComboBox fontt = new JComboBox(fontTypeStr);
private JComboBox fontStyle = new JComboBox(fontStyleStr);
private JComboBox fontSize = new JComboBox(fontSizeStr);
// 文件对话框
private JFileChooser Chooser;
private File file;
private int flag;
private BufferedReader br;
private BufferedWriter bw;
private FileNameExtensionFilter filter;
private int fileNum = 1;
private FindString fs;
public void initialized() {
f = new JFrame("MyNotepad");
// 多行文本框
ta = new JTextArea();
ta.addKeyListener(new Process());
// 滚动条
JScrollPane jsp = new JScrollPane(ta);
// 元素
fontt.addItemListener(new Process());
fontStyle.addItemListener(new Process());
fontSize.addItemListener(new Process());
mOpenFile = new JMenuItem("Open");
mOpenFile.addActionListener(new Process());
mSave = new JMenuItem("Save to...");
mSave.addActionListener(new Process());
mExit = new JMenuItem("Exit");
mExit.addActionListener(new Process());
mFind = new JMenuItem("Find");
mFind.addActionListener(new Process());
fRed = new JMenuItem("Red");
fRed.addActionListener(new Process());
fGreen = new JMenuItem("Green");
fGreen.addActionListener(new Process());
fBlack = new JMenuItem("Black");
fBlack.addActionListener(new Process());
fGray = new JMenuItem("Gray");
fGray.addActionListener(new Process());
fBlue = new JMenuItem("Blue");
fBlue.addActionListener(new Process());
mAbout = new JMenuItem("About");
mAbout.addActionListener(new Process());
// 菜单列表元素
fileMenu = new JMenu("File");
edit = new JMenu("Edit");
FColor = new JMenu("Font Color");
help = new JMenu("Help");
// 菜单
mb = new JMenuBar();
// 添加组件
fileMenu.add(mOpenFile);
fileMenu.add(mSave);
fileMenu.add(mExit);
fileMenu.setForeground(Color.WHITE);
edit.add(mFind);
edit.setForeground(Color.WHITE);
help.add(mAbout);
help.setForeground(Color.WHITE);
FColor.add(fRed);
FColor.add(fGreen);
FColor.add(fBlack);
FColor.add(fGray);
FColor.add(fBlue);
FColor.setForeground(Color.WHITE);
mb.add(fileMenu);
mb.add(edit);
mb.add(FColor);
mb.add(help);
mb.add(fontt);
mb.add(fontStyle);
mb.add(fontSize);
mb.setBackground(Color.gray);
ta.setBackground(Color.LIGHT_GRAY);
ta.setForeground(Color.black);
f.add(mb, BorderLayout.NORTH);
f.add(jsp, BorderLayout.CENTER);
f.setLocation(400, 200);
f.setSize(800, 450);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}

这是写记事本中的界面代码,功能代码没有贴上来,要不你看着感觉复杂了。。。如果要源码可以留邮箱,运行这段代码直接写个main函数实例化一个该类对象,调用initialized方法

追问
1113441497@qq.com  谢谢  只要有一个功能就行 我看网上用MyEclipse  开发C/S程序 需要下载插件?要用到哪些重要的包类?
追答
网上说的插件应该是用于快速开发的,所有的组件都是自动生成,用鼠标托放的,代码自动生成,在大多数情况下会生成一部分垃圾代码,我这里所有的组件都是用手动new出来的,在开发C/S程序时,JAVA并不需要下载其它的什么包类,在JDK中自己存在所有的组件类。好了,程序已发送。
百度网友c9c7c27
2013-04-07
知道答主
回答量:67
采纳率:0%
帮助的人:12.6万
展开全部
server.java
public class Server extends JFrame{
private JTextArea jta=new JTextArea();
public static void main(String[]args){
new Server();
}
public Server()
{
setLayout(new BorderLayout());
add(new JScrollPane(jta),BorderLayout.CENTER);

setTitle("Server");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try
{
ServerSocket serverSocket=new ServerSocket(8000);
jta.append("Server started at"+new Date()+'\n');
Socket socket=serverSocket.accept();
DataInputStream inputFromClient=new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient=new DataOutputStream(socket.getOutputStream());
while(true)
{
double radius=inputFromClient.readDouble();
double area=radius*radius*Math.PI;
outputToClient.writeDouble(area);
jta.append("Radius received from client"+radius+'\n');
jta.append("Area found:"+area+'\n');
}

}
catch(Exception e)
{
System.out.println(e);
}
}

}
client.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Client extends JFrame{
private JTextField jtf=new JTextField();
private JTextArea jta=new JTextArea();
private DataOutputStream toServer;
private DataInputStream fromServer;
public static void main(String[]args){
new Client();

}
public Client()
{
JPanel p=new JPanel();
p.setLayout(new BorderLayout());
p.add(new JLabel("Enter radius"),BorderLayout.WEST);
p.add(jtf,BorderLayout.CENTER);
jtf.setHorizontalAlignment(JTextField.RIGHT);
setLayout(new BorderLayout());
add(p,BorderLayout.NORTH);
add(new JScrollPane(jta),BorderLayout.CENTER);
jtf.addActionListener(new ButtonListener());
setTitle("Client");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try
{
Socket socket=new Socket("localhost",8000);
fromServer=new DataInputStream(socket.getInputStream());
toServer=new DataOutputStream(socket.getOutputStream());
}
catch(Exception e)
{
e.printStackTrace();
}
}
private class ButtonListener implements ActionListener
{

@Override
public void actionPerformed(ActionEvent e) {
try
{
double radius=Double.parseDouble(jtf.getText().trim());
toServer.writeDouble(radius);
toServer.flush();
double area=fromServer.readDouble();
jta.append("Radius is"+radius+"\n");
jta.append("Area received from the server is"+area+'\n');
}
catch(Exception ex)
{
System.out.println(ex);
}

}

}

}
追问
您好 我想问下 这个出来时什么效果的?我现在没法测试,希望您能给我发个截图
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式