導航:首頁 > 編程語言 > javaframe關閉

javaframe關閉

發布時間:2022-10-24 04:32:34

『壹』 java怎麼樣通過按鈕關閉一個JFrame

importjavax.swing.JFrame;

{

publicstaticvoidmain(String[]args){
newFrameTest("frame1");
newFrameTest("frame2");
newFrameTest("frame3");
}

publicFrameTest(Stringtitle){
this.setTitle(title);
this.setSize(800,600);
// this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// this.setDefaultCloseOperation(HIDE_ON_CLOSE);
// this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setVisible(true);
}

}

只要在每個Frame里設定this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);即可。

EXIT_ON_CLOSE,關閉程序。(所有窗口和進程都會關閉)

DISPOSE_ON_CLOSE,只關閉本窗口。

HIDE_ON_CLOSE,只隱藏本窗口,不關閉。

DO_NOTHING_ON_CLOSE,不做任何事,點擊關閉無效。

『貳』 java swing:關閉當前Frame

setVisible(false); //隱藏當前窗口
dispose();//銷毀當前窗口
一般用隱藏

『叄』 java的Frame窗體如何設置滑鼠點擊關閉圖標時關閉窗體

每個AWT窗體的右上角都有三個圖標,分別控制窗體的最小化、最大化和關閉。最小化和最大化圖標的功能是內置的,無需編程實現。而當用戶單擊關閉圖標時,AWT窗體無響應。用戶如果要關閉AWT窗體,只能通過任務管理器結束java進程,這顯然太麻煩了。因此,必須編程實現關閉圖標的功能,而這需要通過捕獲窗體事件來實現。

窗體事件是指示窗體狀態改變的低級別事件。當打開、關閉、激活、停用、圖標化或取消圖標化窗體對象時,或者焦點轉移到窗體內或移出窗體時,由窗體對象生成此事件。該事件被傳遞給每一個使用窗體的 addWindowListener 方法注冊以接收窗體事件的 WindowListener對象。也就是說,當用戶單擊窗體的關閉圖標時,將產生窗體事件(WindowEvent)。該事件由窗體監聽器介面(WindowListener)負責捕獲。

捕獲窗體事件的步驟是:
(1) 讓類實現窗體監聽器介面,這將使得該類變成窗體事件監聽器;
(2) 向類注冊窗體事件監聽器;
(3) 實現窗體事件監聽器的所有方法。

源代碼Calc.java可修改如下:
package calc;

import java.awt.*;
import java.awt.event.*;

public class Calc extends Frame implements WindowListener{

public Calc(){
super(「計算器」);
setSize(200,200);
addWindowListener(this);
}

// 用戶單擊窗體的關閉圖標時調用本方法。
public void windowClosing(WindowEvent e) {
System.exit(0); //終止當前正在運行的 Java 虛擬機,退出系統。
}
public void windowClosed(WindowEvent e) {}

public void windowOpened(WindowEvent e) {}

public void windowIconified(WindowEvent e) {}

public void windowDeiconified(WindowEvent e) {}

public void windowActivated(WindowEvent e) {}

public void windowDeactivated(WindowEvent e) {}

public static void main(String args[]){
new Calc().setVisible(true);
}
}

運行結果:單擊窗體的關閉圖標,窗體關閉。這說明窗體已具備響應事件的能力。

『肆』 java中怎樣關閉一個框架

框架沒有說關閉不關閉的問題,框架只是對你平時工作的一種封裝,用來簡化你的工作量的。一般的框架都是要在工程中引入一些jar包,然後配置一些文件,如web.xml和框架對應的XXX.xml,如果你不想使用這些框架,直接去掉web.xml中的相關配置即可。
目前常用的java web框架有 Spring,Hibernate,Struts1,Struts2等,有需要的可以去了解下。

『伍』 JAVA關於窗體JFrame的關閉事件

//設置關閉後直接退出
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);


packageLogin;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.net.URL;
importjavax.swing.ComboBoxModel;
importjavax.swing.DefaultComboBoxModel;

importjavax.swing.JLabel;
importjavax.swing.JPasswordField;
importjavax.swing.JRadioButton;
importjavax.swing.JTextField;

importjavax.swing.ImageIcon;
importjavax.swing.JButton;
importjavax.swing.JComboBox;
importjavax.swing.JOptionPane;
importjavax.swing.WindowConstants;
importjavax.swing.SwingUtilities;


