一、問答題
1.Frame容器的默認布局是BorderLayout布局。
2.不可以。
3.ActionEvent。
4.DocumentEvent。
5.5個。
6.MouseMotionListener。
二、選擇題
1.C。2.A。3.A。4.D。5.C。
三、編程題
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("[^0123456789.]+");
??????sum=0;
??????aver=0; ?
??????for(int i=0;i<a.length;i++) {
????????try { sum=sum+Double.parseDouble(a[i]);
????????}
????????catch(Exception ee) {}
?????}
?????aver=sum/count;
?????text2.setText(null);
?????text2.append("\n和:"+sum);
?????text2.append("\n平均值:"+aver);
???}
???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(" ",JLabel.CENTER);
???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);
???add(buttonMul);
???add(buttonDiv);
???buttonAdd.addActionListener(this);
???buttonSub.addActionListener(this);
???buttonMul.addActionListener(this); ?
???buttonDiv.addActionListener(this);
???setSize(300,320);
???setVisible(true);
???validate();
???setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
??} ??
??public void actionPerformed(ActionEvent e) {
????double n;
????if(e.getSource()==buttonAdd) {
???????double n1,n2; ?
???????try{ n1=Double.parseDouble(text1.getText());
????????????n2=Double.parseDouble(text2.getText());
????????????n=n1+n2;
????????????text3.setText(String.valueOf(n));
????????????label.setText("+");
??????????}
???????catch(NumberFormatException ee)
??????????{ text3.setText("請輸入數(shù)字字符");
??????????}
?????}
????else if(e.getSource()==buttonSub) {
???????double n1,n2; ?
???????try{ ?n1=Double.parseDouble(text1.getText());
????????????n2=Double.parseDouble(text2.getText());
????????????n=n1-n2;
????????????text3.setText(String.valueOf(n));
????????????label.setText("-");
??????????}
???????catch(NumberFormatException ee)
??????????{ text3.setText("請輸入數(shù)字字符");
??????????}
?????}
?????else if(e.getSource()==buttonMul)
??????{double n1,n2; ?
???????try{ n1=Double.parseDouble(text1.getText());
????????????n2=Double.parseDouble(text2.getText());
????????????n=n1*n2;
????????????text3.setText(String.valueOf(n));
????????????label.setText("*");
??????????}
???????catch(NumberFormatException ee)
??????????{ text3.setText("請輸入數(shù)字字符");
??????????}
??????}
??????else if(e.getSource()==buttonDiv)
??????{double n1,n2; ?
???????try{ n1=Double.parseDouble(text1.getText());
????????????n2=Double.parseDouble(text2.getText());
????????????n=n1/n2;
????????????text3.setText(String.valueOf(n));
????????????label.setText("/");
??????????}
???????catch(NumberFormatException ee)
??????????{ text3.setText("請輸入數(shù)字字符");
??????????}
??????}
?????validate();
??}
}
3.???import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class E {
???public static void main(String args[]){
??????Window win = new Window();
??????win.setTitle("使用MVC結構");
??????win.setBounds(100,100,420,260);
???}
}
class Window extends JFrame implements ActionListener {
???Lader lader; ????????????//模型
???JTextField textAbove,textBottom,textHeight; ??//視圖
???JTextArea showArea; ????????//視圖
???JButton controlButton; ???????//控制器
???Window() {
??????init();
??????setVisible(true);
??????setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
???}
???void init() {
?????lader = new Lader();
?????textAbove = new JTextField(5); ??
?????textBottom = new JTextField(5);
?????textHeight = new JTextField(5);
?????showArea = new JTextArea(); ???
?????controlButton=new JButton("計算面積");
?????JPanel pNorth=new JPanel();
?????pNorth.add(new JLabel("上底:"));
?????pNorth.add(textAbove);
?????pNorth.add(new JLabel("下底:"));
?????pNorth.add(textBottom);
?????pNorth.add(new JLabel("高:"));
?????pNorth.add(textHeight);
?????pNorth.add(controlButton);
?????controlButton.addActionListener(this);
?????add(pNorth,BorderLayout.NORTH);
?????add(new JScrollPane(showArea),BorderLayout.CENTER);
???}
???public void actionPerformed(ActionEvent e) {
?????try{ ?
????????double above = Double.parseDouble(textAbove.getText().trim());
????????double bottom = Double.parseDouble(textBottom.getText().trim());
????????double height = Double.parseDouble(textHeight.getText().trim());
????????lader.setAbove(above) ; ?????????
????????lader.setBottom(bottom);
????????lader.setHeight(height);
????????double area = lader.getArea(); ????
????????showArea.append("面積:"+area+"\n");
?????}
?????catch(Exception ex) {
????????showArea.append("\n"+ex+"\n");
?????}
???}
}
class Lader {
????double above,bottom,height;
????public double getArea() {
???????double area = (above+bottom)*height/2.0;
???????return area;
????}
????public void setAbove(double a) {
??????above = a;
????}
???public void setBottom(double b) {
??????bottom = b;
???}
???public void setHeight(double c) {
?????height = c;
???}
}
一、問答題
1.使用FileInputStream。
2.FileInputStream按字節(jié)讀取文件,FileReader按字符讀取文件。
3.不可以。
4.使用對象流寫入或讀入對象時,要保證對象是序列化的。
5.使用對象流很容易得獲取一個序列化對象的克隆,只需將該對象寫入到對象輸出流,那么用對象輸入流讀回的對象一定是原對象的一個克隆。
二、選擇題
1.C。2.B。
三、閱讀程序
1.【代碼1】:51?!敬a2】:0。
2.【代碼1】:3?!敬a2】:abc。【代碼3】:1。【代碼4】:dbc。
3.上機實習題,解答略。
四、編程題
1. import java.io.*;
public class E {
???public static void main(String args[]) {
???????File f=new File("E.java");;
???????try{ ??RandomAccessFile random=new RandomAccessFile(f,"rw");
??????????????random.seek(0);
??????????????long m=random.length();
??????????????while(m>=0) {
??????????????????m=m-1;
??????????????????random.seek(m);
??????????????????int c=random.readByte();
??????????????????if(c<=255&&c>=0)
?????????????????????System.out.print((char)c);
??????????????????else {
????????????????????m=m-1;
????????????????????random.seek(m);
????????????????????byte cc[]=new byte[2];
????????????????????random.readFully(cc);
????????????????????System.out.print(new String(cc));
??????????????????}
??????????????}
???????}
???????catch(Exception exp){}
????}
}
2. ??import java.io.*;
public class E {
???public static void main(String args[ ]) {
??????File file=new File("E.java");
??????File tempFile=new File("temp.txt");
??????try{ FileReader ?inOne=new FileReader(file);
???????????BufferedReader inTwo= new BufferedReader(inOne);
???????????FileWriter ?tofile=new FileWriter(tempFile);
???????????BufferedWriter out= new BufferedWriter(tofile);
???????????String s=null;
???????????int i=0;
???????????s=inTwo.readLine();
???????????while(s!=null) {
???????????????i++;
???????????????out.write(i+" "+s);
???????????????out.newLine();
???????????????s=inTwo.readLine();
???????????}
???????????inOne.close();
???????????inTwo.close();
???????????out.flush();
???????????out.close();
???????????tofile.close();
??????}
??????catch(IOException e){}
???}
}
3.???import java.io.*;
import java.util.*;
public class E {
???public static void main(String args[]) {
??????File file = new File("a.txt");
??????Scanner sc = null;
??????double sum=0;
??????int count = 0;
??????try { sc = new Scanner(file);
????????????sc.useDelimiter("[^0123456789.]+");
????????????while(sc.hasNext()){
???????????????try{ ?double price = sc.nextDouble();
????????????????????count++;
????????????????????sum = sum+price;
????????????????????System.out.println(price);
???????????????}
???????????????catch(InputMismatchException exp){
????????????????????String t = sc.next();
???????????????} ??
????????????}
????????????System.out.println("平均價格:"+sum/count);
??????}
??????catch(Exception exp){
?????????System.out.println(exp);
??????}
???}
}