1.A
2.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class Dwindow extends Frame implements ActionListener
{ ??TextField inputNumber;
????TextArea ?save;
????Dwindow(String s)
????{ super(s);
??????inputNumber=new TextField(22);
??????inputNumber.addActionListener(this);
??????save=new TextArea(12,16);
??????setLayout(new FlowLayout());
??????add(inputNumber);
??????add(save);
??????setBounds(60,60,300,300);
??????setVisible(true);
??????validate();
??????addWindowListener(new WindowAdapter() ???
??????????????????????{ ??public void windowClosing(WindowEvent e)
???????????????????????????{ ?System.exit(0);
???????????????????????????}
??????????????????????}
???????????????????);
????}
????public void actionPerformed(ActionEvent event)
????{ ?String s=inputNumber.getText();
???????double n=0;
???????try{ ?n=Double.parseDouble(s);
????????????if(n>1000)
?????????????{ ?int select=JOptionPane.showConfirmDialog(this,"已經(jīng)超過(guò)1000確認(rèn)正確嗎?","確認(rèn)對(duì)話框",
??????????????????????????????????????????????????JOptionPane.YES_NO_OPTION );
?????????????????if(select==JOptionPane.YES_OPTION) ?
??????????????????{ ?save.append("\n"+s);
??????????????????} ?
?????????????????else ?
??????????????????{ ??inputNumber.setText(null);
??????????????????} ?
?????????????}
????????????else
?????????????{ ??save.append("\n"+s);
?????????????}
??????????}
??????catch(NumberFormatException e)
??????????{ ?JOptionPane.showMessageDialog(this,"您輸入了非法字符","警告對(duì)話框",
?????????????????????????????????????????????JOptionPane.WARNING_MESSAGE);
??????????????inputNumber.setText(null);
??????????}
???}
}
public class E
{ ??public static void main(String args[])
????{ ?new Dwindow("帶對(duì)話框的窗口");
????}
}
3.參照以下例子完成
Xiti3.java
public class Xiti3?{
???public static void main(String args[]) {
??????WindowColor win=new WindowColor();
??????win.setTitle("帶顏色對(duì)話框的窗口");
???}
}
WindowColor.java
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class WindowColor extends JFrame implements ActionListener {
???JButton button;
???WindowColor() {
??????button=new JButton("打開(kāi)顏色對(duì)話框");
??????button.addActionListener(this);
??????setLayout(new FlowLayout());
??????add(button);
??????setBounds(60,60,300,300);
??????setVisible(true);
??????setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
???}
???public void actionPerformed(ActionEvent e) {
??????Color newColor=JColorChooser.showDialog(this,"調(diào)色板",button.getBackground());
??????if(newColor!=null) {
?????????button.setBackground(newColor);
??????} ???
???}
}
1.使用FileInputStream流。
2.FileInputStream按字節(jié)讀取文件,FileReader按字符讀取文件。
3.不能。
4.使用對(duì)象流寫(xiě)入或讀入對(duì)象時(shí),要保證對(duì)象是序列化的。
5.使用對(duì)象流很容易得獲取一個(gè)序列化對(duì)象的克隆,只需將該對(duì)象寫(xiě)入到對(duì)象輸出流,那么用對(duì)象輸入流讀回的對(duì)象一定是原對(duì)象的一個(gè)克隆。
6.
import java.io.*;
public class Xiti6
{ ?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){}
????}
} ?
7.
import java.io.*;
public class Xiti7
{ ?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)
????????{ System.out.println(e);
????????} ?
???}
}
8.屬于操作題目,解答略。
9.
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class?Xiti9
{ ??public static void main(String args[])
????{ ?EWindow w=new EWindow();
???????w.validate();
????}
}
class EWindow extends Frame implements ActionListener,ItemListener
{ ?String str[]=new String[7],s;
???FileReader file;
???BufferedReader in; ?
???Button start,next;
???Checkbox checkbox[];
???TextField 題目,分?jǐn)?shù);
???int score=0;
???CheckboxGroup age=new CheckboxGroup();
???EWindow()
???{ ?super("英語(yǔ)單詞學(xué)習(xí)");
??????分?jǐn)?shù)=new TextField(10);題目=new TextField(70);
??????start=new Button("重新練習(xí)");
??????start.addActionListener(this);
??????next=new Button("下一題目");
??????next.addActionListener(this);
??????checkbox=new Checkbox[4];
??????for(int i=0;i<=3;i++)
???????{ ?checkbox[i]=new Checkbox("",false,age);
??????????checkbox[i].addItemListener(this);
???????}
??????try { ?file=new FileReader("English.txt");
?????????????in=new BufferedReader(file);
??????????}
??????catch(IOException e){} ??
??????setBounds(20,100,660,300); setVisible(true);
??????Box box=Box.createVerticalBox();
??????Panel p1=new Panel(),p2=new Panel(),
??????p3=new Panel() ,p4=new Panel(),p5=new Panel();
??????p1.add(new Label("題目:"));p1.add(題目);
??????p2.add(new Label("選擇答案:"));
??????for(int i=0;i<=3;i++)
???????????{ ?p2.add(checkbox[i]);
???????????}
??????p3.add(new Label("您的得分:"));p3.add(分?jǐn)?shù));
??????p4.add(start); p4.add(next);
??????box.add(p1);box.add(p2);box.add(p3);box.add(p4);
??????addWindowListener(new WindowAdapter()
????????????????????????{public void windowClosing(WindowEvent e)
???????????????????????????{ ?System.exit(0);
???????????????????????????}
??????
????????????????????????});
?????add(box,BorderLayout.CENTER);
?????reading();
???}
???public void reading()
???{ ??int i=0;
???????try { s=in.readLine();
?????????????if(!(s.startsWith("endend")))
?????????????????{ ?StringTokenizer tokenizer=new StringTokenizer(s,"#");
????????????????????while(tokenizer.hasMoreTokens())
????????????????????????{ ?str[i]=tokenizer.nextToken();
???????????????????????????i++;
????????????????????????}
?????????????????????題目.setText(str[0]);
?????????????????????for(int j=1;j<=4;j++)
???????????????????????{ ?checkbox[j-1].setLabel(str[j]);
???????????????????????}
??????????????????}
??????????????else if(s.startsWith("endend"))
??????????????????{ ?題目.setText("學(xué)習(xí)完畢");
?????????????????????for(int j=0;j<4;j++)
???????????????????????{ ?checkbox[j].setLabel("end");
??????????????????????????in.close();file.close();
???????????????????????}
??????????????????}
????????????}
?????????catch(Exception exp){ 題目.setText("無(wú)試題文件") ; }
???}
???public void actionPerformed(ActionEvent event)
???{ ?if(event.getSource()==start)
?????????{ ?score=0;
????????????分?jǐn)?shù).setText("得分: "+score);
????????????try { ?file=new FileReader("English.txt");
???????????????????in=new BufferedReader(file);
????????????????}
????????????catch(IOException e){} ?
????????????reading();
?????????}
??????if(event.getSource()==next)
?????????{ ?reading();
????????????for(int j=0;j<4;j++)
?????????????{ ?checkbox[j].setEnabled(true); ?
?????????????}
?????????}
???}
???public void itemStateChanged(ItemEvent e)
???{ ?for(int j=0;j<4;j++)
????????{ if(checkbox[j].getLabel().equals(str[5])&&checkbox[j].getState())
?????????????{ ?score++;
????????????????分?jǐn)?shù).setText("得分: "+score);
?????????????}
??????????checkbox[j].setEnabled(false); ?
????????}
???}
}