JAVA结束了一个程序,虚拟机还占用内存怎么解决

JAVA结束了一个程序,虚拟机还占用内存怎么解决,第1张

一般出现这种情况是程序还有线程没有结束。处理方法有二:

1、记录下自己开启的线程(比如定时器),并手动destroy。

2、监听窗口的关闭事件,执行强制退出。代码示例如下:

myWindowaddWindowListener(new WindowAdapter() {

 @Override

 public void windowClosed(WindowEvent e) {

  Systemexit(0);// 强制退出

 }

});

/

WindowListener:窗口监听事件 ex:关闭、最小化

@author Administrator

/

public class WindowListenerDemo {

public static void main(String[] args) {

// TODO Auto-generated method stub

JFrame frame = new JFrame("窗口程序");//创建一个带标题的窗口对象frame

framesetVisible(true);//显示窗口

framesetSize(600, 400);//设置窗口大小

framesetLocationRelativeTo(null);//设置窗口居中

//framesetDefaultCloseOperation(3);//点击X关闭程序 3为结束程序

framesetLayout(null);//清空布局

MyWindowListener myWindowListener = new MyWindowListener();//创建一个窗口监听事件对象

frameaddWindowListener(myWindowListener);//添加myMouseListener鼠标监听事件

framedispose();//关闭程序

}

}

class MyWindowListener implements WindowListener{

@Override

public void windowOpened(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowClosing(WindowEvent e) {

// TODO Auto-generated method stub

}

/

关闭窗口之后,自动调用此方法

/

@Override

public void windowClosed(WindowEvent e) {

// TODO Auto-generated method stub

Systemoutprintln("您关闭了窗口");

}

/

最小化窗口之后,自动调用此方法

/

@Override

public void windowIconified(WindowEvent e) {

// TODO Auto-generated method stub

Systemoutprintln("您把窗口最小化了");

}

@Override

public void windowDeiconified(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowActivated(WindowEvent e) {

// TODO Auto-generated method stub

}

@Override

public void windowDeactivated(WindowEvent e) {

// TODO Auto-generated method stub

}

}

int i=0;

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

if(egetActionCommand()equals("aa"))

{

Systemoutprintln("你点击了黑色");

jpsetBackground(ColorBLACK);

}

else if(egetActionCommand()equals("bb"))

{

if(i==0){

Systemoutprintln("你点击了红色");

jpsetBackground(ColorRED);

i++;

}else if(i==1){

Systemoutprintln("你点击了红色");

jpsetBackground(ColorYELLOW);

i=0;

}

}

else

{

Systemoutprintln("不知道");

}

}

以上就是关于JAVA结束了一个程序,虚拟机还占用内存怎么解决全部的内容,包括:JAVA结束了一个程序,虚拟机还占用内存怎么解决、java拿到窗体的句柄后如何监听一个 窗体上的控件、java监听事件处理等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/10208857.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存