1、java2实用教程第三版第七章课后习题答案耿祥义import java.awt.*;import java.awt.event.*;import java.util.*;public class T7_4 public static void main(String args) FirstWindow win = new FirstWindow(计算的窗口); class FirstWindow extends Frame implements TextListener TextArea textA1,textA2; FirstWindow(String s) setTitle(s); set
2、Layout(new FlowLayout(); textA1 = new TextArea(6,15); textA2 = new TextArea(6,15); add(textA1); add(textA2); textA1.addTextListener(this); setBounds(0,0,300,300); setVisible(true); validate(); public void textValueChanged(TextEvent e) String s = textA1.getText(); StringTokenizer fenxi = new StringTo
3、kenizer(s, nr); int n = fenxi.countTokens(); long sum=0; try for(int i = 0; in; i+) sum += Long.parseLong(fenxi.nextToken(); textA2.setText(总和 + String.valueOf(sum); double avg = (double)sum/n; textA2.append(n平均数 + String.valueOf(avg); catch (NumberFormatException e1) /System.out.println(输入有误!); Sys
4、tem.out.println(s); import java.awt.*;import java.awt.event.*;public class T7_5 public static void main(String args) new MyFrame(挑单词); class MyFrame extends Frame implements ActionListener TextArea ta1,ta2; Button btn; MyFrame (String s) super(s); setLayout(new BorderLayout(); ta1 = new TextArea(5,1
5、5); ta2 = new TextArea(5,15); btn = new Button(追加); btn.addActionListener(this); add(ta1,BorderLayout.EAST); add(ta2,BorderLayout.WEST); add(btn,BorderLayout.SOUTH); setBounds(100,100,300,200); setVisible(true); public void actionPerformed(ActionEvent e) String s = ta1.getSelectedText(); ta2.append(
6、s); /带关闭功能的窗口import java.awt.*;import java.awt.event.*;public class T7_6 public static void main(String args) new MathWindow(计算); class MathWindow extends Frame implements ActionListener /定义一个类继承于Frame 并实现了接口ActionListener Button btn_Add,btn_Sub,btn_Mul,btn_Mov;/定义四个表示运算的按钮 TextField tf1,tf2,tf3;/定义
7、三个文本框 Label l1,l2;/定义两个标签 MathWindow(String s) super(s);/设置标题 setLayout(new FlowLayout();/设置窗口模式 btn_Add = new Button(加); btn_Sub = new Button(减); btn_Mul = new Button(乘); btn_Mov = new Button(除);/实例化四个表示运算的按钮 tf1 = new TextField(8); tf2 = new TextField(8); tf3 = new TextField(8);/实例化三个文本框 l1 = new
8、Label(,1); l2 = new Label(=,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);/设置四个表示运算按钮的
9、监视器为本窗口 this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) dispose();/撤销当前窗口 ); setBounds(100,100,500,80);/设置窗口大小 setResizable(false);/设置窗口大小是否可由用户改变 setVisible(true);/设置窗口的显示状态 validate();/刷新 public void actionPerformed(ActionEvent e) /实现ActionListener接口的方法 String s
10、1 = tf1.getText(); String s2 = tf2.getText();/获取两个文本框的内容 try long m = Long.parseLong(s1); long n = Long.parseLong(s2);/将获取的文本框的内容转换成长整型 if(e.getSource() = btn_Add) /判断事件的发生源 并做出相应的处理 l1.setText(+); tf3.setText(String.valueOf(m+n); else if(e.getSource() = btn_Sub) l1.setText(-); tf3.setText(String.va
11、lueOf(m-n); else if (e.getSource() = btn_Mul) l1.setText(x); tf3.setText(String.valueOf(m*n); else if(e.getSource() = btn_Mov) l1.setText(/); tf3.setText(String.valueOf(1.0*m/n); else System.exit(0); catch(NumberFormatException e1) /异常处理 System.out.println(输入数字字符串!); import java.awt.*;import java.aw
12、t.event.*;import javax.swing.*;import javax.swing.border.*;public class T7_7 public static void main(String args) WindowBox wb = new WindowBox(确定); class WindowBox extends Frame implements ActionListener Box baseBox,boxV1,boxV2; TextField tf1,tf2,tf3; Button btn_OK; TextArea ta; WindowBox(String s)
13、super(s); tf1 = new TextField(12); tf2 = new TextField(12); tf3 = new TextField(12); boxV1 = Box.createVerticalBox(); boxV1.add(new Label(姓名); boxV1.add(Box.createVerticalStrut(8); boxV1.add(new Label(E_mail); boxV1.add(Box.createVerticalStrut(8); boxV1.add(new Label(职业); boxV2 = Box.createVerticalB
14、ox(); 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(new FlowLayout(); btn_OK = new Button(提交); btn_OK.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1