import javax.swing.*;
我們提供的服務(wù)有:網(wǎng)站設(shè)計制作、網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、博樂ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的博樂網(wǎng)站制作公司
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class login extends JFrame implements ActionListener,ItemListener
{
JButton buttonone,buttontwo;
login(String s)
{
super(s);
//歡迎界面
JLabel labelone=new JLabel(new ImageIcon("login.jpg")),
labeltwo=new JLabel("UID"),
labelthree=new JLabel("Password");
JPanel panelone=new JPanel(),
paneltwo=new JPanel();
//panelone中為登陸信息
panelone.add(labeltwo);
JTextField textone=new JTextField(16);
//textone.addActionListener(this);
panelone.add(textone);
panelone.add(labelthree);
JPasswordField passone=new JPasswordField(16);
//passone.addActionListener(this);
panelone.add(passone);
//paneltwo中為權(quán)限選擇
JRadioButton radioone=new JRadioButton("學(xué)生"),
radiotwo=new JRadioButton("教師"),
radiothree=new JRadioButton("學(xué)院");
ButtonGroup group=new ButtonGroup();
radioone.setSelected(true);//默認(rèn)該單選按鈕被選中
/*radioone.addItemListener(this);
radiotwo.addItemListener(this);
radiothree.addItemListener(this);*/
group.add(radioone);
group.add(radiotwo);
group.add(radiothree);
buttonone=new JButton("登陸");
buttontwo=new JButton("退出");
buttonone.addActionListener(this);
buttontwo.addActionListener(this);
paneltwo.add(radioone);
paneltwo.add(radiotwo);
paneltwo.add(radiothree);
paneltwo.add(buttonone);
paneltwo.add(buttontwo);
Container con=getContentPane();
con.add(labelone,BorderLayout.NORTH);
con.add(panelone,BorderLayout.CENTER);
con.add(paneltwo,BorderLayout.SOUTH);
validate();
setVisible(true);
setSize(500,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==buttontwo)
{
System.exit(0);//?????????問題出現(xiàn)的地方.類似的題目做過許多都沒問題,但是本次實驗就是無法退出!!望各位指點!!
System.out.println("sssss");
}
}
public void itemStateChanged(ItemEvent e2)
{
}
}
public class Window
{
public static void main(String args[])
{
new login("login");
}
}
你前面申明了Button buttonone,buttontwo; 改為JButton buttonone,buttontwo;
JButton buttonone=new JButton("登陸"),
buttontwo=new JButton("退出");
改為
buttonone=new JButton("登陸");
buttontwo=new JButton("退出");
如果是點擊上面的那個叉號退出的話就加上這樣一句setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
如果是通過按鈕退出就用監(jiān)聽器實現(xiàn)如:
class MyListener2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
一般情況下這兩種都有。
關(guān)閉代碼:
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
清空輸入欄
button.addActionListener(this);
再定義方法:
public void actionPerformed(ActionEvent e){
if(e.getSource()==button) //button為你的按鈕名
{ text.setText(null); //text為你的輸入欄名稱
}
}
1、打開eclipse,并且建立java一個工程,具體如下代碼:
addActionListene
(new ActionListene
()
{
pu
lic void actionPe
fo
med(ActionEvent e)
{
dispose();
}
});
2、執(zhí)行該程序查看結(jié)果,如圖所示。
你用的 swing 嗎?加上 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
或者加上窗口事件監(jiān)聽器:
addWindowListener(new WindowAdapter() {
public void windowClosing (WindowEvent we) {
dispose();
}
});
在你的構(gòu)造起里 或者main方法里 添加一句代碼
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
這局代碼意思是 當(dāng)你點擊本程序的關(guān)閉按鈕時 程序退出 而不是惡心的不可見了。
分享文章:窗口按鈕退出代碼java 窗體退出按鈕代碼怎么寫
鏈接地址:http://muchs.cn/article22/docdojc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站策劃、靜態(tài)網(wǎng)站、App設(shè)計、網(wǎng)站制作、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)