导航:首页 > 编程语言 > 100个java经典编程实例源代码

100个java经典编程实例源代码

发布时间:2023-06-17 14:44:52

java100行以上源代码,至少五个class以及一个interface,可以简单点

下面是一个可能的Java源代码,它包含了一个接口租冲薯(Shape)和五个类(Circle, Rectangle, Triangle, Square 和 Main)。它的功能是计算不同形状的面积和周长。
//定义一个接口Shape,有两判指个抽象方法:getArea()和getPerimeter()interface Shape { double getArea(); double getPerimeter();
}//定义一个类Circle,实现Shape接口class Circle implements Shape { //定义一个私有属性radius,表示圆的半径
private double radius; //定义一个公有构造方法,用于初始化radius
public Circle(double radius) { this.radius = radius;
} //实现getArea()方法,返回圆的面积
public double getArea() { return Math.PI * radius * radius;
} //实现getPerimeter()方法,返回圆的周长
public double getPerimeter() { return Math.PI * radius * 2;
}
}//定义一个类Rectangle,实现Shape接口class Rectangle implements Shape { //定义两个私有属性width和height,表示矩形的宽度和高度
private double width; private double height; //定义一个公有构造方法,用于初始化width和height
public Rectangle(double width, double height) { this.width = width; this.height = height;
} //实现getArea()方法,返回矩形的面积
public double getArea() { return width * height;
} //实现getPerimeter()方法,返回矩形的周长
public double getPerimeter() { return (width + height) *2;
}
}//定义一个类Triangle,实现Shape接口class Triangle implements Shape { //定义三个私有属性a,b,c表示三角形的三条边长
private double a; private double b; private double c; //定义一个公有构造方法,用于初始化a,b,c,并检查是否满足三角形条件(任意两边之和大于第三边)
public Triangle(double a, double b, double c) throws Exception{ if (a + b > c && a + c > b && b + c > a) {
this.a = a; this.b = b;
this.c = c;
} else {
throw new Exception("Invalid triangle");
}
} //实现getArea()方法,返回三角形的面积(使用海伦公式)
public double getArea() { //计算半周长p
double p = (a + b + c) /2; //计算并返回面积s(使用Math.sqrt()函数求平方根)
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
} //实现getPerimeter()方法,返回三角形的周长
public double getPerimeter(){ return a + b + c;
}
}//定义一个类Square,继承Rectangle类,并重写构造方法和toString()方法class Square extends Rectangle { //重写构造方法,在调用父类构造方法时传入相弊者同的参数side作为width和height
public Square(double side){ super(side, side);
} //重写toString()方法,在原来基础上加上"Square:"前缀,并只显示side属性而不显示width和height属性(使用String.format()函数格式化字符串)
@Override
public String toString(){ return String.format("Square: side=%.2f", super.width); /* 或者直接使用super.getPerimeter()/4作为side */
/* return String.format("Square: side=%.2f", super.getPerimeter()/4); */

/* 注意:不能直接访问super.side属性,

② JAVA编程

Java视频教程 孙鑫Java无难事 (全12CD)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=154630
上海交大 Java初级编程基础 共25讲下载 avi格式
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=185162
张孝祥Java课堂系列之 Tomcat入门(七讲 swf)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=181503
张孝祥Java课堂系列之 Servlet开发入门 (九讲 swf)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=181502
张孝祥Java课堂系列之 HTTP协议详解(四讲 swf)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=181500
张孝祥Java课堂系列之 Web XML基础(四讲 swf)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=181499
J2EE视频教程 中间件技术 浙江大学胡晓军 全24讲完整版下载
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=181597
EJB技术及应用视频教程 东北大学 全26讲完整版 RMVB格式
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=181583
J2EE开发IDE Eclipse视频教程 全9CD 完整版
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=158075
Java视频教程 即学即会java
http://bbs.topsage.com/dispbbs_121_156264_6.html

Java 编程初步 傻瓜书
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159167
零基础学Java电子教程 附随书光盘
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=181032
数据结与算法 Java语言版
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=160250
精通JBuilder 2005 中文版 高清PDF+随书光盘
http://bbs.topsage.com/dispbbs_121_172861_1.html
程序天下:J2EE整合详解与典型案例 电子教程+随书光盘
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=181884
JSF JavaServer Faces in Action
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=160229
Spring in Action (第二版)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=146170
Manning jQuery in Action
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174769
Spring In Action
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174107
AspectJ in Action
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174106
Hibernate In Action
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174105
Struts in Action
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174005
Lucene in Action
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=173991
Wrox出版 Beginning Spring Framework 2
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=175714
Java Extreme Programming Cookbook
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174729
Java Programming Cookbook
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=173667
Beginning Java Objects
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174685
Building Java Enterprise App
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174684
Ant: The Definitive Guide
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174683
Better Faster Lighter Java
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174681
Hibernate Reference 3.1
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174674
Pragmatic Version Control using CVS
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174479
Pragmatic Project Automation
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174476
jQuery Reference Guide
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174411
Java 2 核心技术 卷1&卷2 CHM版
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174123
Wrox Professional Java JDK 6 Edition
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=163451
Java 2 宝典
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159154
侯捷java编程思想 PDF中文版
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=163902
Learning Java (第三版)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=146869
The Java Language Specification
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=176082
Beginning Java Programming for Dummies 第二版
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=163923
Java 2 核心编程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=146174
Java How to Program (第六版)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=146371
Java All-In-One案头参考傻瓜书 (第二版)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=152871
21天自学 Java 6 (2007年5月更新出版) PDF
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159315
Java咖啡馆
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151447
Thinking in Java(第四版)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=152053
深入学习JFC SWING - Java基础类组件集
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=152049
J2EE全实例教程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=158057
Java信息系统设计与开发实例(第二版)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151993
Java优化编程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151994
Effective Java
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=175258
Java信息系统设计与开发实例(第二版)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151993
Tricks of the Java Programming
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159305
Wrox Professional Java Native Interfaces with SWT JFace
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159290
Java Swing 第二版 PDF文字版 O'Relliy出版
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159158
O'Reilly - Java Database Programming with JDBC
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159292
JDBC与Java数据库程序设计
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151448
Learning JQurey (2007年7月最新出版)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159652
J2EE with BEA WebLogic Server
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=176088
Pragmatic Unit Testing in Java with JUnit
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=176084
Wiley出版 J2EE 1.4 Bible
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=176080
J2EE设计开发编程指南
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159288
Java Web Services简明教程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159159
O'Reilly Java and XML (第二版PDF)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=146231
O'Reilly Java and XML (第三版PDF)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=167584
Java技术XML高级编程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=147800
Java Reflection in Action [MANNING]
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=178017
Java Puzzler (Java 解惑)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=178016
Java Database Best Practices
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=178015
Struts: The Complete Reference
http://bbs.topsage.com/dispbbs.asp?boardID=123&ID=178025
Log4j The Complete Manual
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=178023
Programming for the Java Virtual Machine
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=178021
Expert One-on-One J2EE Design and Development
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=160652
Expert One-on-One J2EE Development without EJB
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=160653
JBoss - A Developer's Notebook
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=146179
The Java Programming Language (第四版)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=147095
Professional Java Development with the Spring Framework
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159303
Core Java Server Faces 第二版
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=145549
精通Enterprise JavaBeans
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=146018
Enterprise JavaBeans EJB 第四版
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159166
J2EE应用与BEA WebLogic Server (第二版PDF)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=146227
O'Reilly Java Web Services
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=146377
Ant权威指南
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=148383
Ajax和Java框架高级编程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=148371
Java Web Services简明教程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159159
Cross-Platform Web Services Using C# and Java
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=160472
O'Reilly Ajax on Java
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=160600
O'Reilly Java and XSLT
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=149585
O'Reilly Java and XML Binding
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=149583
O'Reilly Java and SOAP
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=149581
Design Patterns Java Companion
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=163920
J2EE Java黑客大曝光 开发安全的Java应用程序
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151446
J2ME API 速查手册
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=158248
精通J2ME无线编程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=152047
J2ME开发大全
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151987
Beginning J2ME 从入门到精通 第三版
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=184149
Beginning JBoss Seam从入门到精通
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=183607
Beginning Hibernate 从入门到精通
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=183602
Java网页开发的艺术
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151991
Java编程高手
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151990
Java 5.0 Tiger程序高手秘笈
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151989
Java2网络协议技术内幕(附源码)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151988
Using Enterprise JavaBeans 2
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=166051
Java技术实用教程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=158244
企业级Java安全性(构建安全的J2EE应用)
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=152048
Java语言集成开发环境Eclipse中文教程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=158237
NetBeans IDE 5.5 企业版高级开发教程
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=159653
Using Enterprise JavaBeans 2
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=166051
Borland JBuilder Developer's Guide
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=166294
Eclipse [O'Reilly]
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=175257
Eclipse精要与高级开发技术
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=151986
Code Generation in Action
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=174722
Java 6 3D游戏开发
http://bbs.topsage.com/dispbbs.asp?boardID=121&ID=160251

③ 急求JAVA源代码,小游戏或者别的

//这是个聊天程序, 在ECLIPSE 运行 Client.java 就可以了。 连接是:localhost
//Server 代码,

package message;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {

public static void main(String[] args) throws Exception{
System.out.print("Server");
ServerSocket socket=new ServerSocket(8888);
Vector v=new Vector();
while(true){
Socket sk=socket.accept();
DataInputStream in=new DataInputStream(sk.getInputStream());
DataOutputStream out=new DataOutputStream(sk.getOutputStream());
v.add(sk);
new ServerThread(in,v).start();
}

}

}

//ServerThread.java 代码
package message;
import java.net.*;
import java.io.*;
import java.util.*;
public class ServerThread extends Thread{

DataInputStream in;
Vector all;
public ServerThread(DataInputStream in,Vector v){
this.in=in;
this.all=v;

}

public void run()
{
while(true)
{
try{
String s1=in.readUTF();
for(int i=0;i<all.size();i++)
{
Object obj=all.get(i);
Socket socket=(Socket)obj;
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
out.writeUTF(s1);
System.out.print(i);
out.flush();

}
System.out.print("Message send over!");
}catch(Exception e){e.printStackTrace();};

}
}

}

//ClientFrame.java 代码
package message;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class ClientFrame extends JFrame implements ActionListener{

JButton b1=new JButton ("SendMessage");
JButton b2=new JButton("Link Server");
JTextField t1=new JTextField(20);
JTextField t2=new JTextField(20);
JLabel l=new JLabel("输入服务器名字:");
JTextArea area=new JTextArea(10,20);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
Socket socket;
public ClientFrame()
{
this.getContentPane().add(p1);
p2.add(new JScrollPane(area));
p3.add(t1);
p3.add(b1);
p4.add(l);
p4.add(t2);
p4.add(b2);

p2.setLayout(new FlowLayout());
p3.setLayout(new FlowLayout());
p4.setLayout(new FlowLayout());
p1.setLayout(new BorderLayout());
p1.add("North",p2);
p1.add("Center",p3);
p1.add("South",p4);
b1.addActionListener(this);
b2.addActionListener(this);

this.pack();
show();

}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Link Server"))
{
try{
socket=new Socket(t2.getText(),8888);
b2.setEnabled(false);
JOptionPane.showMessageDialog(this, "Connection Success");
DataInputStream in=new DataInputStream(socket.getInputStream());
new ClientThread(in,area).start();
}
catch(Exception e1){
JOptionPane.showMessageDialog(this, "Connection Error");
e1.printStackTrace();};
}
else if(e.getActionCommand().equals("SendMessage"))
{

try{
DataOutputStream out=new DataOutputStream(socket.getOutputStream());
out.writeUTF(t1.getText());
t1.setText("");
}catch(Exception e1){e1.printStackTrace();};

}

}

}

//ClientThread.java 代码

package message;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class ClientThread extends Thread {
DataInputStream in;
JTextArea area;
public ClientThread(DataInputStream in,JTextArea area){
this.in=in;
this.area=area;
}
public void run()
{
while(true){
try{

String s=in.readUTF();
area.append(s);
}
catch(Exception e){e.printStackTrace();};
}
}

}

//Client.java代码

package message;

public class Client {

/**
* @param args
*/
public static void main(String[] args) {
new ClientFrame();

}

}

