java复习资料.docx
《java复习资料.docx》由会员分享,可在线阅读,更多相关《java复习资料.docx(39页珍藏版)》请在冰豆网上搜索。
java复习资料
第六章
1、会写基本的输入输出流
2、字符流、字节流有关于file的类
第七章
1、数组的声明,初始化
2、对数组元素排序
3、集合主要是list接口中的arraylist的用法
4、增强for循环
第八章
1、线程同步问题(synchronized用法)
2、死锁问题的解决
3、NotifyP258
第九章
1、Swing特点Swing组件和容器层次
2、能区分顶层容器、中间容器、原子组件及各部分包含类方法
3、布局管理(borderlayoutflowlayoutgridlayout)
4、事件事件监听器P299
5、熟悉图形用户界面所需各类原子组件及常用的类方法
第10章(可能有大题)
1、JDBC数据库访问的操作步骤,老师上课讲的那几部要背会的代码
2、能通过JDBC访问数据库(书上比较完整的参照程序P356)
packagea;
//正确的输出使用
importjava.io.*;
publicclassI1
{
publicstaticvoidmain(Stringargs[])
{
PrintWritercout=newPrintWriter(System.out,true);
cout.println("helloworld");
//System.out.println("ee");
//cout.write("hello");
//cout.flush();
cout.close();
}
}
packagea;
//显示输入的字符串
importjava.io.*;
publicclassI2
{
InputStreamReaderisr;
BufferedReaderbr;
Stringstr;
publicstaticvoidmain(Stringargs[])
{
I2me=newI2();
me.a();
}
publicvoida()
{
br=newBufferedReader(
newInputStreamReader(
System.in));
try
{
str=br.readLine();
}
catch(Exceptione)
{
System.out.println(e);
}
finally
{
System.out.println(str);
}
try{
br.read();
}
catch(Exceptionee)
{
System.exit
(1);
}
}
}
packagea;
importjava.io.*;
publicclassI4
{
publicstaticvoidmain(Stringargs[])
{
FileInputStreamfis;
FileOutputStreamfos;
PrintWriterpw;
byte[]buffer=newbyte[512];
intcount=0;
try
{
fis=newFileInputStream("I4.java");
fos=newFileOutputStream("a.txt");
pw=newPrintWriter(System.out);
while((count=fis.read(buffer))!
=-1)
{
pw.print(newString(buffer,0,count));
fos.write(buffer,0,count);
}
pw.close();
fis.close();
fos.close();
}
catch(FileNotFoundExceptione)
{System.out.println(e);}
catch(IOExceptionee)
{System.out.println(ee);}
catch(Exceptioneee)
{System.out.println(eee);}
}
}
///
packagea;
importjava.io.*;
publicclassStudentimplementsSerializable
{
intid;//学号
Stringname;//姓名
intage;//年龄
Stringdepartment;//系别
publicStudent(intid,Stringname,intage,Stringdepartment)
{
this.id=id;
this.name=name;
this.age=age;
this.department=department;
}
publicStudent(){}
publicStringtoString()
{
return("id:
"+id+"\nname:
"+name+"\nage:
"+age+"\ndep:
"+department);
}
publicstaticvoidmain(Stringargs[])throwsIOException,ClassNotFoundException
{
ObjectInputStreamois;
ObjectOutputStreamoos;
Studentme=newStudent(1,"Tom",24,"cst");
oos=newObjectOutputStream(
newFileOutputStream("Student.txt"));
oos.writeObject(me);
oos.close();
ois=newObjectInputStream(
newFileInputStream("Student.txt"));
Studenthe=(Student)ois.readObject();
System.out.println(he);
}
}
packagea;
importjava.io.Serializable;
publicclassTTTimplementsSerializable{
}
packageg;
//布局管理CardLayout
importjava.awt.*;
importjava.awt.event.*;
publicclassMyLayout4extendsWindowAdapter
{
@Override
publicvoidwindowClosing(WindowEventarg0){
System.exit(0);
}
Framef;
CardLayoutcl;
publicstaticvoidmain(Stringargs[])
{
MyLayout4me=newMyLayout4();
me.display();
}
publicvoiddisplay()
{
cl=newCardLayout();
f=newFrame("CardLayout");
Panelp1=newPanel();
Panelp2=newPanel();
Buttonb1=newButton("panel1");
Labell1=newLabel("panel1");
p1.add(l1);
p1.add(b1);
Buttonb2=newButton("panel2");
Labell2=newLabel("panel2");
p2.add(l2);
p2.add(b2);
p2.setBackground(Color.RED);
f.setLayout(cl);
f.add("panel1",p1);
f.add("panel2",p2);
f.setSize(400,300);
f.addWindowListener(this);
b1.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
if(e.getActionCommand()=="panel1")
show2();
}
}
);
b2.addActionListener(newActionListener()
{
publicvoidactionPerformed(ActionEvente)
{
if(e.getActionCommand()=="panel2")
show1();
}
}
);
f.setVisible(true);
}
privatevoidshow1()
{
cl.show(f,"panel1");
}
privatevoidshow2()
{
cl.show(f,"panel2");
}
}
packageg;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseMotionAdapter;
importjava.awt.event.MouseMotionListener;
importjava.awt.event.WindowListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
publicclassT1{
JFramef;
JButtonb;
publicvoiddisp(){
}
publicvoidinit(){
f=newJFrame();
f.setSize(400,300);
b=newJButton("clickme");
b.addMouseMotionListener(newMouseMotionAdapter(){
@Override
publicvoidmouseMoved(MouseEventarg0){
System.out.println(arg0.getX()+","+arg0.getY());
}
});
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
publicstaticvoidmain(String[]args){
T1t=newT1();
t.init();
}
}
packagegg;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
publicclassCimplementsActionListener{
@Override
publicvoidactionPerformed(ActionEventarg0){
if(arg0.getActionCommand().equals("abc")){
M.runa();
}
if(arg0.getActionCommand().equals("b")){
M.runb();
}
}
}
packagegg;
publicclassM{
publicstaticvoidruna(){
System.out.println("doworka");
}
publicstaticvoidrunb(){
System.out.println("doworkb");
}
}
packagegg;
publicclassMain{
publicstaticvoidmain(String[]args){
Vv=newV();
v.init();
}
}
packagegg;
importjava.awt.FlowLayout;
importjavax.swing.JButton;
importjavax.swing.JFrame;
publicclassV{
JFramef;
JButtonb1;
JButtonb2;
FlowLayoutlay;
publicvoidinit(){
f=newJFrame();
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lay=newFlowLayout();
f.setLayout(lay);
b1=newJButton();
b1.setActionCommand("abc");
b1.setText("xyz");
b2=newJButton("b");
f.add(b1);
f.add(b2);
Cc=newC();
b1.addActionListener(c);
b2.addActionListener(c);
f.setVisible(true);
}
}
packageh;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.util.StringTokenizer;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclassCimplementsActionListener{
JTextFieldtf;
JTextAreata;
publicC(JTextFieldtf,JTextAreata){
this.ta=ta;
this.tf=tf;
}
@Override
publicvoidactionPerformed(ActionEventarg0){
Stringstr=ta.getText();
intsum=0;
StringTokenizertoken=newStringTokenizer(str);
while(token.hasMoreTokens()){
sum+=Integer.parseInt(token.nextToken());
}
tf.setText(sum+"");
}
}
packageh;
publicclassMain{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
Vv=newV();
v.init();
}
}
packageh;
importjava.awt.FlowLayout;
importjava.awt.Font;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
publicclassV{
JFramef;
JButtonb;
JTextFieldtf;
JTextAreata;
publicvoidinit(){
f=newJFrame();
b=newJButton("计算");
tf=newJTextField(10);
ta=newJTextArea(3,10);
tf.setFont(newFont(null,Font.PLAIN,25));
ta.setFont(newFont(null,Font.PLAIN,25));
f.setLayout(newFlowLayout());
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(ta);
f.add(tf);
f.add(b);
Cc=newC(tf,ta);
b.addActionListener(c);
f.setVisible(true);
}
}
Javavch10
StuDao.java
packageb;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.SQLException;
importjava.text.SimpleDateFormat;
publicclassStuDao{
publicstaticvoidaddStu(Stus){
try{
Connectionconn=DbUtil.getConnection();
conn.setAutoCommit(false);
Stringsql="insertintot_stu(username,password,height,birthday)values(?
?
?
?
)";
PreparedStatementps=conn.prepareStatement(sql);
ps.setString(1,s.getUsername());
ps.setString(2,s.getPassword());
ps.setDouble(3,s.getHeight());
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddHH:
mm:
ss");
ps.setString(4,sdf.format(s.getBirthday()));
ps.executeUpdate();
mit();
DbUtil.closeConnection(conn);
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
}
Stu.java
packageb;
importjava.util.Date;
publicclassStu{
privateintid;
privateStringusername;
privateStringpassword;
privatedoubleheight;
privateDatebirthday;
publicintgetId(){
returnid;
}
publicvoidsetId(intid){
this.id=id;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicdoublegetHeight(){
returnheight;
}
publicvoidsetHeight(doubleheight){
this.height=height;
}
publicDategetBirthday(){
returnbirthday;
}
publicvoidsetBirthday(Datebirthday){
this.birthday=birthday;
}
}
packageb;
importjava.util.Date;
publicclassMain{
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
Stus=newStu();
s.setBirthday(newDate());
s.setHeight(1.56);
s.setPassword("789");
s.setUsername("mearry");
StuDao.addStu(s);
}
}
packageb;
importjava.sql.Connect