java窗体退出_java做一个窗口怎么设置一个退出按钮

importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassX10{publicstaticvoidmain(String[]args){Winwin=newWin();win.setTitle("下拉框的事件");}}classWinexten...

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class X10

{

public static void main(String[]args){

Win win=new Win();

win.setTitle("下拉框的事件");

}

}

class Win extends JFrame implements ItemListener,ActionListener

{

JComboBox choice;

JTextField text;

JTextArea area;

JButton add,del,exi;

Win(){

init();

setBounds(300,400,300,300);

setVisible(true);

setDefaultCloseOperation(EXIT_ON_CLOSE);

}

void init(){

setLayout(new FlowLayout());

choice=new JComboBox();

text=new JTextField(8);

area=new JTextArea(6,25);

choice.addItem("音乐天地");

choice.addItem("武术天地");

choice.addItem("象棋乐园");

choice.addItem("交友聊天");

add=new JButton("添加");

del=new JButton("删除");

exi=new JButton("退出");

add.addActionListener(this);

del.addActionListener(this);

text.addActionListener(this);

choice.addItemListener(this);

exi.addActionListener(this);

add(choice);

add(add);

add(del);

add(text);

add(exi);

add(new JScrollPane(area));

}

//public void actionPerformed(ActionEvent e){

public void itemStateChanged(ItemEvent e){

String name=choice.getSelectedItem().toString();

int index=choice.getSelectedIndex();

area.setText("\n"+index+":"+name);

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==add||e.getSource()==text){

String name=text.getText();

if(name.length()>0){

choice.addItem(name);

choice.setSelectedItem(name);

area.append("\n列表添加:"+name);

}

}

else if(e.getSource()==del){

area.append("\n列表删除:"+choice.getSelectedItem());

choice.removeItemAt(choice.getSelectedIndex());

}

}

}请看上面的代码,按钮exi该怎么添加监视器,能让点击“退出”关闭窗口

展开


版权声明:本文为weixin_30082211原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。