(完整word版)Java-2实用教程(第5版)习题解答 联系客服

发布时间 : 星期四 文章(完整word版)Java-2实用教程(第5版)习题解答更新完毕开始阅读ca8c170950d380eb6294dd88d0d233d4b04e3f50

System.out.println(c); c=Math.exp(1); System.out.println(c); c=Math.log(8); System.out.println(c); } }

5.public class E {

public static void main (String args[ ]) { String str = \你是谁?\ String regex = \ str = str.replaceAll(regex,\ System.out.println(str); }

}

6. import java.util.*;

public class E {

public static void main(String args[]) {

String cost = \数学87分,物理76分,英语96分\ Scanner scanner = new Scanner(cost); scanner.useDelimiter(\ double sum=0; int count =0;

while(scanner.hasNext()){

try{ double score = scanner.nextDouble(); count++; sum = sum+score; System.out.println(score); }

catch(InputMismatchException exp){ String t = scanner.next(); } }

System.out.println(\总分:\分\ System.out.println(\平均分:\分\ } }

习题9(第9章)

一、问答题

1.JFrame类的对象的默认布局是什么布局?

2.一个容器对象是否可以使用add方法添加一个JFrame窗口? 3.JTextField可以触发什么事件?

4.JTextArea中的文档对象可以触发什么类型的事件? 5.MouseListener接口中有几个方法?

6.处理鼠标拖动触发的MouseEvent事件需使用哪个接口?

1.Frame容器的默认布局是BorderLayout布局。 2.不可以。

3.ActionEvent。 4.DocumentEvent。 5.5个。

6.MouseMotionListener。 二、选择题

1.下列哪个叙述是不正确的?c

A.一个应用程序中最多只能有一个窗口。 B.JFrame创建的窗口默认是不可见的。

C.不可以向JFrame窗口中添加JFame窗口。

D.窗口可以调用setTitle(String s)方法设置窗口的标题。 2.下列哪个叙述是不正确的?a

A.JButton对象可以使用使用addActionLister(ActionListener l)方法将没有实现ActionListener接口的类的实例注册为自己的监视器。

B.对于有监视器的JTextField文本框,如果该文本框处于活动状态(有输入焦点)时,用户即使不输入文本,只要按回车(Enter)键也可以触发ActionEvent事件 C.监视KeyEvent事件的监视器必须实现KeyListener接口。

D.监视WindowEvent事件的监视器必须实现WindowListener接口。 3.下列哪个叙述是不正确的?a

A.使用FlowLayout布局的容器最多可以添加5个组件。 B.使用BorderLayout布局的容器被划分成5个区域。 C.JPanel的默认布局是FlowLayout布局。 D.JDialog的默认布局是BorderLayout布局。 1.C。2.A。3.A。4.D。5.C。 三、编程题

1.编写应用程序,有一个标题为“计算”的窗口,窗口的布局为FlowLayout布局。窗口中添加两个文本区,当我们在一个文本区中输入若干个数时,另一个文本区同时对你输入的数进行求和运算并求出平均值,也就是说随着你输入的变化,另一个文本区不断地更新

求和及平均值。

2.编写一个应用程序,有一个标题为“计算”的窗口,窗口的布局为FlowLayout布局。设计四个按钮,分别命名为“加”、“差”、“积、”、“除”,另外,窗口中还有三个文本框。单击相应的按钮,将两个文本框的数字做运算,在第三个文本框中显示结果。要求处理NumberFormatException异常。

3.参照例子15编写一个体现MVC结构的GUI程序。首先编写一个封装梯形类,然后再编写一个窗口。要求窗口使用三文本框和一个文本区为梯形对象中的数据提供视图,其中三个文本框用来显示和更新梯形对象的上底、下底和高;文本区对象用来显示梯形的面积。窗口中有一个按钮,用户单击该按钮后,程序用3个文本框中的数据分别作为梯形对象的上底、下底和高,并将计算出的梯形的面积显示在文本区中。 1. import java.awt.*;

import javax.swing.event.*; import javax.swing.*; import java.awt.event.*; public class E {

public static void main(String args[]) { Computer fr=new Computer(); } }

class Computer extends JFrame implements DocumentListener { JTextArea text1,text2; int count=1;

double sum=0,aver=0; Computer() {

setLayout(new FlowLayout()); text1=new JTextArea(6,20); text2=new JTextArea(6,20); add(new JScrollPane(text1)); add(new JScrollPane(text2)); text2.setEditable(false);

(text1.getDocument()).addDocumentListener(this); setSize(300,320); setVisible(true); validate();

setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); }

public void changedUpdate(DocumentEvent e) { String s=text1.getText();

String []a =s.split(\ sum=0; aver=0;

for(int i=0;i

try { sum=sum+Double.parseDouble(a[i]); }

catch(Exception ee) {} }

aver=sum/count; text2.setText(null); text2.append(\和:\ text2.append(\平均值:\ }

public void removeUpdate(DocumentEvent e){ changedUpdate(e); }

public void insertUpdate(DocumentEvent e){ changedUpdate(e); } }

2. import java.awt.*;

import javax.swing.event.*; import javax.swing.*; import java.awt.event.*; public class E {

public static void main(String args[]) { ComputerFrame fr=new ComputerFrame(); } }

class ComputerFrame extends JFrame implements ActionListener { JTextField text1,text2,text3;

JButton buttonAdd,buttonSub,buttonMul,buttonDiv; JLabel label;

public ComputerFrame() { setLayout(new FlowLayout()); text1=new JTextField(10); text2=new JTextField(10); text3=new JTextField(10);

label=new JLabel(\ label.setBackground(Color.green); add(text1); add(label); add(text2); add(text3);

buttonAdd=new JButton(\加\ buttonSub=new JButton(\减\ buttonMul=new JButton(\乘\ buttonDiv=new JButton(\除\ add(buttonAdd); add(buttonSub);