// 每段代码都是个类,不要弄在一个文件里。 运行 Client.java
good luck to you!

④ 求一个JAVA游戏代码!!!急!!!

俄罗斯方侍告块——java源代码提供
import java.awt.*;
import java.awt.event.*;
//俄罗斯方块类
public class ERS_Block extends Frame{
public static boolean isPlay=false;
public static int level=1,score=0;
public static TextField scoreField,levelField;

public static MyTimer timer;
GameCanvas gameScr;

public static void main(String[] argus){
ERS_Block ers = new ERS_Block("俄罗斯方块游戏 V1.0 Author:Vincent");
WindowListener win_listener = new WinListener();
ers.addWindowListener(win_listener);
}

//俄罗斯方块类的构造方法
ERS_Block(String title){
super(title);

setSize(600,480);
setLayout(new GridLayout(1,2));

gameScr = new GameCanvas();
gameScr.addKeyListener(gameScr);

timer = new MyTimer(gameScr);
timer.setDaemon(true);
timer.start();
timer.suspend();

add(gameScr);

Panel rightScr = new Panel();
rightScr.setLayout(new GridLayout(2,1,0,30));
rightScr.setSize(120,500);
add(rightScr);

//右边信息窗体的布局
MyPanel infoScr = new MyPanel();
infoScr.setLayout(new GridLayout(4,1,0,5));
infoScr.setSize(120,300);
rightScr.add(infoScr);

/和物/定义标签和初始值
Label scorep = new Label("分数:",Label.LEFT);
Label levelp = new Label("级数:",Label.LEFT);
scoreField = new TextField(8);
levelField = new TextField(8);
scoreField.setEditable(false);
levelField.setEditable(false);
infoScr.add(scorep);
infoScr.add(scoreField);
infoScr.add(levelp);
infoScr.add(levelField);
scorep.setSize(new Dimension(20,60));
scoreField.setSize(new Dimension(20,60));
levelp.setSize(new Dimension(20,60));
levelField.setSize(new Dimension(20,60));
scoreField.setText("0");
levelField.setText("1");

//右边控制按钮窗体的布局
MyPanel controlScr = new MyPanel();
controlScr.setLayout(new GridLayout(5,1,0,5));
rightScr.add(controlScr);

//定义按唤谈液钮play
Button play_b = new Button("开始游戏");
play_b.setSize(new Dimension(50,200));
play_b.addActionListener(new Command(Command.button_play,gameScr));

//定义按钮Level UP
Button level_up_b = new Button("提高级数");
level_up_b.setSize(new Dimension(50,200));
level_up_b.addActionListener(new Command(Command.button_levelup,gameScr));

//定义按钮Level Down
Button level_down_b =new Button("降低级数");
level_down_b.setSize(new Dimension(50,200));
level_down_b.addActionListener(new Command(Command.button_leveldown,gameScr));

//定义按钮Level Pause
Button pause_b =new Button("游戏暂停");
pause_b.setSize(new Dimension(50,200));
pause_b.addActionListener(new Command(Command.button_pause,gameScr));

//定义按钮Quit
Button quit_b = new Button("退出游戏");
quit_b.setSize(new Dimension(50,200));
quit_b.addActionListener(new Command(Command.button_quit,gameScr));

controlScr.add(play_b);
controlScr.add(level_up_b);
controlScr.add(level_down_b);
controlScr.add(pause_b);
controlScr.add(quit_b);
setVisible(true);
gameScr.requestFocus();
}
}

