窗口关闭事件,点"叉"关不了窗口.程序是在一个记事本中写的

shiyuqian 2007-12-24 07:05:48
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


public void actionPerformed(ActionEvent e)
if(e.getSource()=="退出"){
int a = JOptionPane.showConfirmDialog(this,"文件已被改变,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION);
if(a==1){
this.dispose();
}else if(a==0){

File f2 = null;
JFileChooser jfc2 = new JFileChooser();
int num2 = jfc2.showSaveDialog(this);
if(num2==JFileChooser.APPROVE_OPTION){
f2=jfc2.getSelectedFile();
this.setTitle(f2.getName());
try{
FileWriter fw = new FileWriter(f2);
BufferedWriter bw = new BufferedWriter(fw);

bw.write(textarea.getText());
bw.close();
fw.close();
}catch(IOException e2){
e2.printStackTrace();
}
this.dispose();
}
}
}

最上面一句是在构造器中写的.总是有错误.显示"cannot find symbol method setDefaultCloseOperation(int);
请问一下到底是为什么呀?
...全文
284 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
胡矣 2007-12-26
  • 打赏
  • 举报
回复
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
恭喜LZ
shiyuqian 2007-12-26
  • 打赏
  • 举报
回复
嘿嘿 谢谢各位了啊,我做好了,谢谢帮忙啊
xql80329 2007-12-25
  • 打赏
  • 举报
回复
你应该返回去看看API
cursor_wang 2007-12-25
  • 打赏
  • 举报
回复
我怎么就可以啊!给你个例子参考下.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class PersonInfoRecorder extends JFrame implements ActionListener{
JLabel l1=new JLabel("姓名");
JLabel l2=new JLabel("性别");
JLabel l3=new JLabel("年龄");
JLabel l4=new JLabel("");

JTextField tf1=new JTextField(7);
JTextField tf2=new JTextField(7);

JTextArea ta=new JTextArea(80,30);

JButton b=new JButton("确定");

JComboBox cb=new JComboBox();

JPanel p=new JPanel();
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();

PersonInfoRecorder(){
cb.addItem("男");
cb.addItem("女");
ta.setBackground(Color.cyan);
ta.setEditable(false);
p.setLayout(new BorderLayout());
p.add(p1,BorderLayout.NORTH);
p.add(p2,BorderLayout.CENTER);
p.add(p3,BorderLayout.SOUTH);
p1.add(l1);
p1.add(tf1);
p1.add(l2);
p1.add(cb);
p1.add(l3);
p1.add(tf2);
p1.add(b);
p2.add(ta);
p3.add(l4);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setContentPane(p);
this.setTitle("个人信息录入器");
this.setSize(400,200);
this.setVisible(true);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{

if(e.getSource().equals(b))
{
if(tf1.getText().length()==0)
{
l4.setText("姓名不能为空!");
ta.setText("");
}
else
{
ta.setText(tf1.getText()+","+ cb.getSelectedItem() +","+tf2.getText());
l4.setText("");
}
}
}
public static void main(String[] args){
new PersonInfoRecorder();
}
}


newflypig 2007-12-24
  • 打赏
  • 举报
回复
如果用一楼的方法不行的话,那你应该是用awt写的吧,但是你上面又出现JFileChooser,呵呵,真不知道你是怎么搞的,对了把一楼的那一句话放在JFrame的构造函数看看,我估计你放的位置不对
shiyuqian 2007-12-24
  • 打赏
  • 举报
回复
还是不行啊
cursor_wang 2007-12-24
  • 打赏
  • 举报
回复
我是这样写的,试下这个this.setDefaultCloseOperation(EXIT_ON_CLOSE);

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