框架和鼠标侦听器 [英] frame and mouse listener

查看:77
本文介绍了框架和鼠标侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在获取客户端图像,鼠标操作侦听器命令不起作用
请解决我这个问题

我对此完全陌生,实际上我无法遵循此代码
在这里,他们使用了线程,我不知道它是如何工作的.
开始();此功能是做什么的..



在这里,我已经摇了一个框架并添加了tabbedpanel
在srever窗格中-具有一个文本字段以获取端口号和一个按钮连接客户端"

如果按下该按钮,称为
新ServerInitiator()

now i am getting images of client,mouse action listener command not working
please solve me this problem

i am totally new to this really i am not able to follow this code
here they have used thread i dont know how does it work.
start(); what does this function do..



here i have craeted one frame and add tabbedpanel
in srever pane--having one text field to get the port no and one button "connect client"

if that button is pressed new ServerInitiator() called

if (action!=null && action.equals("Connect Client")){

                        String port = lMainPanelObj.lListiningPortField.getText();
                        System.out.println("heeee System Connected"+port);
                        new ServerInitiator().initialize(Integer.parseInt(port));
                        System.out.println("heeee System77777777777777777777 Connected");
                  }



在这种情况下,我在框架中面临问题,



In this i am facing problem in frames ,

public class ServerInitiator {
      //Main server frame
      public JFrame frame = new JFrame();
      //JDesktopPane represents the main container that will contain all
      //connected clients'' screens
      private JDesktopPane desktop = new JDesktopPane();

      //public static void main(String args[]){
      //   String port = JOptionPane.showInputDialog("Please enter listening port");
      //   new ServerInitiator().initialize(Integer.parseInt(port));
   // }

      public void initialize(int port){
                  System.out.println("333333333333333333333"+port);
            try {
                  ServerSocket sc = new ServerSocket(port);
                  //Show Server GUI
                  drawGUI();
                  //Listen to server port and accept clients connections
                  while(true){
                        System.out.println("333333333333333333333"+port);
                        Socket client = sc.accept();
                        System.out.println("New client Connected to the server");
                        //Per each client create a ClientHandler
                        new ClientHandler(client,desktop);
                  }
            } catch (IOException ex) {
                  ex.printStackTrace();
            }
      }

      /*
      * Draws the main server GUI
      */
      public void drawGUI(){
                  frame.add(desktop,BorderLayout.CENTER);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setSize(1024,800);
                  frame.setFocusable(true);
                  //Show the frame in a maximized state
                  //frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
                  frame.setVisible(true);
      }
}



在这里它正在调用< b> new ClientHandler(client,desktop);</b>



here it is calling <b>new ClientHandler(client,desktop);</b>

class ClientHandler extends Thread {

      private JDesktopPane desktop = null;
      private Socket cSocket = null;
      private JInternalFrame interFrame = new JInternalFrame("Client Screen",
                                                                                          true, true, true);
      private JPanel cPanel = new JPanel();
     
      public ClientHandler(Socket cSocket, JDesktopPane desktop) {
            System.out.println("Hello ram i am here");
            this.cSocket = cSocket;
            this.desktop = desktop;
            start();
      }

      /*
      * Draw GUI per each connected client
      */
      public void drawGUI(){
            interFrame.setLayout(new BorderLayout());
            interFrame.getContentPane().add(cPanel,BorderLayout.CENTER);
            interFrame.setSize(1024,100);
            desktop.add(interFrame);
         try {
                  //Initially show the internal frame maximized
                  interFrame.setMaximum(true);
            } catch (PropertyVetoException ex) {
                  ex.printStackTrace();
            }
            //this allows to handle KeyListener events

            cPanel.setFocusable(true);
            interFrame.setVisible(true);
      }

      public void run(){

            //used to represent client screen size
            Rectangle clientScreenDim = null;
            //Used to read screenshots and client screen dimension
            ObjectInputStream ois = null;
            //start drawing GUI
            drawGUI();

            try{
                  //Read client screen dimension
                  ois = new ObjectInputStream(cSocket.getInputStream());
                  clientScreenDim =(Rectangle) ois.readObject();
            }catch(IOException ex){
                  ex.printStackTrace();
            }catch(ClassNotFoundException ex){
                  ex.printStackTrace();
            }
            //Start recieveing screenshots
            new ClientScreenReciever(ois,cPanel);
            System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
            //Start sending events to the client
            new ClientCommandsSender(cSocket,cPanel,clientScreenDim);
      }

}



