「java销毁界面」java销毁窗口
本篇文章给大家谈谈java销毁界面,以及java销毁窗口对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
Java的JFrame怎么销毁啊?
JFrame有一个 public void dispose()可以撤销窗口,并且释放窗口使用的资源。直接调用就可以了,例如:
JFrame frame = new JFrame();
frame.dispose();
不过更加一般的方法是在创建窗体时调用frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);这样就可以直接点击关闭按钮时撤销窗体并且释放窗体所使用的资源。
Java怎样销毁控制和窗口
方式一
import java.io.IOException;
public class Test {
public static void main(String[] args){
//执行批处理文件
String strcmd="cmd /c start D:\\antrelease.bat";
Runtime rt = Runtime.getRuntime();
Process ps = null;
try {
ps = rt.exec(strcmd);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
ps.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int i = ps.exitValue();
if (i == 0) {
System.out.println("执行完成.") ;
} else {
System.out.println("执行失败.") ;
}
ps.destroy();
ps = null;
new Test().killProcess();
}
public void killProcess(){
Runtime rt = Runtime.getRuntime();
Process p = null;
try {
rt.exec("cmd.exe /C start wmic process where name='cmd.exe' call terminate");
} catch (IOException e) {
e.printStackTrace();
}
}
}
方式二
process.destroy();
cmd /c dir 是执行完dir命令后封闭命令窗口。
cmd /k dir 是执行完dir命令后不封闭命令窗口。
cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会封闭。
cmd /k start dir 会打开一个新窗口后执行dir指令,原窗口不会封闭。
java如何销毁这个窗口?
this.dispose()不可以,因为你用的new ActionListener() Anonymous Inner Class.
要简单的话,让你得frame implements ActionListener,按钮事件放那里, 就可以用this.dispose()
java如何主动销毁Dialog面板?
Dialog出现后会阻塞后面的程序,但只是阻塞调用处的处理进程,而Dialog本身的代码却不受限制。因此,销毁的代码可以放在Dialog里面。
我的方法是在Dialog类里面添加一个Timer控件,用于计时,时间一到,dispose()销毁Dialog,下面是我的代码(好用的话请采纳):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Test extends JFrame implements ActionListener
{
JButton btnTest=new JButton("Test");
public Test()
{
setTitle("Test");
setSize(300,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(btnTest,BorderLayout.NORTH);
btnTest.addActionListener(this);
}
public static void main(String... args)
{
new Test().setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
new AutoDialog(this).setVisible(true);
}
private class AutoDialog extends JDialog //implements ActionListener
{
JLabel lblMsg=new JLabel("5");
Timer timer=new Timer(1000,new ActionListener()
{
int count=5;
public void actionPerformed(ActionEvent e)
{
count--;
if(count0)
{
timer.stop();
AutoDialog.this.dispose();
}
else lblMsg.setText(""+count);
}
});
public AutoDialog(Frame owner)
{
super(owner,"Dialog demo",true);
setSize(100,100);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
add(lblMsg,BorderLayout.CENTER);
timer.start();
}
}
}
关于java销毁界面和java销毁窗口的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-30,除非注明,否则均为
原创文章,转载请注明出处。