成都网站建设设计

将想法与焦点和您一起共享

java中窗口代码 Java窗口代码

java关闭当前窗口代码

方法一:

营山ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!

类 JFrame

javax.swing.JFrame

JFrame中的方法void setDefaultCloseOperation(int)可以设置

以下为改方法的用法:

setDefaultCloseOperation

public void setDefaultCloseOperation(int operation)设置用户在此窗体上发起

"close" 时默认执行的操作。必须指定以下选项之一:

DO_NOTHING_ON_CLOSE(在 WindowConstants 中定义):不执行任何操作;要求程序在已注册的

WindowListener 对象的 windowClosing 方法中处理该操作。

HIDE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册的 WindowListener

对象后自动隐藏该窗体。

DISPOSE_ON_CLOSE(在 WindowConstants 中定义):调用任意已注册 WindowListener

的对象后自动隐藏并释放该窗体。

EXIT_ON_CLOSE(在 JFrame 中定义):使用 System exit

方法退出应用程序。仅在应用程序中使用。

默认情况下,该值被设置为 HIDE_ON_CLOSE。更改此属性的值将导致激发属性更改事件,其属性名称为

"defaultCloseOperation"。

注:当 Java 虚拟机 (VM) 中最后一个可显示窗口被释放后,虚拟机可能会终止

要实现你说的,应该采用

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

方法二:

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class Test extends JFrame {

public Test(){

this.setTitle("title");

this.setSize(300,200);

this.setLocation(100,100);

//设置关闭时什么也不做

this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

//监听关闭按钮的点击操作

this.addWindowListener(new WindowAdapter(){

//new 一个WindowAdapter 类 重写windowClosing方法

//WindowAdapter是个适配器类 具体看jdk的帮助文档

public void windowClosing(WindowEvent e) {

//这里写对话框

if(JOptionPane.showConfirmDialog(null,

"退出","提

示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.YES_OPTION){

System.exit(0);

}

}

});

this.setVisible(true);

}

public static void main(String[] args) {

new Test();

}

}

窗口可见代码怎么打java

jframe.setVisible(true) 即可让窗口可见.

API里关于该方法的说明

public void setVisible(boolean b)

根据参数 b 的值显示或隐藏此 Window。

窗口的其他常用属性的设置,详细见下面的例子

示例图

参考代码和详细的注释

import java.awt.Color;

import java.awt.Font;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class MyFrame extends JFrame {

//构造函数

public MyFrame() {

JLabel jl = new JLabel("床前明月光,疑是地上霜。",JLabel.CENTER);//文字标签,文字居中

jl.setForeground(Color.BLUE);//文字的颜色

jl.setFont(new Font("仿宋", Font.BOLD, 20));//设置文字,字体

add(jl);//把文字添加到窗口

//getContentPane().setBackground(Color.WHITE); //设置窗口(内容面板)的背景颜色

setTitle("窗口示例");// 窗口标题

setSize(300, 200);// 窗口大小 宽300 高200

setLocationRelativeTo(null);// 窗口居中

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 当窗口关闭时,程序结束

}

//main函数

public static void main(String[] args) {

MyFrame frame = new MyFrame();// 创建窗口

frame.setVisible(true);// 让该窗口实例可见

}

}

java中关闭当前窗口用什么代码

你用的 swing 吗?加上 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

或者加上窗口事件监听器:

addWindowListener(new WindowAdapter() {

public void windowClosing (WindowEvent we) {

dispose();

}

});


网站题目:java中窗口代码 Java窗口代码
分享地址:http://chengdu.cdxwcx.cn/article/dosdocs.html