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

2024-11-08 02:41:01
推荐回答(2个)
回答1:

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方法

回答2:

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);
}

}

}

}