Chinaunix首页 | 论坛 | 博客
  • 博客访问: 63556
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 60
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-15 14:57
文章分类

全部博文(21)

文章存档

2017年(3)

2016年(18)

我的朋友

分类: Java

2016-04-16 12:45:16


点击(此处)折叠或打开

  1. import com.sun.org.apache.bcel.internal.generic.BranchInstruction;

  2. import javax.swing.*;
  3. import javax.swing.border.Border;
  4. import javax.swing.event.*;
  5. import javax.swing.tree.DefaultMutableTreeNode;
  6. import javax.swing.tree.DefaultTreeModel;
  7. import java.awt.*;
  8. import java.awt.event.*;
  9. import java.beans.PropertyChangeEvent;
  10. import java.beans.PropertyVetoException;
  11. import java.beans.VetoableChangeListener;


  12. /**
  13.  * Created by zy on 16-4-13.
  14.  */
  15.  class MyGUI{
  16.     MouseListener myMouselistener=new MyMouseListener();
  17.     static JFrame jframe=new JFrame();//JFrame框
  18.     JPanel jpanel_west=new JPanel();//Left Panel
  19.     JPanel jpanel_center=new JPanel();//Right Panel


  20.     static JTextArea textArea=new JTextArea("服务器接收的数据");;
  21.     JTabbedPane jTabbedPane=new JTabbedPane(JTabbedPane.TOP);
  22.     JPanel jPanelip=new JPanel();

  23.     JPopupMenu pMenu1=new JPopupMenu(); //创建弹出式菜单,下面三项是菜单项
  24.     JMenuItem mItem1=new JMenuItem("创建一个服务器");
  25.     DefaultMutableTreeNode root=new DefaultMutableTreeNode("创建服务器");
  26.     JTree tree=new JTree(root);
  27.     MyGUI(){
  28.         this.creat_mainJframe();//创建一个主框体
  29.         this.creat_tree();
  30.         jframe.setVisible(true);
  31.     }
  32.     MyGUI(int a){

  33.     }
  34.     void creat_mainJframe(){
  35.         jframe.setBounds(100,100,600,300);
  36.         jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  37.         jframe.setTitle("服务器");
  38.         jframe.setLayout(new BorderLayout());
  39.         //jpanel_west.setBounds(0,0,100,300);
  40.         jpanel_west.setBorder(BorderFactory.createLineBorder(Color.black));
  41.         jpanel_west.setBackground(Color.LIGHT_GRAY);

  42.         jframe.add(jpanel_west,BorderLayout.WEST);
  43.         jframe.add(jpanel_center,BorderLayout.CENTER);
  44.         jframe.setVisible(true);
  45.     }

  46.     void creat_tree(){
  47.         tree.addTreeSelectionListener(new TreeSelectionListener() {
  48.             @Override
  49.             public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
  50.                 // treeSelectionEvent.
  51.                 System.out.println("choose");
  52.                 DefaultMutableTreeNode node=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
  53.                 if(node.isLeaf()){//判断是否选择了叶子节点,选择root返回false。选择叶子节点返回true
  54.                     //启动服务器
  55.                     GetIP_Port getIP_port=new GetIP_Port(node.toString());
  56.                     String ip=getIP_port.getIp();
  57.                     String port=getIP_port.getPort();
  58.                     if(ip.equals("0.0.0.0")){
  59.                         //unstart server
  60.                         System.out.println("Server have not Started");
  61.                     }
  62.                     else{
  63.                         //start server
  64.                         System.out.println("Server have Started");
  65.                  // System.out.print(Integer.parseInt(port));
  66.                         MyLocalServer localServer=new MyLocalServer(ip,Integer.parseInt(port));
  67.                         MyThread myThread=new MyThread(localServer,"MyServer");
  68.                         creat_TabbebPane(ip);
  69. // set_textArea(MyLocalServer.str);
  70. // set_textArea("");
  71.                     }


  72.                 }

  73.             }
  74.         });
  75.         tree.addMouseListener(myMouselistener);
  76.         JScrollPane treeScrollPanel=new JScrollPane();
  77.         // treeScrollPanel.add(tree);
  78.         treeScrollPanel.setViewportView(tree);
  79.         jpanel_west.add(treeScrollPanel);

  80.         mItem1.addMouseListener(myMouselistener);
  81.     }
  82.      void set_textArea(String str){
  83. // System.out.println(str);
  84.         textArea.setText(str);

  85. // jPanelip.revalidate();
  86. // jTabbedPane.revalidate();
  87. // jpanel_center.revalidate();
  88. //
  89. // jframe.revalidate();
  90.         jframe.setVisible(true);
  91.     }
  92.     void creat_TabbebPane(String ip){

  93.         jTabbedPane.setBackground(Color.LIGHT_GRAY);
  94.         jTabbedPane.setPreferredSize(new Dimension(jpanel_center.getWidth(),jpanel_center.getHeight()));
  95.         // System.out.println("get center width:"+jpanel_center.getWidth());

  96.         jPanelip.setBackground(Color.CYAN);
  97.         jPanelip.setLayout(new FlowLayout(FlowLayout.LEFT));
  98.         jPanelip.setPreferredSize(new Dimension(jTabbedPane.getWidth(),jTabbedPane.getHeight()));
  99.         // jPanelip.setBounds(new Rectangle(jpanel_center.getWidth(),jpanel_center.getHeight()));
  100.         // jPanelip.setBounds(new Rectangle(100,100));
  101. // textArea=new JTextArea("服务器接收的数据");
  102.         textArea.setEditable(true);

  103.         textArea.setLineWrap(true);
  104. // System.out.println("jjj"+jPanelip.getWidth());//为什么获取的宽度是0
  105.         textArea.setPreferredSize(new Dimension(400,100));

  106.         jPanelip.add(textArea);
  107.         jTabbedPane.addTab(ip,jPanelip);
  108.         //jTabbedPane.addTab("kanwo",new JPanel());

  109.         jpanel_center.add(jTabbedPane);
  110.         jpanel_center.setLayout(new FlowLayout(FlowLayout.LEFT));
  111.         jframe.setVisible(true);

  112. // System.out.println("entry into tabbedpanel");

  113.     }
  114.     class MyMouseListener implements MouseListener{
  115.         JFrame jframe_Small;
  116.         JTextField textField_top;
  117.         JTextField jtextField_bottom;
  118.         @Override
  119.         public void mouseClicked(MouseEvent mouseEvent) {
  120.             /*****************左击事件****************/
  121.             if(mouseEvent.getClickCount()==2){
  122.                 System.out.println("you click twice");

  123.             }
  124.             if(mouseEvent.getButton()==MouseEvent.BUTTON1){
  125.                 System.out.println("1");

  126.             }
  127.             /*****************中间滚轮点击事件****************/
  128.             else if(mouseEvent.getButton()==MouseEvent.BUTTON2){
  129.                 System.out.println("2");
  130.             }
  131.             /*****************右击事件****************/
  132.             else if(mouseEvent.getButton()==MouseEvent.BUTTON3){
  133.                 System.out.println("3");
  134.                 pMenu1.add(mItem1);
  135.                 pMenu1.show(mouseEvent.getComponent(),mouseEvent.getX(),mouseEvent.getY());
  136.             }

  137.         }

  138.         @Override
  139.         public void mousePressed(MouseEvent mouseEvent) {
  140.      // System.out.println(mouseEvent.getSource().getClass().toString());

  141.             if(mouseEvent.getSource().getClass().toString().equals("class javax.swing.JMenuItem")){
  142.                 System.out.println("进入");
  143.                 if(mouseEvent.getButton()==MouseEvent.BUTTON1){
  144.                     jframe_Small=new JFrame("服务器信息");
  145.                     jframe_Small.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  146.                     jframe_Small.setBounds(300,200,200,100);
  147.                     JLabel label_ip=new JLabel("IP地址:");
  148.                     textField_top=new JTextField("202.206.212.233");
  149.                     //textField_top.setLayout(new L);
  150.                     JLabel label_port=new JLabel("Port:");
  151.                     jtextField_bottom=new JTextField("60000");
  152.                     jtextField_bottom.addKeyListener(new KeyListener() {
  153.                         @Override
  154.                         public void keyTyped(KeyEvent keyEvent) {

  155.                         }

  156.                         @Override
  157.                         public void keyPressed(KeyEvent keyEvent) {
  158.                             if(keyEvent.getKeyCode()==KeyEvent.VK_ENTER){
  159.                                 System.out.println("enter");
  160.                                 DefaultTreeModel model= (DefaultTreeModel) tree.getModel();
  161.                                 String str=textField_top.getText()+":"+jtextField_bottom.getText();
  162.                                 DefaultMutableTreeNode child=new DefaultMutableTreeNode(str);
  163.                                 DefaultMutableTreeNode chosen= (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
  164.                                 if(chosen==null){
  165.                                     chosen=root;
  166.                                     System.out.println("root is null");
  167.                                 }
  168.                                 model.insertNodeInto(child,chosen,0);
  169. // tree.updateUI();
  170.                                 jframe.setVisible(true);
  171.                                 jframe_Small.setVisible(false);
  172.                             }
  173.                         }

  174.                         @Override
  175.                         public void keyReleased(KeyEvent keyEvent) {

  176.                         }
  177.                     });

  178.                     Box box_top=Box.createHorizontalBox();
  179.                     Box box_bottom=Box.createHorizontalBox();
  180.                     box_top.add(label_ip);
  181.                     box_top.add(Box.createHorizontalStrut(30));
  182. // box_top.add(Box.createVerticalGlue());
  183.                     box_top.add(textField_top);
  184.                     box_bottom.add(label_port);
  185.                     box_bottom.add(Box.createHorizontalStrut(40));
  186.                     box_bottom.add(jtextField_bottom);

  187.                     Box box=Box.createVerticalBox();
  188.                     box.add(Box.createVerticalStrut(20));
  189. // box.add(Box.createVerticalGlue());
  190.                     box.add(box_top);
  191.                     box.add(Box.createVerticalStrut(20));
  192. // box.add(Box.createVerticalGlue());
  193.                     box.add(box_bottom);
  194.                     box.add(Box.createVerticalStrut(20));
  195. // box.add(Box.createVerticalGlue());
  196.                     jframe_Small.add(box);
  197.                     jframe_Small.setVisible(true);

  198.                 }
  199.             }

  200. // System.out.println("click2222");

  201.         }

  202.         @Override
  203.         public void mouseReleased(MouseEvent mouseEvent) {
  204.       // System.out.println("unknow");

  205.         }

  206.         @Override
  207.         public void mouseEntered(MouseEvent mouseEvent) {
  208.       // System.out.println("mouseon");

  209.         }

  210.         @Override
  211.         public void mouseExited(MouseEvent mouseEvent) {
  212.        // System.out.println("mouseleave");

  213.         }
  214.     }
  215. }



  216. public class Myserver{
  217.     public static void main(String args[]){

  218.         MyGUI gui=new MyGUI();

  219.     }
  220. }


