问答题
本程序中,主窗口包括一个文本框和一个文本域,在文本框中输入字符串后回车,则弹出一个带有“是”和“否”按钮的确认对话框,单击“是”按钮,则将文本框中的字符串添加到文本域中,否则不添加,并且将文本框中的内容清空,如图所示。
import java.awt.event.*;
import java.awt.*;
import javax.swing. JOptionPane;
class Dwindow extends Frame implements ActionListener
{ TextField inputName;
TextArea save;
Dwindow(String s)
{ super (s);
inputName=new TextField(22);
inputName.addActionListener(this);
save=new TextArea();
add(inputName,BorderLayout.NORTH);
add(save,BorderLayout.CENTER);
setBounds(60,60,300,300);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{ String s=inputName.getText();
int n=JOptionPane.______ (this,"确认正确吗? ","确认对话框",
______);
if(n==JOptionPane.YES_OPTION)
{ save.append("/n"+s);
}
else if(n==JOptionPane.NO_OPTION)
{ inputName.setText (null);
}
}
}
public class exam_90
{ public static void main(String args[])
{ new Dwindow("exam_90");
}
}