//重写MyPanel类,使Panel的四周留空间
class MyPanel extends Panel{
public Insets getInsets(){
return new Insets(30,50,30,50);
}
}

//游戏画布类
class GameCanvas extends Canvas implements KeyListener{
final int unitSize = 30; //小方块边长
int rowNum; //正方格的行数
int columnNum; //正方格的列数
int maxAllowRowNum; //允许有多少行未削
int blockInitRow; //新出现块的起始行坐标
int blockInitCol; //新出现块的起始列坐标
int [][] scrArr; //屏幕数组
Block b; //对方快的引用

//画布类的构造方法
GameCanvas(){
rowNum = 15;
columnNum = 10;
maxAllowRowNum = rowNum - 2;
b = new Block(this);
blockInitRow = rowNum - 1;
blockInitCol = columnNum/2 - 2;
scrArr = new int [32][32];
}

//初始化屏幕,并将屏幕数组清零的方法
void initScr(){
for(int i=0;i<rowNum;i++)
for (int j=0; j<columnNum;j++)
scrArr[j]=0;
b.reset();
repaint();
}

//重新刷新画布方法
public void paint(Graphics g){
for(int i = 0; i < rowNum; i++)
for(int j = 0; j < columnNum; j++)
drawUnit(i,j,scrArr[j]);
}

//画方块的方法
public void drawUnit(int row,int col,int type){
scrArr[row][col] = type;
Graphics g = getGraphics();
tch(type){ //表示画方快的方法
case 0: g.setColor(Color.black);break; //以背景为颜色画
case 1: g.setColor(Color.blue);break; //画正在下落的方块
case 2: g.setColor(Color.magenta);break; //画已经落下的方法
}
g.fill3DRect(col*unitSize,getSize().height-(row+1)*unitSize,unitSize,unitSize,true);
g.dispose();
}

public Block getBlock(){
return b; //返回block实例的引用
}

//返回屏幕数组中(row,col)位置的属性值
public int getScrArrXY(int row,int col){
if (row < 0 || row >= rowNum || col < 0 || col >= columnNum)
return(-1);
else
return(scrArr[row][col]);
}

//返回新块的初始行坐标方法
public int getInitRow(){
return(blockInitRow); //返回新块的初始行坐标
}

//返回新块的初始列坐标方法
public int getInitCol(){
return(blockInitCol); //返回新块的初始列坐标
}

//满行删除方法
void deleteFullLine(){
int full_line_num = 0;
int k = 0;
for (int i=0;i<rowNum;i++){
boolean isfull = true;

L1:for(int j=0;j<columnNum;j++)
if(scrArr[j] == 0){
k++;
isfull = false;
break L1;
}
if(isfull) full_line_num++;
if(k!=0 && k-1!=i && !isfull)
for(int j = 0; j < columnNum; j++){
if (scrArr[j] == 0)
drawUnit(k-1,j,0);
else
drawUnit(k-1,j,2);
scrArr[k-1][j] = scrArr[j];
}
}
for(int i = k-1 ;i < rowNum; i++){
for(int j = 0; j < columnNum; j++){
drawUnit(i,j,0);
scrArr[j]=0;
}
}
ERS_Block.score += full_line_num;
ERS_Block.scoreField.setText(""+ERS_Block.score);
}

//判断游戏是否结束方法
boolean isGameEnd(){
for (int col = 0 ; col <columnNum; col ++){
if(scrArr[maxAllowRowNum][col] !=0)
return true;
}
return false;
}

public void keyTyped(KeyEvent e){
}

public void keyReleased(KeyEvent e){
}

//处理键盘输入的方法
public void keyPressed(KeyEvent e){
if(!ERS_Block.isPlay)
return;
tch(e.getKeyCode()){
case KeyEvent.VK_DOWN:b.fallDown();break;
case KeyEvent.VK_LEFT:b.leftMove();break;
case KeyEvent.VK_RIGHT:b.rightMove();break;
case KeyEvent.VK_SPACE:b.leftTurn();break;
}
}
}

