導航:首頁 > 編程語言 > java按鈕關閉窗口

java按鈕關閉窗口

發布時間:2024-10-12 21:32:04

A. 如何在java程序中,當點擊一個按鈕後,關閉當前窗口,開啟一個新的窗口。

java中一般使用swing 和awt技術來實現圖形界面,Swing組件較多,功能比較強大,所以這里使用Swing組件來實現。窗口使用(JFrame),按鈕使用(JButton)。


兩個窗口關聯並且跳轉,最常見的場景就是登陸了。

登陸窗口,輸入用戶名和密碼,如果成功就跳轉到主窗口


但是隱藏窗口,會在後面浪費系統資源。

如果不再使用該窗口,應該徹底銷毀(dispose();),釋放系統資源


LoginFrame 類

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

//LoginFrame登陸窗口功能實現用戶登陸,如果成功就跳轉
//LoginFrame繼承於窗口類,並實現ActionListener介面
{
//申明需要的組件
JButtonjb1,jb2;//按鈕
JTextFieldjtf1;//文本框
JPasswordFieldjpf1;//密碼框

publicLoginFrame(){
//窗口屬性的設置
setTitle("登陸窗");//窗口標題
setSize(300,180);//窗口大小
setLocationRelativeTo(null);//窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//關閉窗口則退出虛擬機
setLayout(newFlowLayout());//設置布局流式布局

JPaneljp=newJPanel(newGridLayout(4,1));//設置面板為表格布局4行1列
//第一行
JPaneljp1=newJPanel();
JLabeljl1=newJLabel("賬號");
jtf1=newJTextField(12);
jp1.add(jl1);
jp1.add(jtf1);
jp.add(jp1);
//第二行
JPaneljp2=newJPanel();
JLabeljl2=newJLabel("密碼");
jpf1=newJPasswordField(12);
jp2.add(jl2);
jp2.add(jpf1);
jp.add(jp2);
//第三行
JPaneljp3=newJPanel();
jb1=newJButton("登陸");
jb1.addActionListener(this);//添加動作響應器
jb2=newJButton("重置");
jb2.addActionListener(this);//添加動作響應器
jp3.add(jb1);
jp3.add(jb2);
jp.add(jp3);
//第四行
JPaneljp4=newJPanel();
JLabeljl3=newJLabel("提示:賬號admin密碼123");
jl3.setForeground(Color.DARK_GRAY);
jp4.add(jl3);
jp.add(jp4);

add(jp);

}

//動作響應
publicvoidactionPerformed(ActionEvente){
Stringcmd=e.getActionCommand();//根據動作命令,來進行分別處理
if(cmd.equals("登陸")){
Stringid=jtf1.getText();//取得用戶名
Stringkey=newString(jpf1.getPassword());//取得密碼
if(id.equals("admin")&&key.equals("123")){//判斷是否登錄成功
//如果登錄成功
setVisible(false);//本窗口隱藏,
newMainFrame(id).setVisible(true);//新窗口顯示
dispose();//本窗口銷毀,釋放內存資源
}else{
//如果登錄失敗彈出提示
JOptionPane.showMessageDialog(this,"用戶名或者密碼錯誤.","通知",JOptionPane.ERROR_MESSAGE);
clearText();//清空文本框密碼框的輸入
}
}elseif(cmd.equals("重置")){
clearText();
}

}

privatevoidclearText(){//清空文本框,密碼框的輸入
jtf1.setText("");
jpf1.setText("");
}

//main方法,程序的入口
publicstaticvoidmain(String[]args){
newLoginFrame().setVisible(true);//創建登錄窗口,並可見
}

}

MainFrame類

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

{

publicMainFrame(Stringname){
setTitle("主窗口");//標題
setSize(300,260);//大小
setLocationRelativeTo(null);//居中
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPaneljp=newJPanel();
JLabeljl1=newJLabel("歡迎");
JLabeljl2=newJLabel(name);
jl2.setForeground(Color.BLUE);//設置文本顏色
JLabeljl3=newJLabel("使用系統.");
jp.add(jl1);
jp.add(jl2);
jp.add(jl3);
add(jp,BorderLayout.NORTH);
JTextAreajta=newJTextArea();
jta.setLineWrap(true);
jta.append("消息提示");
jta.append("消息!通知~!");
JScrollPanejsp=newJScrollPane(jta,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(jsp);
JPaneljp2=newJPanel();
JButtonjb=newJButton("退出");
jb.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.exit(0);//退出
}
});
jp2.add(jb);
add(jp2,BorderLayout.SOUTH);
}

}


B. java 如何實現點擊關閉後 關閉窗口 求詳細代碼

方法一:
類 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();
}

}
分享

C. JAVA怎麼實現點擊按鈕關閉窗口

你可以按鈕上添加事件把窗口2設置為隱藏。

jButton1.setText("ok");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jFrame1.setVisible(false);
}
});

另外 按鈕退出的命令是在按鈕的事件中寫
System.exit(0);

D. java設置關閉窗口按鈕

package org.t20110219.test;

/**
* 修改後的代碼
* 能關閉,
* 希望能幫助你
*/
import java.awt.*;
import java.awt.event.*;
public class MainProgram extends Frame implements ActionListener,WindowListener
{
int i;
Button b1 = new Button("確定");
Button b2 = new Button("關閉");
void FlowLayoutEX()
{
this.setTitle("布局管理器");
this.setLayout(new FlowLayout());
this.add(b1);
b1.addActionListener(this);
this.add(b2);
b2.addActionListener(this);
this.addWindowListener(this);
this.setBounds(100, 100, 250, 250);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
/**------------修改代碼--------------*/
/**
* 修改 合理就可以了
* e.getSource() 可以得到 你點擊了哪個 按鈕
* 判斷之後就可以 設定 對應的 操作了
*/
if(e.getSource()==b2){
System.exit(0);
}else{
i++;
Button bi = new Button("按鈕"+i);
this.add(bi);
this.show(true);
}
}
public void windowClosing(WindowEvent f)
{
System.exit(0);
}
public void windowOpened(WindowEvent f){}
public void windowClosed(WindowEvent f){}
public void windowIconified(WindowEvent f){}
public void windowDeiconified(WindowEvent f){}
public void windowActivated(WindowEvent f){}
public void windowDeactivated(WindowEvent f){}
public static void main(String args[])
{
MainProgram window = new MainProgram();
window.FlowLayoutEX();
}
}

閱讀全文

與java按鈕關閉窗口相關的資料

熱點內容
平安信用卡app怎麼激活 瀏覽:133
單張文件夾力架 瀏覽:838
android自定義jar 瀏覽:543
APP怎麼安裝的系統 瀏覽:922
程序員那麼可愛哪部小說改編的 瀏覽:136
免費解壓軟體官方免費下載 瀏覽:89
程序員做bug被女人懟 瀏覽:806
javajdk配置環境變數 瀏覽:685
京東刷腳本用什麼安卓模擬器 瀏覽:112
惠州解壓地方 瀏覽:966
java按鈕關閉窗口 瀏覽:50
單片機開發是什麼 瀏覽:504
pdf機位 瀏覽:698
如何把數據編譯到程序中 瀏覽:611
php中文分詞api 瀏覽:828
電腦程序員寫代碼有潛質嗎 瀏覽:944
android添加字體樣式 瀏覽:709
程序員只一輪面試好不好 瀏覽:153
每日優鮮安卓怎麼退出登錄 瀏覽:332
編譯器怎麼區分字元 瀏覽:404