java访问动态创建jframe

wvyml7n5  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(149)

我正在创建一个应用程序,用java监视用户的桌面。有些客户端正在向服务器发送静态映像,服务器会刷新为该连接动态创建的jframe。我想为每个不存在的连接创建一个jframe。

package screensender;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.net.*;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ScreenSender extends Thread
{
       private ServerSocket serverSocket=null;
       private static Socket server = null;
       private Date date = null;
       private static final String DIR_NAME = "SACHIN";

       public ScreenSender() throws IOException, SQLException, ClassNotFoundException, Exception{
           serverSocket = new ServerSocket(1729);
           serverSocket.setSoTimeout(1800000);
       }

    public void run()
       {
           JFrame frame = new JFrame("Viewer");
           frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
           JLabel message = new JLabel();
           JLabel wIcon = new JLabel();

           ArrayList<String> framecimek = new ArrayList<String>();

           while(true)
          {
               try
               {
                  server = serverSocket.accept();
                  date = new Date();
                      DateFormat dateFormat = new SimpleDateFormat("_yyMMdd_HHmmss");
                  String fileName = server.getInetAddress().getHostName().replace(".", "-");

                  BufferedImage img=ImageIO.read(ImageIO.createImageInputStream(server.getInputStream()));
                  int width = img.getWidth();
                  int height = img.getHeight();

                  ImageIO.write(img, "jpg", new File("C:\\CFEDITSNAPS\\"+fileName+".jpg"));

                  Frame[] allFrames = Frame.getFrames();

                    for(Frame fr : allFrames){

                        String specificFrameName = fr.getTitle();

                        if(!framecimek.contains(fileName))
                        {
                            framecimek.add(fileName);

                            JFrame frame2 = new JFrame(fileName);
                            frame2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                            JLabel message2 = new JLabel();
                            JLabel wIcon2 = new JLabel();

                            frame2.setPreferredSize(new Dimension(width+15, height+39));
                            frame2.pack();
                            frame2.setVisible(true);

                            try {
                              img = ImageIO.read(new File("C:\\CFEDITSNAPS\\"+fileName+".jpg"));
                            } catch (IOException e) {
                              e.printStackTrace();
                            }

                            Image dimg2 = img.getScaledInstance(width, height,
                            Image.SCALE_SMOOTH);

                            ImageIcon wPic2 = new ImageIcon(dimg2);

                            wIcon2.setIcon(wPic2);

                            frame2.remove(wIcon2);
                            frame2.add(wIcon2);
                            SwingUtilities.updateComponentTreeUI(frame2);
                        } else if(framecimek.contains(fileName) && specificFrameName.equals(fileName))
                        {
                            fr.setPreferredSize(new Dimension(width+15, height+39));
                            fr.pack();
                            fr.setVisible(true);

                            try {
                              img = ImageIO.read(new File("C:\\CFEDITSNAPS\\"+fileName+".jpg"));
                            } catch (IOException e) {
                              e.printStackTrace();
                            }

                            Image dimg = img.getScaledInstance(width, height,
                            Image.SCALE_SMOOTH);

                            ImageIcon wPic = new ImageIcon(dimg);

                            wIcon.setIcon(wPic);

                            fr.remove(wIcon);
                            fr.add(wIcon);
                            SwingUtilities.updateComponentTreeUI(fr);
                        }
                    }
              }
             catch(SocketTimeoutException st)
             {
                   System.out.println("Socket timed out!");
                  break;
             }
             catch(IOException e)
             {
                  e.printStackTrace();
                  break;
             }
             catch(Exception ex)
            {
                  System.out.println(ex);
            }
          }
       }

       public static void main(String [] args) throws IOException, SQLException, ClassNotFoundException, Exception{
              ScreenSender serverApp = new ScreenSender();
              serverApp.createDirectory(DIR_NAME);
              Thread thread = new Thread(serverApp);
                thread.start();
       }

    private void createDirectory(String dirName) {
        File newDir = new File("C:\\"+dirName);
        if(!newDir.exists()){
            boolean isCreated = newDir.mkdir();
        }
     }
    }

这个想法是,如果连接提供了一个不存在的jframe,那么我创建它,但是如果它存在,我只刷新它的内容。问题是我无法使用 for(Frame fr : allFrames) 变量,但我需要用 wIcon 变量。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题