0

我在动态更改 JLabel 中的图标时遇到了一些问题。首先,我要做的基本上是模拟屏幕共享操作。在客户端,我每秒都在截屏,然后将它们发送到服务器。在服务器端,我试图在一个简单的 GUI 中打开这些图片。

我可以毫无问题地发送图片,也可以毫无问题地获取它们。但是,我编写的 GUI 代码无法打开图片。更具体地说,如果有图片,它可以打开它,但它不会打开另一个已经来的图片。

我在服务器端所做的是,只要图片到达服务器,我就会用预定的名称保存它。然后我可以使用 Windows 自己的图片照片查看器打开图片。事实上,只要有新图片出现,照片查看器就会自行更新并显示新的屏幕截图。

但是,我无法在 JFrame 中打开屏幕截图。我编写了一个程序,以 jpg 格式截取屏幕截图,将它们发送到服务器,然后在 GUI 中打开它们。但是我在 GUI 部分打开时遇到问题。据我了解,它不会打开来自客户端的文件。

以下是我的代码,任何帮助将不胜感激。

服务器端,

 package ScreenCap;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import FileServer.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import test.TsgIcons;


/**
 *
 * @author busra
 */
public class ScreenCapServer extends Thread{
    String filePath;
    int portNumber;
    FileServer screenCapServer;
    ServerSocket getFileServer; 
    Socket getFile; 
    InputStream in;
    FileOutputStream fileOutputStream;
    TsgIcons screenShotIcons;

    public ScreenCapServer(String path, int port) {
        this.filePath = path;
        this.portNumber = port;
        this.screenShotIcons = new TsgIcons();
    }


    public static void waitTime(long millisecond){  
        long max = millisecond;  
        for(long i = 0;  i < max; i++){  
            for(long j = 0;  j < max; j++){  

            }  
        }  
    }      

    public void run() {
        while(true) {
            try {
                for(int i = 0; i < 10; i++) {
                    getFileServer = new ServerSocket(portNumber);
                    getFile = getFileServer.accept();
                    in = getFile.getInputStream();
                    fileOutputStream = new FileOutputStream(filePath + "\\" + i + ".jpg");
                    byte [] buffer = new byte[64*1024]; 
                    int bytesRead = 0;
                    while ( (bytesRead = in.read(buffer)) != -1) {
                        fileOutputStream.write(buffer, 0, bytesRead);
                    }                

                    in.close();
                    fileOutputStream.close();
                    getFileServer.close();
                    screenShotIcons.update();
                }
            } catch (IOException ex) {
                ex.printStackTrace(); 
            }
        }   

    }


}

图形用户界面,

    package test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

public class TsgIcons extends JFrame implements ActionListener {
    protected Timer timer;
    protected JLabel viewIcon;
    private String[] SMILEY = {"orig_screen"};
    private String BUTTON = "Button";
    private int timeCount;
    private int iconNumber;
    private String image;

    public TsgIcons() {
        this(1, 100);
    }

    public TsgIcons(int initialTime, int delay) {
        super("TSG Smileys");
        this.timeCount = initialTime;
        this.iconNumber = this.timeCount % this.SMILEY.length;
        this.image = "TransferredScreenShots\\" + this.SMILEY[this.iconNumber] + ".jpg";
        this.viewIcon = new JLabel();
        this.viewIcon.setIcon(new javax.swing.ImageIcon(this.image));
        this.timer = new Timer(delay, this);
        this.init();
    }

    protected void init() {
        JButton button = new JButton("Start / Stop");
        button.setActionCommand(BUTTON);
        button.addActionListener(this);
        this.viewIcon.setHorizontalAlignment(JLabel.CENTER);
        this.getContentPane().add(button, BorderLayout.SOUTH);
        this.getContentPane().add(this.viewIcon, BorderLayout.CENTER);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(250, 250);
        this.pack();

        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if ( BUTTON.equals(e.getActionCommand()) )  { // test if the button clicked
            if ( this.timer.isRunning() ) {
                this.timer.stop();
            } else {
                this.timer.start();
            }
        } else
        {   this.timeCount++;
                this.iconNumber = this.timeCount % this.SMILEY.length;
                this.image = "TransferredScreenShots\\" + this.SMILEY[this.iconNumber] + ".jpg";
                this.viewIcon.setIcon(new javax.swing.ImageIcon(this.image));
        }
    }

    public void update() {
        this.timeCount++;
        this.iconNumber = this.timeCount % this.SMILEY.length;
        this.image = "TransferredScreenShots\\" + this.SMILEY[this.iconNumber] + ".jpg";
        this.viewIcon.setIcon(new javax.swing.ImageIcon(this.image));

    }

    public static void main(String argv []) {
        new TsgIcons();
    }
}
4

0 回答 0