-
import com.sun.org.apache.bcel.internal.generic.BranchInstruction;
-
-
import javax.swing.*;
-
import javax.swing.border.Border;
-
import javax.swing.event.*;
-
import javax.swing.tree.DefaultMutableTreeNode;
-
import javax.swing.tree.DefaultTreeModel;
-
import java.awt.*;
-
import java.awt.event.*;
-
import java.beans.PropertyChangeEvent;
-
import java.beans.PropertyVetoException;
-
import java.beans.VetoableChangeListener;
-
-
-
/**
-
* Created by zy on 16-4-13.
-
*/
-
class MyGUI{
-
MouseListener myMouselistener=new MyMouseListener();
-
static JFrame jframe=new JFrame();//JFrame框
-
JPanel jpanel_west=new JPanel();//Left Panel
-
JPanel jpanel_center=new JPanel();//Right Panel
-
-
-
static JTextArea textArea=new JTextArea("服务器接收的数据");;
-
JTabbedPane jTabbedPane=new JTabbedPane(JTabbedPane.TOP);
-
JPanel jPanelip=new JPanel();
-
-
JPopupMenu pMenu1=new JPopupMenu(); //创建弹出式菜单,下面三项是菜单项
-
JMenuItem mItem1=new JMenuItem("创建一个服务器");
-
DefaultMutableTreeNode root=new DefaultMutableTreeNode("创建服务器");
-
JTree tree=new JTree(root);
-
MyGUI(){
-
this.creat_mainJframe();//创建一个主框体
-
this.creat_tree();
-
jframe.setVisible(true);
-
}
-
MyGUI(int a){
-
-
}
-
void creat_mainJframe(){
-
jframe.setBounds(100,100,600,300);
-
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
jframe.setTitle("服务器");
-
jframe.setLayout(new BorderLayout());
-
//jpanel_west.setBounds(0,0,100,300);
-
jpanel_west.setBorder(BorderFactory.createLineBorder(Color.black));
-
jpanel_west.setBackground(Color.LIGHT_GRAY);
-
-
jframe.add(jpanel_west,BorderLayout.WEST);
-
jframe.add(jpanel_center,BorderLayout.CENTER);
-
jframe.setVisible(true);
-
}
-
-
void creat_tree(){
-
tree.addTreeSelectionListener(new TreeSelectionListener() {
-
@Override
-
public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
-
// treeSelectionEvent.
-
System.out.println("choose");
-
DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
-
if(node.isLeaf()){//判断是否选择了叶子节点,选择root返回false。选择叶子节点返回true
-
//启动服务器
-
GetIP_Port getIP_port=new GetIP_Port(node.toString());
-
String ip=getIP_port.getIp();
-
String port=getIP_port.getPort();
-
if(ip.equals("0.0.0.0")){
-
//unstart server
-
System.out.println("Server have not Started");
-
}
-
else{
-
//start server
-
System.out.println("Server have Started");
-
// System.out.print(Integer.parseInt(port));
-
MyLocalServer localServer=new MyLocalServer(ip,Integer.parseInt(port));
-
MyThread myThread=new MyThread(localServer,"MyServer");
-
creat_TabbebPane(ip);
-
// set_textArea(MyLocalServer.str);
-
// set_textArea("");
-
}
-
-
-
}
-
-
}
-
});
-
tree.addMouseListener(myMouselistener);
-
JScrollPane treeScrollPanel=new JScrollPane();
-
// treeScrollPanel.add(tree);
-
treeScrollPanel.setViewportView(tree);
-
jpanel_west.add(treeScrollPanel);
-
-
mItem1.addMouseListener(myMouselistener);
-
}
-
void set_textArea(String str){
-
// System.out.println(str);
-
textArea.setText(str);
-
-
// jPanelip.revalidate();
-
// jTabbedPane.revalidate();
-
// jpanel_center.revalidate();
-
//
-
// jframe.revalidate();
-
jframe.setVisible(true);
-
}
-
void creat_TabbebPane(String ip){
-
-
jTabbedPane.setBackground(Color.LIGHT_GRAY);
-
jTabbedPane.setPreferredSize(new Dimension(jpanel_center.getWidth(),jpanel_center.getHeight()));
-
// System.out.println("get center width:"+jpanel_center.getWidth());
-
-
jPanelip.setBackground(Color.CYAN);
-
jPanelip.setLayout(new FlowLayout(FlowLayout.LEFT));
-
jPanelip.setPreferredSize(new Dimension(jTabbedPane.getWidth(),jTabbedPane.getHeight()));
-
// jPanelip.setBounds(new Rectangle(jpanel_center.getWidth(),jpanel_center.getHeight()));
-
// jPanelip.setBounds(new Rectangle(100,100));
-
// textArea=new JTextArea("服务器接收的数据");
-
textArea.setEditable(true);
-
-
textArea.setLineWrap(true);
-
// System.out.println("jjj"+jPanelip.getWidth());//为什么获取的宽度是0
-
textArea.setPreferredSize(new Dimension(400,100));
-
-
jPanelip.add(textArea);
-
jTabbedPane.addTab(ip,jPanelip);
-
//jTabbedPane.addTab("kanwo",new JPanel());
-
-
jpanel_center.add(jTabbedPane);
-
jpanel_center.setLayout(new FlowLayout(FlowLayout.LEFT));
-
jframe.setVisible(true);
-
-
// System.out.println("entry into tabbedpanel");
-
-
}
-
class MyMouseListener implements MouseListener{
-
JFrame jframe_Small;
-
JTextField textField_top;
-
JTextField jtextField_bottom;
-
@Override
-
public void mouseClicked(MouseEvent mouseEvent) {
-
/*****************左击事件****************/
-
if(mouseEvent.getClickCount()==2){
-
System.out.println("you click twice");
-
-
}
-
if(mouseEvent.getButton()==MouseEvent.BUTTON1){
-
System.out.println("1");
-
-
}
-
/*****************中间滚轮点击事件****************/
-
else if(mouseEvent.getButton()==MouseEvent.BUTTON2){
-
System.out.println("2");
-
}
-
/*****************右击事件****************/
-
else if(mouseEvent.getButton()==MouseEvent.BUTTON3){
-
System.out.println("3");
-
pMenu1.add(mItem1);
-
pMenu1.show(mouseEvent.getComponent(),mouseEvent.getX(),mouseEvent.getY());
-
}
-
-
}
-
-
@Override
-
public void mousePressed(MouseEvent mouseEvent) {
-
// System.out.println(mouseEvent.getSource().getClass().toString());
-
-
if(mouseEvent.getSource().getClass().toString().equals("class javax.swing.JMenuItem")){
-
System.out.println("进入");
-
if(mouseEvent.getButton()==MouseEvent.BUTTON1){
-
jframe_Small=new JFrame("服务器信息");
-
jframe_Small.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-
jframe_Small.setBounds(300,200,200,100);
-
JLabel label_ip=new JLabel("IP地址:");
-
textField_top=new JTextField("202.206.212.233");
-
//textField_top.setLayout(new L);
-
JLabel label_port=new JLabel("Port:");
-
jtextField_bottom=new JTextField("60000");
-
jtextField_bottom.addKeyListener(new KeyListener() {
-
@Override
-
public void keyTyped(KeyEvent keyEvent) {
-
-
}
-
-
@Override
-
public void keyPressed(KeyEvent keyEvent) {
-
if(keyEvent.getKeyCode()==KeyEvent.VK_ENTER){
-
System.out.println("enter");
-
DefaultTreeModel model= (DefaultTreeModel) tree.getModel();
-
String str=textField_top.getText()+":"+jtextField_bottom.getText();
-
DefaultMutableTreeNode child=new DefaultMutableTreeNode(str);
-
DefaultMutableTreeNode chosen= (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
-
if(chosen==null){
-
chosen=root;
-
System.out.println("root is null");
-
}
-
model.insertNodeInto(child,chosen,0);
-
// tree.updateUI();
-
jframe.setVisible(true);
-
jframe_Small.setVisible(false);
-
}
-
}
-
-
@Override
-
public void keyReleased(KeyEvent keyEvent) {
-
-
}
-
});
-
-
Box box_top=Box.createHorizontalBox();
-
Box box_bottom=Box.createHorizontalBox();
-
box_top.add(label_ip);
-
box_top.add(Box.createHorizontalStrut(30));
-
// box_top.add(Box.createVerticalGlue());
-
box_top.add(textField_top);
-
box_bottom.add(label_port);
-
box_bottom.add(Box.createHorizontalStrut(40));
-
box_bottom.add(jtextField_bottom);
-
-
Box box=Box.createVerticalBox();
-
box.add(Box.createVerticalStrut(20));
-
// box.add(Box.createVerticalGlue());
-
box.add(box_top);
-
box.add(Box.createVerticalStrut(20));
-
// box.add(Box.createVerticalGlue());
-
box.add(box_bottom);
-
box.add(Box.createVerticalStrut(20));
-
// box.add(Box.createVerticalGlue());
-
jframe_Small.add(box);
-
jframe_Small.setVisible(true);
-
-
}
-
}
-
-
// System.out.println("click2222");
-
-
}
-
-
@Override
-
public void mouseReleased(MouseEvent mouseEvent) {
-
// System.out.println("unknow");
-
-
}
-
-
@Override
-
public void mouseEntered(MouseEvent mouseEvent) {
-
// System.out.println("mouseon");
-
-
}
-
-
@Override
-
public void mouseExited(MouseEvent mouseEvent) {
-
// System.out.println("mouseleave");
-
-
}
-
}
-
}
-
-
-
-
public class Myserver{
-
public static void main(String args[]){
-
-
MyGUI gui=new MyGUI();
-
-
}
-
}
-
import javax.swing.*;
-
import java.awt.*;
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.io.OutputStream;
-
import java.io.PrintStream;
-
import java.net.InetAddress;
-
import java.net.ServerSocket;
-
import java.net.Socket;
-
-
/**
-
* Created by zy on 16-4-15.
-
*/
-
class MyLocalServer {
-
ServerSocket myserver;
-
String ip;
-
int port;
-
MyGUI myGUI=new MyGUI(1);
-
String str="str parameter inition";
-
MyLocalServer(String ip,int port){
-
this.ip=ip;
-
this.port=port;
-
try {
-
myserver= new ServerSocket(this.port,1, InetAddress.getByName(this.ip));
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
System.out.println("Myserver start");
-
}
-
public void runServer() {
-
try {
-
for (; ; ) {
-
Socket socket = myserver.accept();
-
// myGUI.creat_TabbebPane(ip);
-
System.out.println("Received request from " + socket.getInetAddress() + ":" + socket.getPort());
-
OutputStream out = socket.getOutputStream();
-
PrintStream pout = new PrintStream(out);
-
pout.print(new java.util.Date());
-
out.flush();
-
// out.close();
-
-
int c;
-
InputStream timeStream = socket.getInputStream();
-
StringBuffer time = new StringBuffer();
-
// while ((c = timeStream.read()) != -1) {
-
// time.append((char) c);
-
// }
-
String timeString = time.toString();
-
str="It is " + timeString + " at " + InetAddress.getByName(ip);
-
System.out.println("MyLocalSocket's "+str);
-
myGUI.set_textArea(str);
-
// MyGUI.set_textArea(str);
-
socket.close();
-
}
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
class MyThread implements Runnable{
-
MyLocalServer myLocalServer;
-
Thread thread;
-
MyThread(MyLocalServer mylocalServer,String name){
-
this.myLocalServer=mylocalServer;
-
thread=new Thread(this,name);
-
thread.start();
-
}
-
-
@Override
-
public void run() {
-
myLocalServer.runServer();
-
}
-
}
注意上面黄色标注:注意static的使用,否则不成功
阅读(2708) | 评论(0) | 转发(0) |