//处理控制类
class Command implements ActionListener{
static final int button_play = 1; //给按钮分配编号
static final int button_levelup = 2;
static final int button_leveldown = 3;
static final int button_quit = 4;
static final int button_pause = 5;
static boolean pause_resume = true;

int curButton; //当前按钮
GameCanvas scr;

//控制按钮类的构造方法
Command(int button,GameCanvas scr){
curButton = button;
this.scr=scr;
}

//按钮执行方法
public void actionPerformed (ActionEvent e){
tch(curButton){
case button_play:if(!ERS_Block.isPlay){
scr.initScr();
ERS_Block.isPlay = true;
ERS_Block.score = 0;
ERS_Block.scoreField.setText("0");
ERS_Block.timer.resume();
}
scr.requestFocus();
break;
case button_levelup:if(ERS_Block.level < 10){
ERS_Block.level++;
ERS_Block.levelField.setText(""+ERS_Block.level);
ERS_Block.score = 0;
ERS_Block.scoreField.setText(""+ERS_Block.score);
}
scr.requestFocus();
break;
case button_leveldown:if(ERS_Block.level > 1){
ERS_Block.level--;
ERS_Block.levelField.setText(""+ERS_Block.level);
ERS_Block.score = 0;
ERS_Block.scoreField.setText(""+ERS_Block.score);
}
scr.requestFocus();
break;
case button_pause:if(pause_resume){
ERS_Block.timer.suspend();
pause_resume = false;
}else{
ERS_Block.timer.resume();
pause_resume = true;
}
scr.requestFocus();
break;
case button_quit:System.exit(0);
}
}
}