/**
*'sJigloo
*SWT/SwingGUIBuilder,whichisfreefornon-commercial
*use.(ie,byacorporation,
*)thenyou
*.
*Pleasevisit

*.
*
*THISMACHINE,
*.
*/
.swing.JFrame{
privateJLabeljLabel_School_logo;
privateJLabeljLabel_user_name;
privateJLabeljLabel_passwd;
privateJButtonjButton_reg;
privateJButtonjButton_login;
;
privateJTextFieldjTextField_user_name;
privateJLabeljLabel_title;
privateintstudent_statue=0;
privateJComboBoxjComboBox1;
privateintmanager_statue=0;
/**
*
*/
/**
*
*/
/**
*Auto-
*/
publicstaticvoidmain(String[]args){
SwingUtilities.invokeLater(newRunnable(){
publicvoidrun(){
NewJFrameinst=newNewJFrame();
inst.setTitle("教室管理系統登錄");
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}

publicNewJFrame(){
super();
initGUI();
}

privatevoidinitGUI(){
try{
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
jLabel_School_logo=newJLabel();
getContentPane().add(jLabel_School_logo);
jLabel_School_logo.setBounds(0,0,86,84);
ImageIconlogo=newImageIcon("img/log.jpg");
jLabel_School_logo.setIcon(logo);
}
{
jLabel_title=newJLabel();
getContentPane().add(jLabel_title);
jLabel_title.setBounds(86,0,447,84);
ImageIcontitle=newImageIcon("img/title.jpg");
jLabel_title.setIcon(title);
}
{
jLabel_user_name=newJLabel();
getContentPane().add(jLabel_user_name);
jLabel_user_name.setText("u5b66u53f7uff1a");
jLabel_user_name.setBounds(155,131,51,31);
jLabel_user_name.setFont(newjava.awt.Font("葉根友毛筆行書2.0版",0,12));
}
{
jTextField_user_name=newJTextField();
getContentPane().add(jTextField_user_name);
jTextField_user_name.setBounds(199,135,151,23);
}
{
jLabel_passwd=newJLabel();
getContentPane().add(jLabel_passwd);
jLabel_passwd.setText("u5bc6u7801uff1a");
jLabel_passwd.setBounds(152,185,51,31);
jLabel_passwd.setFont(newjava.awt.Font("葉根友毛筆行書2.0版",0,12));
}
{
jPasswordField=newJPasswordField();
getContentPane().add(jPasswordField);
jPasswordField.setBounds(199,189,151,23);
}
{
jButton_login=newJButton();
getContentPane().add(jButton_login);
jButton_login.setText("u767bu5f55");
jButton_login.setBounds(199,235,68,23);
jButton_login.setFont(newjava.awt.Font("葉根友毛筆行書2.0版",0,12));
}
{
jButton_reg=newJButton();
getContentPane().add(jButton_reg);
jButton_reg.setText("u6ce8u518c");
jButton_reg.setBounds(282,235,68,23);
jButton_reg.setFont(newjava.awt.Font("葉根友毛筆行書2.0版",0,12));
jButton_reg.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEventevt){
jButton_regMouseClicked(evt);
}
});
}
{
ComboBoxModeljComboBox1Model=
newDefaultComboBoxModel(
newString[]{"學生/老師登錄","管理員登錄"});
jComboBox1=newJComboBox();
getContentPane().add(jComboBox1);
jComboBox1.setModel(jComboBox1Model);
jComboBox1.setBounds(199,90,152,23);
jComboBox1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevt){
jComboBox1ActionPerformed(evt);
}
});
}
pack();
this.setSize(549,334);
}catch(Exceptione){
//addyourerrorhandlingcodehere
e.printStackTrace();
}
}

(ActionEventevt){
if(jComboBox1.getSelectedIndex()==0)
{
jLabel_user_name.setText("學號:");
}
else
{
jLabel_user_name.setText("帳號:");
}
}

privatevoidjButton_regMouseClicked(MouseEventevt){
if(jComboBox1.getSelectedIndex()==1)
{
JOptionPane.showMessageDialog(NewJFrame.this,"管理員帳號不提供注冊!","",JOptionPane.DEFAULT_OPTION);
}
else
{
regregister=newreg();
register.setTitle("注冊");
register.setLocationRelativeTo(this);
register.setVisible(true);
}
}

}

//設置setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);在另一個方法,你怎麼用到你這個F呢?

『陸』 如何在frame裡面的窗口裡面關掉整個窗口

importjava.awt.*;

