如何在Java中隐藏窗口而不是关闭窗口? [英] How to hide window instead of closing it, in java?

查看:354
本文介绍了如何在Java中隐藏窗口而不是关闭窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过

setDefaultCloseOperation(HIDE_ON_CLOSE);

隐藏隐藏的窗口.
.
但是,当我使用

.
But when I respawn the window by using

setVisible(true)

重新生成窗口时,窗口的大小为0,并且所有组件均不可见.为什么会发生这种情况,解决方案可能是什么?


这是MyClient类和ListenServerOpenWindow类的代码,其中我更改了另一个类ChatWindow的可见性

the window appears with size 0, and none of the components are visible. Why does this happen and what could be the solution?


This is the code for MyClient class and ListenServerOpenWindow class in which i have changed the visibility of the ChatWindow which is another class

public class MyClient extends WindowAdapter implements ActionListener
{
     **********
      MyClient(String name)throws IOException
      {
                windowsNames = new String[10];
                c= new ChatWindow[10];
                count =0; found =false;
              ********************
                 ListenServerOpenWindow m=new ListenServerOpenWindow(name,din,dout);
		Thread t=new Thread(m);
		t.start(); t.setName("Listening to Server for Request to Open Chat Window");
      }
      public void actionPerformed(ActionEvent e){
		// sending the messages
            try
            {
		if(e.getSource()==button){	
			System.out.println(list1+"test");
			str=(String)list1.getSelectedValue();
			dout3.writeUTF(str);
                        //Thread.currentThread().sleep(500);
                        found =false;
                        for(i=0;i<windowsnames.length;i++)>
                        {
                          if(windowsNames[i]!=null)
                            if(windowsNames[i].equalsIgnoreCase(str)&&windowsNames[i]!=null)
                          {
                              found=true; break;
                          }
                        }
                        if(found)
                        {
                            c[i].setVisible(true);
                        }
                        else
                        {
                            windowsNames[count]=str;
                            c[count++]=  new ChatWindow(name,str,din,dout);
                        }
                  }
            }catch(Exception ex){ex.printStackTrace();}
}




还有ListenServerOpenWindow类




And the ListenServerOpenWindow class

class ListenServerOpenWindow implements Runnable
{
    Socket s,s3;String chatwith,name; ChatWindow[] c;String[] windowsNames;
    DataInputStream din;DataOutputStream dout; int count; boolean found; int i;
    
    ListenServerOpenWindow(String name,DataInputStream din,DataOutputStream dout)
    {
       this.din = din;
       this.dout= dout;
        this.name=name;
        this.s=MyClient.s;
        this.s3 = MyClient.s3;
        c=new ChatWindow[10];
        windowsNames = new String[10];
        count =0;found =false;
        
    }
    public void run()
    {
        try
        {
            
            while(true)
            {
                
                
                DataInputStream d = new DataInputStream(s3.getInputStream());
               
                String cmd = d.readUTF();
                if(cmd.length()>9)
                {
                    
                    chatwith = cmd.substring(10);
                    
                    if(cmd.substring(0,10).equalsIgnoreCase("openwindow"))
                    {
                        //windowsNames[count]=chatwith;
                        found =false;
                        for(i=0;i<windowsnames.length;i++)>
                        {
                            if(windowsNames[i]!=null)
                          if(windowsNames[i].equalsIgnoreCase(chatwith))
                          {
                              found=true; break;
                          }
                        }
                        if(found)
                        {
                            c[i].setVisible(true);
                        }
                        else
                        {
                            windowsNames[count]=chatwith;
                            c[count++]=  new ChatWindow(name,chatwith,din,dout);
                        }
                    }
                }
            }
        }
        catch(Exception e){e.printStackTrace();}
    }
}




我在扩展JFrame类的ChatWindow类的构造函数中使用了




I have used

setDefaultCloseOperation(HIDE_ON_CLOSE)



重新生成聊天窗口时,未显示任何组件.

in the constructor of the ChatWindow class which extends JFrame class


On respawning the chatwindow, none of the components are shown

推荐答案

问题已解决.解决方案是使用
Problem solved. Solution was to use
c[i].frame.setVisible(true);

,其中frameJFrame对象


这篇关于如何在Java中隐藏窗口而不是关闭窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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