点击(此处)折叠或打开

  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.io.PrintStream;
  7. import java.net.InetAddress;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;

  10. /**
  11.  * Created by zy on 16-4-15.
  12.  */
  13.  class MyLocalServer {
  14.     ServerSocket myserver;
  15.     String ip;
  16.     int port;
  17.     MyGUI myGUI=new MyGUI(1);
  18.     String str="str parameter inition";
  19.     MyLocalServer(String ip,int port){
  20.         this.ip=ip;
  21.         this.port=port;
  22.         try {
  23.             myserver= new ServerSocket(this.port,1, InetAddress.getByName(this.ip));
  24.         } catch (IOException e) {
  25.             e.printStackTrace();
  26.         }
  27.         System.out.println("Myserver start");
  28.     }
  29.      public void runServer() {
  30.         try {
  31.             for (; ; ) {
  32.                 Socket socket = myserver.accept();
  33. // myGUI.creat_TabbebPane(ip);
  34.                 System.out.println("Received request from " + socket.getInetAddress() + ":" + socket.getPort());
  35.                 OutputStream out = socket.getOutputStream();
  36.                 PrintStream pout = new PrintStream(out);
  37.                 pout.print(new java.util.Date());
  38.                 out.flush();
  39.                 // out.close();

  40.                 int c;
  41.                 InputStream timeStream = socket.getInputStream();
  42.                 StringBuffer time = new StringBuffer();
  43. // while ((c = timeStream.read()) != -1) {
  44. // time.append((char) c);
  45. // }
  46.                 String timeString = time.toString();
  47.                 str="It is " + timeString + " at " + InetAddress.getByName(ip);
  48.                 System.out.println("MyLocalSocket's "+str);
  49.                 myGUI.set_textArea(str);
  50.              // MyGUI.set_textArea(str);
  51.                 socket.close();
  52.             }
  53.         } catch (IOException e) {
  54.             e.printStackTrace();
  55.         }
  56.     }
  57. }
  58. class MyThread implements Runnable{
  59.     MyLocalServer myLocalServer;
  60.     Thread thread;
  61.     MyThread(MyLocalServer mylocalServer,String name){
  62.         this.myLocalServer=mylocalServer;
  63.         thread=new Thread(this,name);
  64.         thread.start();
  65.     }

  66.     @Override
  67.     public void run() {
  68.         myLocalServer.runServer();
  69.     }
  70. }

注意上面黄色标注:注意static的使用,否则不成功
阅读(2708) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~