📄 实验四、五的代码.txt
字号:
试验四:
import java.io.*;
class student implements Serializable{
String id;
String name;
String age;
}
public class Class1
{
public static void main (String[] args)throws IOException,ClassNotFoundException
{
student s[]=new student[5];
int i;
for(i=0;i<s.length;i++)
s[i]=new student();
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
FileOutputStream out1=new FileOutputStream("student.txt");
OutputStreamWriter out2=new OutputStreamWriter(out1);
BufferedWriter out=new BufferedWriter(out2);
FileOutputStream fo=new FileOutputStream("student.dat");
ObjectOutputStream so=new ObjectOutputStream(fo);
for(i=0;i<s.length;i++)
{s[i].id=in.readLine();
s[i].name=in.readLine();
s[i].age=in.readLine();
out.write(s[i].id+" ");
out.write(s[i].name+" ");
out.write(s[i].age+" ");
out.newLine();
so.writeObject(s[i]);
}
out.close();
so.close();
FileInputStream fi=new FileInputStream("student.dat");
ObjectInputStream si=new ObjectInputStream(fi);
student stu1;
for(i=0;i<s.length;i++){
stu1=(student)si.readObject();
System.out.println("student no:"+i);
System.out.println("id:"+stu1.id);
System.out.println("name:"+stu1.name);
System.out.println("age:"+stu1.age);}
si.close();
while((i=System.in.read())!=49){};
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
实验五:
import java.awt.*;
import java.awt.event.*;
public class Class1 implements MouseMotionListener,MouseListener,WindowListener
{ private Frame f;
private TextField tf;
/**
* The main entry point for the application.
*
* @param args Array of parameters passed to the application
* via the command line.
*/
public static void main (String[] args)
{
Class1 two=new Class1();
two.go();// TODO: Add initialization code here
}
public void go()
{ f=new Frame("Two listeners example");
f.add(new Label("Click and Drag the mouse"),"North");
tf=new TextField(30);
f.add(tf,"South");
f.addMouseMotionListener(this);
f.addMouseListener(this);
f.addWindowListener(this);
f.setSize(300,200);
f.setVisible(true);
}
public void mouseDragged(MouseEvent e)
{ String s="Mouse dragging:X"+e.getX()+"Y="+e.getY();
tf.setText(s);
}
public void mouseMoved(MouseEvent e){
String s="The moving entered";
tf.setText(s);
}
public void mouseClicked(MouseEvent e){
String s="The click entered";
tf.setText(s);
}
public void mouseEntered(MouseEvent e){
String s="The mouse entered";
tf.setText(s);}
public void mouseExited(MouseEvent e){
String s="The mouse has left the building";
tf.setText(s); }
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void windowClosing(WindowEvent e){
System.exit(1);}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){
System.out.println("xsgvcbvchnv!");
}
public void windowDeiconified(WindowEvent e){
System.out.println("123456789!");
}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
对话框:
import java.awt.*;
import java.awt.event.*;
class Handler implements ActionListener {
Dialog d;
public void actionPerformed(ActionEvent e){
if (e.getActionCommand()=="打开对话框")
d.setVisible(true);
else
d.setVisible(false);
}
}
public class Class1
{
public static void main (String[] args)
{
Frame f=new Frame();
Handler h=new Handler();
Dialog d=new Dialog(f,"Dialog",false);
d.add("Center",new Label("Hello,I am a Dialog!"));
d.setSize(100,100);
//d.pack();
//d.setVisible(true);
Button b1=new Button("打开对话框");
Button b2=new Button("关闭对话框");
f.add("North",b1);
d.add("North",b2);
h.d=d;
b1.addActionListener(h);
b2.addActionListener(h);
f.setSize(200,100);
f.show();
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
菜单:
import java.awt.*;
import java.awt.event.*;
//菜单无法进行布局管理,只能出现在窗口的固定位置
//在菜单中真正产生动作的是菜单项
public class Class1
{
public static void main (String[] args)
{
Frame f=new Frame("菜单条");
MenuBar mb=new MenuBar();//菜单条无法单独显示出来,用于管理菜单
Menu m1=new Menu("文件");
Menu m2=new Menu("编辑");
Menu m3=new Menu("帮助");
MenuItem mi1=new MenuItem("打开文件");
CheckboxMenuItem mi2=new CheckboxMenuItem("CheckItem");
m1.add(mi1);//在m1菜单上添加mi1菜单项
m1.add(mi2);
mb.add(m1);//将菜单添加到菜单条上
mb.add(m2);
mb.add(m3);
Handler handler=new Handler();
handler.f=f;
mi1.addActionListener(handler);
Handler1 handler1=new Handler1();
mi2.addItemListener(handler1);
f.setMenuBar(mb);//注意并没有使用f.add方法
f.setSize(180,180);
f.show();
}
}
class Handler implements ActionListener {
Frame f;
public void actionPerformed(ActionEvent e){
FileDialog fd=new FileDialog(f,"Dialog");
fd.setVisible(true);
}
}
class Handler1 implements ItemListener{
public void itemStateChanged(ItemEvent e){
System.out.println(e.getItem()+" "+e.getStateChange() );
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
期中考试:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Class1
{
public static void main (String[] args)
{
Frame f=new Frame("菜单条");
Student student[]=new Student[10];
int i;
for (i=0;i<student.length;i++)
{
student[i]=new Student();
student[i].name=new String();
student[i].sex=new String();
student[i].no=new String();
}
MenuBar mb=new MenuBar();//菜单条无法单独显示出来,用于管理菜单
Menu m1=new Menu("文件");
Menu m2=new Menu("输入");
MenuItem mi1=new MenuItem("打开文件");
MenuItem mi2=new MenuItem("保存数据");
MenuItem mi3=new MenuItem("数据输入");
m1.add(mi1);//在m1菜单上添加mi1菜单项
m1.add(mi2);
m2.add(mi3);
mb.add(m1);//将菜单添加到菜单条上
mb.add(m2);
Handler handler=new Handler();
handler.f=f;
handler.stu=student;
mi1.addActionListener(handler);
mi2.addActionListener(handler);
mi3.addActionListener(handler);
f.setMenuBar(mb);//注意并没有使用f.add方法
f.setSize(280,280);
f.show();
}
}
class Handler implements ActionListener {
Frame f;
Student stu[];
public void actionPerformed(ActionEvent e){
if (e.getActionCommand()=="打开文件")
{
FileDialog fd=new FileDialog(f,"Dialog");
fd.setVisible(true);
System.out.println(stu[0].name+" " +stu[0].sex);
}
else if (e.getActionCommand()=="保存数据")
{
FileDialog fd=new FileDialog(f,"Dialog");
fd.setVisible(true);
try
{
FileOutputStream out =new FileOutputStream(fd.getFile()) ;
OutputStreamWriter out1=new OutputStreamWriter(out);
int i;
for (i=0;i<stu.length-1;i++)
{
out1.write(stu[i].name,0,stu[i].name.length());
out1.write(stu[i].sex,0,stu[i].sex.length());
out1.write(stu[i].no,0,stu[i].no.length());
out1.write('\r');
out1.write('\n');
System.out.println(stu[i].name+stu[i].sex+stu[i].no);
}
out1.close();
out.close();
}
catch(IOException ioe)
{
System.out.println(ioe);
}
}
else if (e.getActionCommand()=="数据输入")
{
Dialog d=new Dialog(f,"数据输入",true);
TextField tf1,tf2,tf3;
Button b1,b2,b3,b4;
d.setLayout(new GridLayout(5,2));
d.add(new Label("姓名"));
d.add(tf1=new TextField(8));
d.add(new Label("性别"));
d.add(tf2=new TextField("男",1));
d.add(new Label("学号"));
d.add(tf3=new TextField(8));
d.add(b1=new Button("前一条"));
d.add(b2=new Button("后一条"));
d.add(b3=new Button("确定"));
d.add(b4=new Button("取消"));
Handler1 handler1=new Handler1();
b1.addActionListener(handler1);
b2.addActionListener(handler1);
b3.addActionListener(handler1);
b4.addActionListener(handler1);
handler1.d=d;
handler1.stu=stu;
handler1.num=0;
handler1.max=0;
handler1.tf1=tf1;
handler1.tf2=tf2;
handler1.tf3=tf3;
d.pack();
d.setVisible(true);
}
}
}
class Handler1 implements ActionListener {
Dialog d;
TextField tf1,tf2,tf3;
Student stu[];
static int num,max;
public void actionPerformed(ActionEvent e){
if (e.getActionCommand()=="后一条" && num < stu.length-1 )
{
stu[num].name=tf1.getText();
stu[num].sex=tf2.getText();
stu[num].no=tf3.getText();
if (num >=max)
{
tf1.setText("");
tf2.setText("男");
tf3.setText("");
num+=1;
max+=1;
}
else
{
num+=1;
tf1.setText(stu[num].name);
tf2.setText(stu[num].sex);
tf3.setText(stu[num].no);
}
}
else if (e.getActionCommand()=="确定")
{
stu[num].name=tf1.getText();
stu[num].sex=tf2.getText();
stu[num].no=tf3.getText();
d.setVisible(false);
}
}
}
class Student{
String name;
String sex;
String no;
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
P206:
例13-9:
import java.awt.*;
import java.applet.*;
import java.awt.image.*;
import java.awt.event.*;
/**
* This class reads PARAM tags from its HTML host page and sets
* the color and label properties of the applet. Program execution
* begins with the init() method.
*/
public class Applet1 extends Applet implements MouseListener
{
Image img;
Image newImg;
int x1,y1,w,h,count=1;
public void init()
{
img=getImage(getDocumentBase(),"bg.jpg");
newImg=img;
this.addMouseListener(this);
}
public void paint(Graphics g){
int w=newImg.getWidth(this);//this 为图像观测者
int h=newImg.getHeight(this);
Dimension d=getSize();
if(w>d.width ) w=d.width;
if(h>d.height) h=d.height ;
//g.drawImage(newImg,0,0,this);
g.drawImage(newImg,(d.width-w)/2,(d.height-h)/2,this);//居中显示,但有误差
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mousePressed(MouseEvent e){
int x,y;
Graphics g=getGraphics();
x=e.getX();
y=e.getY();
if (count==1){
x1=x;y1=y;count=2;//得到要截取区域的左上角坐标
g.drawLine(x-5,y,x+5,y);
g.drawLine(x,y-5,x,y+5);
}else if(count==2){
count=3;
if(x1>x)w=x1-x;//得到要截取区域的宽度与高度
else w=x-x1;
if(y1>y) h=y1-y;
else h=y-y1;
newImg=crop();
repaint();
}else{
count=1;
newImg=img;
repaint();
}
g.drawString("x1="+x,10,10);
}
public void mouseReleased(MouseEvent e){
Graphics g=getGraphics();
g.drawString("x2="+e.getX(),28,28);
}
Image crop(){
Dimension d=getSize();
ImageFilter filter=new CropImageFilter(x1,y1,w,h);//截取时是按照图像的坐标
ImageProducer producer =new FilteredImageSource(img.getSource(),filter);
return createImage(producer);
// return createImage(img.getSource());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -