java2实用教程第三版第七章课后习题答案耿祥义文档格式.docx
《java2实用教程第三版第七章课后习题答案耿祥义文档格式.docx》由会员分享,可在线阅读,更多相关《java2实用教程第三版第七章课后习题答案耿祥义文档格式.docx(19页珍藏版)》请在冰豆网上搜索。
add(textA2);
textA1.addTextListener(this);
setBounds(0,0,300,300);
setVisible(true);
validate();
publicvoidtextValueChanged(TextEvente){
Strings=textA1.getText();
StringTokenizerfenxi=newStringTokenizer(s,"
\n\r"
intn=fenxi.countTokens();
longsum=0;
try{
for(inti=0;
i<
n;
i++){
sum+=Long.parseLong(fenxi.nextToken());
}
textA2.setText("
总和"
+String.valueOf(sum));
doubleavg=(double)sum/n;
textA2.append("
\n平均数"
+String.valueOf(avg));
}catch(NumberFormatExceptione1){
//System.out.println("
输入有误!
"
System.out.println(s);
}
publicclassT7_5{
newMyFrame("
挑单词"
classMyFrameextendsFrameimplementsActionListener{
TextAreata1,ta2;
Buttonbtn;
MyFrame(Strings){
super(s);
setLayout(newBorderLayout());
ta1=newTextArea(5,15);
ta2=newTextArea(5,15);
btn=newButton("
追加"
btn.addActionListener(this);
add(ta1,BorderLayout.EAST);
add(ta2,BorderLayout.WEST);
add(btn,BorderLayout.SOUTH);
setBounds(100,100,300,200);
publicvoidactionPerformed(ActionEvente){
Strings=ta1.getSelectedText();
ta2.append(s);
//带关闭功能的窗口
publicclassT7_6{
newMathWindow("
计算"
classMathWindowextendsFrameimplementsActionListener{//定义一个类继承于Frame并实现了接口ActionListener
Buttonbtn_Add,btn_Sub,btn_Mul,btn_Mov;
//定义四个表示运算的按钮
TextFieldtf1,tf2,tf3;
//定义三个文本框
Labell1,l2;
//定义两个标签
MathWindow(Strings){
//设置标题
//设置窗口模式
btn_Add=newButton("
加"
btn_Sub=newButton("
减"
btn_Mul=newButton("
乘"
btn_Mov=newButton("
除"
//实例化四个表示运算的按钮
tf1=newTextField(8);
tf2=newTextField(8);
tf3=newTextField(8);
//实例化三个文本框
l1=newLabel("
1);
l2=newLabel("
="
//实例化两个标签
add(tf1);
add(l1);
add(tf2);
add(l2);
add(tf3);
add(btn_Add);
add(btn_Sub);
add(btn_Mul);
add(btn_Mov);
//将所需的组件加入到窗口中
btn_Add.addActionListener(this);
btn_Sub.addActionListener(this);
btn_Mul.addActionListener(this);
btn_Mov.addActionListener(this);
//设置四个表示运算按钮的监视器为本窗口
this.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
dispose();
//撤销当前窗口
}
);
setBounds(100,100,500,80);
//设置窗口大小
setResizable(false);
//设置窗口大小是否可由用户改变
//设置窗口的显示状态
//刷新
publicvoidactionPerformed(ActionEvente){//实现ActionListener接口的方法
Strings1=tf1.getText();
Strings2=tf2.getText();
//获取两个文本框的内容
try{
longm=Long.parseLong(s1);
longn=Long.parseLong(s2);
//将获取的文本框的内容转换成长整型
if(e.getSource()==btn_Add){//判断事件的发生源并做出相应的处理
l1.setText("
+"
tf3.setText(String.valueOf(m+n));
elseif(e.getSource()==btn_Sub){
-"
tf3.setText(String.valueOf(m-n));
elseif(e.getSource()==btn_Mul){
x"
tf3.setText(String.valueOf(m*n));
elseif(e.getSource()==btn_Mov){
/"
tf3.setText(String.valueOf(1.0*m/n));
else{
System.exit(0);
catch(NumberFormatExceptione1){//异常处理
System.out.println("
输入数字字符串!
importjavax.swing.*;
importjavax.swing.border.*;
publicclassT7_7{
WindowBoxwb=newWindowBox("
确定"
classWindowBoxextendsFrameimplementsActionListener{
BoxbaseBox,boxV1,boxV2;
Buttonbtn_OK;
TextAreata;
WindowBox(Strings){
tf1=newTextField(12);
tf2=newTextField(12);
tf3=newTextField(12);
boxV1=Box.createVerticalBox();
boxV1.add(newLabel("
姓名"
));
boxV1.add(Box.createVerticalStrut(8));
E_mail"
职业"
boxV2=Box.createVerticalBox();
boxV2.add(tf1);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(tf2);
boxV2.add(tf3);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
btn_OK=newButton("
提交"
btn_OK.