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);
请问一下到底是为什么呀?
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);
请问一下到底是为什么呀?
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();
}
}
恭喜LZ