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);
}
}
}
importjava.awt.*;
importjava.awt.event.*;
publicclassT7_5{
publicstaticvoidmain(String[]args){
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);
setVisible(true);
}
publicvoidactionPerformed(ActionEvente){
Strings=ta1.getSelectedText();
ta2.append(s);
}
}
//带关闭功能的窗口
importjava.awt.*;
importjava.awt.event.*;
publicclassT7_6{
publicstaticvoidmain(String[]args){
newMathWindow("计算");
}
}
classMathWindowextendsFrameimplementsActionListener{//定义一个类继承于Frame并实现了接口ActionListener
Buttonbtn_Add,btn_Sub,btn_Mul,btn_Mov;//定义四个表示运算的按钮
TextFieldtf1,tf2,tf3;//定义三个文本框
Labell1,l2;//定义两个标签
MathWindow(Strings){
super(s);//设置标题
setLayout(newFlowLayout());//设置窗口模式
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("=",1);//实例化两个标签
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);//设置窗口大小是否可由用户改变
setVisible(true);//设置窗口的显示状态
validate();//刷新
}
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){
l1.setText("-");
tf3.setText(String.valueOf(m-n));
}
elseif(e.getSource()==btn_Mul){
l1.setText("x");
tf3.setText(String.valueOf(m*n));
}
elseif(e.getSource()==btn_Mov){
l1.setText("/");
tf3.setText(String.valueOf(1.0*m/n));
}
else{
System.exit(0);
}
}
catch(NumberFormatExceptione1){//异常处理
System.out.println("输入数字字符串!
");
}
}
}
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjavax.swing.border.*;
publicclassT7_7{
publicstaticvoidmain(String[]args){
WindowBoxwb=newWindowBox("确定");
}
}
classWindowBoxextendsFrameimplementsActionListener{
BoxbaseBox,boxV1,boxV2;
TextFieldtf1,tf2,tf3;
Buttonbtn_OK;
TextAreata;
WindowBox(Strings){
super(s);
tf1=newTextField(12);
tf2=newTextField(12);
tf3=newTextField(12);
boxV1=Box.createVerticalBox();
boxV1.add(newLabel("姓名"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(newLabel("E_mail"));
boxV1.add(Box.createVerticalStrut(8));
boxV1.add(newLabel("职业"));
boxV2=Box.createVerticalBox();
boxV2.add(tf1);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(tf2);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(tf3);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);
setLayout(newFlowLayout());
btn_OK=newButton("提交");
btn_OK.