//方块类
class Block {
static int[][] pattern = {
{0x0f00,0x4444,0x0f00,0x4444},//用十六进至表示,本行表示长条四种状态
{0x04e0,0x0464,0x00e4,0x04c4},
{0x4620,0x6c00,0x4620,0x6c00},
{0x2640,0xc600,0x2640,0xc600},
{0x6220,0x1700,0x2230,0x0740},
{0x6440,0x0e20,0x44c0,0x8e00},
{0x0660,0x0660,0x0660,0x0660}
};
int blockType; //块的模式号(0-6)
int turnState; //块的翻转状态(0-3)
int blockState; //快的下落状态
int row,col; //块在画布上的坐标
GameCanvas scr;

//块类的构造方法
Block(GameCanvas scr){
this.scr = scr;
blockType = (int)(Math.random() * 1000)%7;
turnState = (int)(Math.random() * 1000)%4;
blockState = 1;
row = scr.getInitRow();
col = scr.getInitCol();
}

//重新初始化块,并显示新块
public void reset(){
blockType = (int)(Math.random() * 1000)%7;
turnState = (int)(Math.random() * 1000)%4;
blockState = 1;
row = scr.getInitRow();
col = scr.getInitCol();
dispBlock(1);
}

//实现“块”翻转的方法
public void leftTurn(){
if(assertValid(blockType,(turnState + 1)%4,row,col)){
dispBlock(0);
turnState = (turnState + 1)%4;
dispBlock(1);
}
}

//实现“块”的左移的方法
public void leftMove(){
if(assertValid(blockType,turnState,row,col-1)){
dispBlock(0);
col--;
dispBlock(1);
}
}

//实现块的右移
public void rightMove(){
if(assertValid(blockType,turnState,row,col+1)){
dispBlock(0);
col++;
dispBlock(1);
}
}

//实现块落下的操作的方法
public boolean fallDown(){
if(blockState == 2)
return(false);
if(assertValid(blockType,turnState,row-1,col)){
dispBlock(0);
row--;
dispBlock(1);
return(true);
}else{
blockState = 2;
dispBlock(2);
return(false);
}
}

//判断是否正确的方法
boolean assertValid(int t,int s,int row,int col){
int k = 0x8000;
for(int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if((int)(pattern[t][s]&k) != 0){
int temp = scr.getScrArrXY(row-i,col+j);
if (temp<0||temp==2)
return false;
}
k = k >> 1;
}
}
return true;
}