{

publicDemoFrame(){
ButtonbtnClose=newButton("關閉窗口");

add(btnClose);
setLayout(newFlowLayout());
setTitle("Frame窗口");
setSize(230,120);
setLocationRelativeTo(null);

//點擊窗口的關閉按鈕時,關閉窗口.結束程序
btnClose.addActionListener(e->{
this.setVisible(false);//窗口不可見
this.dispose();//銷毀窗口
System.exit(0);//結束程序
});
}

publicstaticvoidmain(String[]args){
//創建窗口並設置可見
EventQueue.invokeLater(()->newDemoFrame().setVisible(true));
}
}

『柒』 java如何關閉JFrame

1、你沒有為窗口添加窗口事件偵聽
2、這兩個變數要在你的main方法里調用,你的main方法是靜態的,靜態方法只能使用靜態變數,所以要將這兩個變數設置成靜態的
frm.addWindowListener(new WindowListener(){
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}});
貌似低版本jdk需要手動添加窗口事件監聽
你在那個close事件里加上關閉程序的代碼就行了

『捌』 Java jframe中如何實現窗口的關閉

效果圖

importjava.awt.event.*;
importjavax.swing.*;

{
JButtonjbExit;
publicDemoFrame(){
jbExit=newJButton("退出");
//當點擊退出按鈕時候的響應器
jbExit.addActionListener(newActionListener(){
@Override
publicvoidactionPerformed(ActionEvente){
doExit();//退出時候的方法
}
});
JPaneljp=newJPanel();
jp.add(jbExit);

add(jp);
setTitle("窗口");//窗口標題
setSize(380,185);//窗口大小
setLocationRelativeTo(null);//窗口居中
//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//通常添加這行代碼,點擊窗口右下角的關閉時會結束程序
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//右下角的關閉,不主動採取任何行動

//當點擊窗口右上角的關閉按鈕時候的響應器
addWindowListener(newWindowAdapter(){
@Override
publicvoidwindowClosing(WindowEvente){
doExit();
}
});
}

//main方法
publicstaticvoidmain(String[]args){
newDemoFrame().setVisible(true);
}

//退出時候的選擇
privatevoiddoExit(){
intn=JOptionPane.showConfirmDialog(null,"你確定要退出嗎?","消息提示",JOptionPane.YES_NO_OPTION);
//取消選擇是-1,確定是0,取消是1
System.out.println(n);
if(n==0){//如果選擇了確定
System.exit(0);//那麼退出
}
}
}

『玖』 java如何關閉某JFrame窗口,而不是退出整個程序!

  1. DO_NOTHING_ON_CLOSE(在 WindowConstants 中定義):不執行任何操作;要求程序在已注冊的 WindowListener 對象的 windowClosing 方法中處理該操作。

  2. HIDE_ON_CLOSE(在 WindowConstants 中定義):調用任意已注冊的 WindowListener 對象後自動隱藏該窗體。

  3. DISPOSE_ON_CLOSE(在 WindowConstants 中定義):調用任意已注冊 WindowListener 的對象後自動隱藏並釋放該窗體。

  4. EXIT_ON_CLOSE(在 JFrame 中定義):使用 System exit 方法退出應用程序。僅在應用程序中使用。

  5. 默認情況下,該值被設置為 HIDE_ON_CLOSE。更改此屬性的值將導致激發屬性更改事件,其屬性名稱為 "defaultCloseOperation"。

『拾』 Java如何關閉JFrame的其中一個子自定義窗口

1,XXX.dispose()方法吧!只關閉當前想要關閉的窗口,不影響其他窗口
2,JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);方法。
設置右上角的[x]為注銷窗口。

閱讀全文

與javaframe關閉相關的資料

熱點內容
java正則表達式工具 瀏覽:156
oa伺服器怎麼設置ftp 瀏覽:6
安卓如何安裝obb 瀏覽:440
QQ聊天記錄journal文件夾 瀏覽:118
蘋果公司雲伺服器地址 瀏覽:85
加密記事本手機 瀏覽:437
汽車壓縮機變頻閥 瀏覽:95
域外伺服器是什麼意思 瀏覽:639
大眾點評伺服器怎麼老卡頓 瀏覽:556
javavector與list的區別 瀏覽:316
java初始化類數組 瀏覽:303
java字元串轉換成json對象 瀏覽:647
android非阻塞socket 瀏覽:358
編譯系統概念 瀏覽:452
天眼通app能做什麼 瀏覽:557
魅族手機怎麼加密圖庫 瀏覽:8
rpa編譯器 瀏覽:572
車載雲伺服器記錄 瀏覽:740
四川金星壓縮機製造有限公司 瀏覽:55
移動平台圖片壓縮演算法 瀏覽:37