《Java2实用教程》课后习题参考答案 下载本文

button=new Button(\确定\add(text1,BorderLayout.EAST); add(text2,BorderLayout.WEST); add(button,BorderLayout.SOUTH); button.addActionListener(this); addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0); } } );

text2.setEditable(false); setBounds(100,100,350,200); setVisible(true); validate(); }

public void actionPerformed(ActionEvent e) {

if(e.getSource()==button) {

String s=text1.getSelectedText()+\String str=text2.getText(); text2.setText(str+s); } } }

public class Test {

public static void main(String args[]) {

new WindowSelectedText(\挑单词\} }

6. 编写一个应用程序,有一个标题为“计算”的窗口,窗口的布局为FlowLayou t 布局。设计4 个按钮, 分别命名为“加”、“差”、“积”、“除”,另外,窗口中还有3 个文本框。单击相应的按钮,将两个文本

框的数字做运算,在第三个文本框中显示结果。要求处理NumberForma tException。 答: import java.awt.*; import java.awt.event.*;

class Calculated extends Frame implements ActionListener {

TextField text1,text2,text3; //3 个文本框

Button buttonH,buttonC,buttonJ,buttonS; //4 个按钮 Calculated(String s) { super(s);

setLayout(new FlowLayout()); //FlowLayout 布局 text1=new TextField(10); text2=new TextField(10); text3=new TextField(17); buttonH=new Button(\加\buttonC=new Button(\差\buttonJ=new Button(\积\buttonS=new Button(\除\add(text1); add(text2); add(text3);

text3.setEditable(false); add(buttonH); add(buttonC); add(buttonJ); add(buttonS);

buttonH.addActionListener(this); buttonC.addActionListener(this); buttonJ.addActionListener(this); buttonS.addActionListener(this); addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0); } } );

setBounds(100,100,160,180); setVisible(true); validate(); }

public void actionPerformed(ActionEvent e) {

double num1=0,num2=0,totle=0; try {

num1= Double.parseDouble(text1.getText()); num2= Double.parseDouble(text2.getText()); if(e.getSource()==buttonH) {

totle=num1+num2;

text3.setText(\和\}

else if(e.getSource()==buttonC) {

totle=num1-num2;

text3.setText(\差\}

else if(e.getSource()==buttonJ) {

totle=num1*num2;

text3.setText(\积\}

else if(e.getSource()==buttonS) {

totle=num1/num2;

text3.setText(\商\} }

catch(NumberFormatException event) {

text3.setText(\请输入数字字符!\} } }

public class Test {

public static void main(String args[]) {

Calculated calc=new Calculated(\计算\标题为“计算”的窗口 } }

7. 改进例子7.16,在程序中增加一个名称为“确定”的按钮和一个文本区。当单击按钮时,程序验证用 户是否输入了合法的E-mai l 地址格式,如果合法就将用户输入的姓名、E-mai l 和职业尾加到文本区中, 否则在输入E-mai l 的文本框中提示用户输入了非法格式的E-mai l 地址。 答: import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*;

class WindowBox extends Frame implements ActionListener {

TextField text1,text2,text3; TextArea textarea; Box baseBox,boxV1,boxV2; Button button; WindowBox() {

button=new Button(\确定\textarea=new TextArea(6,12); text1=new TextField(12); text2=new TextField(12); text3=new TextField(12); boxV1=Box.createVerticalBox(); boxV1.add(new Label(\姓名\

boxV1.add(Box.createVerticalStrut(8)); boxV1.add(new Label(\boxV1.add(Box.createVerticalStrut(8)); boxV1.add(new Label(\职业\boxV2=Box.createVerticalBox(); boxV2.add(text1);

boxV2.add(Box.createVerticalStrut(8)); boxV2.add(text2);

boxV2.add(Box.createVerticalStrut(8));

boxV2.add(text3);

baseBox=Box.createHorizonta lBox(); baseBox.add(boxV1);

baseBox.add(Box.createHorizonta lStrut(10)); baseBox.add(boxV2);

setLayout(new FlowLayout()); add(baseBox); add(button); add(textarea);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0); } } );

textarea.setEditable(false); button.addActionListener(this); setBounds(100,100,210,250); setVisible(true); validate(); }

public void actionPerformed(ActionEvent e) {

boolean a; int b; String s;

if(e.getSource()==button) {

s=text2.getText(); a=s.endsWith(\b=s.indexOf(\if(a&&b>0) {

String str1=text1.getText()+\String str2=textarea.getText(); textarea.setText(str2+str1);