//同步显示的方法
public synchronized void dispBlock(int s){
int k = 0x8000;
for (int i = 0; i < 4; i++){
for(int j = 0; j < 4; j++){
if(((int)pattern[blockType][turnState]&k) != 0){
scr.drawUnit(row-i,col+j,s);
}
k=k>>1;
}
}
}
}

//定时线程
class MyTimer extends Thread{
GameCanvas scr;

public MyTimer(GameCanvas scr){
this.scr = scr;
}

public void run(){
while(true){
try{
sleep((10-ERS_Block.level + 1)*100);
}
catch(InterruptedException e){}
if(!scr.getBlock().fallDown()){
scr.deleteFullLine();
if(scr.isGameEnd()){
ERS_Block.isPlay = false;
suspend();
}else
scr.getBlock().reset();
}
}
}

class WinListener extends WindowAdapter{
public void windowClosing (WindowEvent l){
System.exit(0);
}
}

⑤ 求JAVA源代码

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class GradeStatistic {

public static void main(String[] args) {

GradeStatistic gs = new GradeStatistic();

List<Mark> list = new ArrayList<Mark>();

float sum = 0;

while(true){

Scanner sc = new Scanner(System.in);

System.out.print("Please input student name: ");
String name = sc.nextLine();

if(name.equals("end")){
break;
}

System.out.print("Please input student score: ");
float score = sc.nextFloat();
sum += score;

list.add(gs.new Mark(name, score));

}

float max = list.get(0).getScore();
float min = list.get(0).getScore();

for(Mark mark: list){
if(max < mark.getScore()){
max = mark.getScore();
}

if(min > mark.getScore()){
min = mark.getScore();
}

}

float average = sum / list.size();

System.out.println("Average is: " + average);
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}

private class Mark{
private String name;
private float score;

public Mark(String name, float score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public float getScore() {
return score;
}
}
}
----------------------
Please input student name: Zhang san
Please input student score: 100
Please input student name: Li Si
Please input student score: 91
Please input student name: Ec
Please input student score: 35
Please input student name: ma qi
Please input student score: 67
Please input student name: end
Average is: 73.25
Max is: 100.0
Min is: 35.0

⑥ 求编写一个超级简单的Java的程序源代码

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Login {

public static void main(String args[]) {
LoginFrm frame = new LoginFrm();
}
}

class LoginFrm extends JFrame implements ActionListener{
JLabel nameLabel=new JLabel("用户名:");
JLabel pwdLabel=new JLabel("密码:");
JTextField name=new JTextField(10);
JPasswordField password=new JPasswordField(10);
JButton butnSure=new JButton("确定");
JButton butnCancel=new JButton("取消");
public LoginFrm() {
super("登陆");
setBounds(500, 200, 280, 220);
setVisible(true);
setLayout(null);
nameLabel.setBounds(45, 20, 100, 25);
add(nameLabel);
add(name);
name.setBounds(105, 20, 110, 25);
add(pwdLabel);
pwdLabel.setBounds(45, 60, 100, 25);
add(password);
password.setBounds(105, 60, 110, 25);
add(butnSure);
butnSure.setBounds(45, 100, 80, 25);
add(butnCancel);
butnCancel.setBounds(135, 100, 80, 25);
butnSure.addActionListener(this);
butnCancel.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();//刷新
}

public void actionPerformed(ActionEvent e) {
if (e.getSource() ==butnSure){
System.out.println("用户名:"+name.getText());
System.out.println("密码:"+name.getText());
if("admin".equals(name.getText().trim())&&"123".equals(password.getText().trim())){
this.dispose();
new MainFrm("用户界面",name.getText().trim(),password.getText().trim());
}else {
JOptionPane.showMessageDialog(this, "用户不存在");
}
}else if(e.getSource()==butnCancel){
System.exit(1);
}
}

class MainFrm extends JFrame{
private JLabel info;

public MainFrm(String s,String name,String password) {
super(s);
setBounds(400, 200, 500, 400);
setLayout(new FlowLayout());
info=new JLabel("登陆成功,用户名:"+name+",密码:"+password);
add(info);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
}
}

⑦ 我想看一点简单的Java程序的源代码

兄弟可以联系 hi留言也行
我这里保留了,从学习java第一天起到现在 学习练习代码,
基础部分注释很详细。

//每一位相加求和
public class Demo
{
public static void main(String[] args)
{
int i = 45678;
int sum = 0;
int c = 0;
for(int j = 4; j >= 0; j--)
{
int b = (int) Math.pow(10, j);
c = i / b;

System.out.println(c);

sum += c;
System.out.println("sum=" + sum);

i %= b;
}
System.out.println(sum);
}
}

public class ZY1030
{
public static void main(String[] asgs)
{
zy1();
zy2();
zy3();
zy4();
zy5();
zy6();
zy7();
yanghuisanjiao();

}

public static void yanghuisanjiao()
{
int[][] pas = new int[6][];
for(int i = 0; i < pas.length; i++)
{
pas[i] = new int[i + 1];
pas[i][0] = 1;
pas[i][i] = 1;
for(int j = 0; j < pas[i].length - 1; j++)
{
if(j >= 1 && i > 1)
pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j];
}
}
for(int i = 0; i < pas.length; i++)
{
for(int j = 0; j < pas[i].length; j++)
{
System.out.print(pas[i][j]);
System.out.println();
}
}
System.out.println();
}

private static void zy1()
{
System.out.println("��ҵ1");
/*

*
**
***
****
*****
******
*******

*/
for(int r = 0; r < 7; r++)
{
for(int j = 0; j < r + 1; j++)
{
System.out.print("*");
}
System.out.println();
}
}

private static void zy2()
{
System.out.println("\n��ҵ2");
/*
*
***
*****
*******
*********
*/
for(int r = 0; r <= 5; r++)
{

for(int j = 0; j < 11 - r; j++)
{
System.out.print(" ");
}

for(int j = 0; j < r * 2 + 1; j++)
{
System.out.print("*");
}
System.out.println();
}
}

private static void zy3()
{
System.out.println("\n��ҵ3");
/*
*
* *
* * *
* * * *
* * * * *
*/
for(int r = 0; r <= 5; r++)
{

for(int j = 0; j < 10 - r; j++)
{
System.out.print(" ");
}

for(int j = 0; j < r; j++)
{
System.out.print("* ");
}
System.out.println();
}
}

private static void zy4()
{
System.out.println("\n��ҵ4");
/*
***********
***********
***********
***********
***********
*/
for(int r = 0; r <= 5; r++)
{

for(int j = 0; j < 7 - r; j++)
{
System.out.print(" ");
}

for(int j = 0; j < 10; j++)
{
System.out.print("*");
}
System.out.println();
}
}

private static void zy5()
{
System.out.println("\n��ҵ5");
/*
1X1=1
1X2=2 2X2=4
1X3=3 2X3=6 3X3=9
....
*/
for(int r = 1; r <= 9; r++)
{

for(int j = 1; j <= r; j++)
{
int iValue = j * r; //����ֵ
String str = j + "X" + r + "=" + iValue + " ";

System.out.print(str);
//System.out.print(" J:" + j);
}

System.out.println();
//System.out.println("R:" + r);
}
}

private static void zy6()
{
System.out.println("\n��ҵ6");
/*
������(����) ֻ�ܱ�1�ͱ����������������
3 5 7 11 13 17 19 23 ....

int n = 4;
boolean t = true; // assume is SU_SHU
for(int i=2; i<n; i++)
{
if(n%i==0)
{
t = false;
break;
}
}
System.out.println(t);
*/
for(int i = 2; i <= 100; i++)
{

// if(i % 2 ==0 || i % 3 == 0 )
// {
// continue;
// }
// System.out.print(i + " ");

boolean bTrue = true;
for(int j = 2; j < i; j++)
{
if(i % j == 0)
{
bTrue = false;
break;
}
}

if(bTrue)
{
System.out.println(i);
}
}
System.out.println();
}

private static void zy7()
{
System.out.println("\n��ҵ7");
/*
int n = 23623;

int x = n % 100000 / 10000;
System.out.println(x);

x = n % 10000 / 1000;
System.out.println(x);

x = n % 1000 / 100;
System.out.println(x);
*/

}

}

⑧ 用JAVA 编程的源代码

直接定义两个接口学生接口里面定义一个学费的变量,老师接口里面定义一个工资变量,
eclipse里面可以自动帮你添加相关变量的getterhe
setter方法的。

阅读全文

与100个java经典编程实例源代码相关的资料

热点内容
为什么安卓车载中控屏看起来很差 浏览:464
吃鸡怎么解压最快 浏览:966
linux网络编程基础 浏览:217
产研是程序员吗 浏览:592
程序员的法律 浏览:967
编程第四关用冰雪火焰闪现通关 浏览:754
批处理当前文件夹参数 浏览:183
鸿蒙安卓如何下载 浏览:902
开3389命令 浏览:540
程序员大都单纯吗 浏览:913
APP如何实现下载功能 浏览:214
通达信源码怎样放到桌面 浏览:643
程序员的脑袋会秃吗 浏览:453
为什么eve登录启动不进去服务器 浏览:270
微信招生app哪个好用 浏览:233
宝可梦剑盾启动文件在哪个文件夹 浏览:765
压缩机比容 浏览:117
python自动化测试面试 浏览:949
买便宜点的鞋子去哪个app买 浏览:890
android中个人页面 浏览:711