在这里,它正在调用< b> new



here it is calling <b>new

ClientScreenReciever(ois,cPanel);</b>
                                 new <b>ClientCommandsSender(cSocket,cPanel,clientScreenDim);</b>


我在
遇到问题 < b> ClientCommandsSender(cSocket,cPanel,clientScreenDim)</b>

这里面临问题


i am facing problem in
<b>ClientCommandsSender(cSocket,cPanel,clientScreenDim)</b>

here am facing problem

KeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);

public class ClientCommandsSender implements KeyListener,
            MouseMotionListener,MouseListener {

      private Socket cSocket = null;
      private JPanel cPanel = null;
      private PrintWriter writer = null;
      private Rectangle clientScreenDim = null;

      ClientCommandsSender(Socket s, JPanel p, Rectangle r) {
            cSocket = s;
            cPanel = p;
            clientScreenDim = r;

            //Associate event listners to the panel
            cPanel.addKeyListener(this);
            cPanel.addMouseListener(this);
            cPanel.addMouseMotionListener(this);
            try {
                  //Prepare PrintWriter which will be used to send commands to
                  //the client
                  System.out.println("fffffffffffffffffffffffffffffff"+this);
                  writer = new PrintWriter(cSocket.getOutputStream());
            } catch (IOException ex) {
                  ex.printStackTrace();
            }
           
      }

      //Not implemeted yet
      public void mouseDragged(MouseEvent e) {
      }

      public void mouseMoved(MouseEvent e) {

            double xScale = clientScreenDim.getWidth()/cPanel.getWidth();
            System.out.println("xScale: " + xScale);
            double yScale = clientScreenDim.getHeight()/cPanel.getHeight();
            System.out.println("yScale: " + yScale);
            System.out.println("Mouse Moved");
            writer.println(EnumCommands1.MOVE_MOUSE.getAbbrev1());
            writer.println((int)(e.getX() * xScale));
            writer.println((int)(e.getY() * yScale));
            writer.flush();
      }

      //this is not implemented
      public void mouseClicked(MouseEvent e) {
      }

      public void mousePressed(MouseEvent e) {
            System.out.println("Mouse Pressed");
            writer.println(EnumCommands1.PRESS_MOUSE.getAbbrev1());
            int button = e.getButton();
            int xButton = 16;
            if (button == 3) {
                  xButton = 4;
            }
            writer.println(xButton);
            writer.flush();
      }

      public void mouseReleased(MouseEvent e) {
            System.out.println("Mouse Released");
            writer.println(EnumCommands1.RELEASE_MOUSE.getAbbrev1());
            int button = e.getButton();
            int xButton = 16;
            if (button == 3) {
                  xButton = 4;
            }
            writer.println(xButton);
            writer.flush();
      }

      //not implemented
      public void mouseEntered(MouseEvent e) {
      }

      //not implemented
      public void mouseExited(MouseEvent e) {

      }

      //not implemented
      public void keyTyped(KeyEvent e) {
      }

      public void keyPressed(KeyEvent e) {
            System.out.println("Key Pressed");
            writer.println(EnumCommands1.PRESS_KEY.getAbbrev1());
            writer.println(e.getKeyCode());
            writer.flush();
      }

      public void keyReleased(KeyEvent e) {
            System.out.println("Mouse Released");
            writer.println(EnumCommands1.RELEASE_KEY.getAbbrev1());
            writer.println(e.getKeyCode());
            writer.flush();
      }

}

推荐答案

rampraveen写道:
rampraveen wrote:

鼠标操作侦听器命令不起作用

mouse action listener command not working



这是什么意思?我们无法猜测您的代码在做什么或操作的结果是什么.请详细说明,并附上您认为不起作用的代码段.



What does this mean? We cannot guess what your code is doing or what the results of your actions are. Please explain in detail, and include snippets of the code that you think is not working.


rampraveen写道:
rampraveen wrote:

我对此完全陌生,实际上我无法遵循此代码

i am totally new to this really i am not able to follow this code



那么在这种情况下,我建议您回到基础知识(请参阅 Java教程 [



Well in that case I suggest you go back to basics (see the Java Tutorials[^]) and spend some time studying the language and its classes. There really is no point in continuing this unless you are at least able to understand what the code does. I am afraid that people here do not have the time to take this project and try to figure out what it does.


这篇关于框架和鼠标侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