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,"已经超过1000确认正确吗?","确认对话框",
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,"您输入了非法字符","警告对话框",
JOptionPane.WARNING_MESSAGE);
inputNumber.setText(null);
}
}
}
public class E
{ public static void main(String args[])
{ new Dwindow("带对话框的窗口");
}
}
3.参照以下例子完成
Xiti3.java
public class Xiti3 {
public static void main(String args[]) {
WindowColor win=new WindowColor();
win.setTitle("带颜色对话框的窗口");
}
}
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("打开颜色对话框");
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,"调色板",button.getBackground());
if(newColor!=null) {
button.setBackground(newColor);
}
}
}
1.使用FileInputStream流。
2.FileInputStream按字节读取文件,FileReader按字符读取文件。
3.不能。
4.使用对象流写入或读入对象时,要保证对象是序列化的。
5.使用对象流很容易得获取一个序列化对象的克隆,只需将该对象写入到对象输出流,那么用对象输入流读回的对象一定是原对象的一个克隆。
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 题目,分数;
int score=0;
CheckboxGroup age=new CheckboxGroup();
EWindow()
{ super("英语单词学习");
分数=new TextField(10);题目=new TextField(70);
start=new Button("重新练习");
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(分数);
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("学习完毕");
for(int j=0;j<4;j++)
{ checkbox[j].setLabel("end");
in.close();file.close();
}
}
}
catch(Exception exp){ 题目.setText("无试题文件") ; }
}
public void actionPerformed(ActionEvent event)
{ if(event.getSource()==start)
{ score=0;
分数.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++;
分数.setText("得分: "+score);
}
checkbox[j].setEnabled(false);
}
}
}