
正文
java经典案例源代码 java编程经典案例
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
GitHub上面有哪些经典的java框架源码
Bazel:来自Google的构建工具,可以快速、可靠地构建代码。官网
Gradle:使用Groovy(非XML)进行增量构建,可以很好地与Maven依赖管理配合工作。官网
Buck:Facebook构建工具。官网
字节码操作
编程方式操作字节码的开发库。
ASM:通用底层字节码操作和分析开发库。官网
Byte Buddy:使用流式API进一步简化字节码生成。官网
Byteman:在运行时通过DSL(规则)操作字节码进行测试和故障排除。官网
Javassist:一个简化字节码编辑尝试。官网
集群管理
在集群内动态管理应用程序的框架。
Apache Aurora:Apache Aurora是一个Mesos框架,用于长时间运行服务和定时任务(cron job)。官网
Singularity:Singularity是一个Mesos框架,方便部署和操作。它支持Web Service、后台运行、调度作业和一次性任务。官网
代码分析
测量代码指标和质量工具。
Checkstyle:代码编写规范和标准静态分析工具。官网
Error Prone:将常见编程错误作为运行时错误报告。官网
FindBugs:通过字节码静态分析查找隐藏bug。官网
jQAssistant:使用基于Neo4J查询语言进行代码静态分析。官网
PMD:对源代码分析查找不良的编程习惯。官网
SonarQube:通过插件集成其它分析组件,对过去一段时间内的数据进行统计。官网
编译器生成工具
用来创建解析器、解释器或编译器的框架。
ANTLR:复杂的全功能自顶向下解析框架。官网
JavaCC:JavaCC是更加专门的轻量级工具,易于上手且支持语法超前预测。官网
外部配置工具
支持外部配置的开发库。
config:针对JVM语言的配置库。官网
owner:减少冗余配置属性。官网
约束满足问题求解程序
帮助解决约束满足问题的开发库。
Choco:可直接使用的约束满足问题求解程序,使用了约束规划技术。官网
JaCoP:为FlatZinc语言提供了一个接口,可以执行MiniZinc模型。官网
OptaPlanner:企业规划与资源调度优化求解程序。官网
Sat4J:逻辑代数与优化问题最先进的求解程序。官网
持续集成
Bamboo:Atlassian解决方案,可以很好地集成Atlassian的其他产品。可以选择开源许可,也可以购买商业版。官网
CircleCI:提供托管服务,可以免费试用。官网
Codeship:提供托管服务,提供有限的免费模式。官网
fabric8:容器集成平台。官网
Go:ThoughtWork开源解决方案。官网
Jenkins:支持基于服务器的部署服务。官网
TeamCity:JetBrain的持续集成解决方案,有免费版。官网
Travis:通常用作开源项目的托管服务。官网
Buildkite: 持续集成工具,用简单的脚本就能设置pipeline,而且能快速构建,可以免费试用。官网
CSV解析
简化CSV数据读写的框架与开发库
uniVocity-parsers:速度最快功能最全的CSV开发库之一,同时支持TSV与固定宽度记录的读写。官网
数据库
简化数据库交互的相关工具。
Apache Phoenix:HBase针对低延时应用程序的高性能关系数据库层。官网
Crate:实现了数据同步、分片、缩放、复制的分布式数据存储。除此之外还可以使用基于SQL的语法跨集群查询。官网
Flyway:简单的数据库迁移工具。官网
H2:小型SQL数据库,以可以作为内存数据库使用著称。官网
HikariCP:高性能JDBC连接工具。官网
JDBI:便捷的JDBC抽象。官网
Protobuf:Google数据交换格式。官网
SBE:简单二进制编码,是最快速的消息格式之一。官网
Wire:整洁轻量级协议缓存。官网
帮实现依赖翻转范式的开发库。 官网
Apache DeltaSpike:CDI扩展框架。官网
Dagger2:编译时注入框架,不需要使用反射。官网
Guice:可以匹敌Dagger的轻量级注入框架。官网
HK2:轻量级动态依赖注入框架。官网
开发流程增强工具
从最基本的层面增强开发流程。
ADT4J:针对代数数据类型的JSR-269代码生成器。官网
AspectJ:面向切面编程(AOP)的无缝扩展。官网
Auto:源代码生成器集合。官网
DCEVM:通过修改JVM在运行时支持对已加载的类进行无限次重定义。官网
HotswapAgent:支持无限次重定义运行时类与资源。官网
Immutables:类似Scala的条件类。官网
JHipster:基于Spring Boot与AngularJS应用程序的Yeoman源代码生成器。官网
JRebel:无需重新部署,可以即时重新加载代码与配置的商业软件。官网
Lombok:减少冗余的代码生成器。官网
Spring Loaded:类重载代理。官网
vert.x:多语言事件驱动应用框架。官网
分布式应用
用来编写分布式容错应用的开发库和框架。
Akka:用来编写分布式容错并发事件驱动应用程序的工具和运行时。官网
Apache Storm:实时计算系统。官网
Apache ZooKeeper:针对大型分布式系统的协调服务,支持分布式配置、同步和名称注册。官网
Hazelcast:高可扩展内存数据网格。官网
Hystrix:提供延迟和容错。官网
JGroups:提供可靠的消息传递和集群创建的工具。官网
Orbit:支持虚拟角色(Actor),在传统角色的基础上增加了另外一层抽象。官网
Quasar:为JVM提供轻量级线程和角色。官网
分布式数据库
对应用程序而言,在分布式系统中的数据库看起来就像是只有一个数据源。
Apache Cassandra:列式数据库,可用性高且没有单点故障。官网
Apache HBase:针对大数据的Hadoop数据库。官网
Druid:实时和历史OLAP数据存储,在聚集查询和近似查询方面表现不俗。官网
Infinispan:针对缓存的高并发键值对数据存储。官网
发布
以本机格式发布应用程序的工具。
Bintray:发布二进制文件版本控制工具。可以于Maven或Gradle一起配合使用。提供开源免费版本和几种商业收费版本。官网
Central Repository:最大的二进制组件仓库,面向开源社区提供免费服务。Apache Maven默认使用Central 官网Repository,也可以在所有其他构建工具中使用。
IzPack:为跨平台部署建立创作工具(Authoring Tool)。官网
JitPack:打包GitHub仓库的便捷工具。可根据需要构建Maven、Gradle项目,发布可立即使用的组件。官网
Launch4j:将JAR包装为轻量级本机Windows可执行程序。官网
Nexus:支持代理和缓存功能的二进制管理工具。官网
packr:将JAR、资源和JVM打包成Windows、Linux和Mac OS X本地发布文件。官网
文档处理工具
处理Office文档的开发库。
Apache POI:支持OOXML规范(XLSX、DOCX、PPTX)以及OLE2规范(XLS、DOC、PPT)。官网
documents4j:使用第三方转换器进行文档格式转换,转成类似MS Word这样的格式。官网
jOpenDocument:处理OpenDocument格式(由Sun公司提出基于XML的文档格式)。官网
函数式编程
函数式编程支持库。
Cyclops:支持一元(Monad)操作和流操作工具类、comprehension(List语法)、模式匹配、trampoline等特性。官网
Fugue:Guava的函数式编程扩展。官网
Functional Java:实现了多种基础和高级编程抽象,用来辅助面向组合开发(composition-oriented development)。官网
Javaslang:一个函数式组件库,提供持久化数据类型和函数式控制结构。官网
jOOλ:旨在填补Java 8 lambda差距的扩展,提供了众多缺失的类型和一组丰富的顺序流API。官网
游戏开发
游戏开发框架。
jMonkeyEngine:现代3D游戏开发引擎。官网
libGDX:全面的跨平台高级框架。官网
LWJGL:对OpenGL/CL/AL等技术进行抽象的健壮框架。官网
GUI
现代图形化用户界面开发库。
JavaFX:Swing的后继者。官网
Scene Builder:开发JavaFX应用的可视化布局工具。官网
高性能计算
涵盖了从集合到特定开发库的高性能计算相关工具。
Agrona:高性能应用中常见的数据结构和工具方法。官网
Disruptor:线程间消息传递开发库。官网
fastutil:快速紧凑的特定类型集合(Collection)。官网
GS Collections:受Smalltalk启发的集合框架。官网
HPPC:基础类型集合。官网
Javolution:实时和嵌入式系统的开发库。官网
JCTools:JDK中缺失的并发工具。官网
Koloboke:Hash set和hash map。官网
Trove:基础类型集合。官网
High-scale-bli:Cliff Click 个人开发的高性能并发库官网
IDE
简化开发的集成开发环境。
Eclipse:老牌开源项目,支持多种插件和编程语言。官网
IntelliJ IDEA:支持众多JVM语言,是安卓开发者好的选择。商业版主要针对企业客户。官网
NetBeans:为多种技术提供集成化支持,包括Java SE、Java EE、数据库访问、HTML5
Imgscalr:纯Java 2D实现,简单、高效、支持硬件加速的图像缩放开发库。官网
Picasso:安卓图片下载和图片缓存开发库。官网
Thumbnailator:Thumbnailator是一个高质量Java缩略图开发库。官网
ZXing:支持多种格式的一维、二维条形码图片处理开发库。官网
im4java: 基于ImageMagick或GraphicsMagick命令行的图片处理开发库,基本上ImageMagick能够支持的图片格式和处理方式都能够处理。官网
Apache Batik:在Java应用中程序以SVG格式显示、生成及处理图像的工具集,包括SVG解析器、SVG生成器、SVG DOM等模块,可以集成使用也可以单独使用,还可以扩展自定义的SVG标签。官网
JSON
简化JSON处理的开发库。
Genson:强大且易于使用的Java到JSON转换开发库。官网
Gson:谷歌官方推出的JSON处理库,支持在对象与JSON之间双向序列化,性能良好且可以实时调用。官网
Jackson:与GSON类似,在频繁使用时性能更佳。官网
LoganSquare:基于Jackson流式API,提供对JSON解析和序列化。比GSON与Jackson组合方式效果更好。官网
Fastjson:一个Java语言编写的高性能功能完善的JSON库。官网
Kyro:快速、高效、自动化的Java对象序列化和克隆库。官网
JVM与JDK
目前的JVM和JDK实现。
JDK 9:JDK 9的早期访问版本。官网
OpenJDK:JDK开源实现。官网
基于JVM的语言
除Java外,可以用来编写JVM应用程序的编程语言。
Scala:融合了面向对象和函数式编程思想的静态类型编程语言。官网
Groovy:类型可选(Optionally typed)的动态语言,支持静态类型和静态编译。目前是一个Apache孵化器项目。官网
Clojure:可看做现代版Lisp的动态类型语言。官网
Ceylon:RedHat开发的面向对象静态类型编程语言。官网
Kotlin:JetBrain针对JVM、安卓和浏览器提供的静态类型编程语言。官网
Xtend:一种静态编程语言,能够将其代码转换为简洁高效的Java代码,并基于JVM运行。官网
日志
记录应用程序行为日志的开发库。
Apache Log4j 2:使用强大的插件和配置架构进行完全重写。官网
kibana:分析及可视化日志文件。官网
Logback:强健的日期开发库,通过Groovy提供很多有趣的选项。官网
logstash:日志文件管理工具。官网
Metrics:通过JMX或HTTP发布参数,并且支持存储到数据库。官网
SLF4J:日志抽象层,需要与具体的实现配合使用。官网
机器学习
提供具体统计算法的工具。其算法可从数据中学习。
Apache Flink:快速、可靠的大规模数据处理引擎。官网
Apache Hadoop:在商用硬件集群上用来进行大规模数据存储的开源软件框架。官网
Apache Mahout:专注协同过滤、聚类和分类的可扩展算法。官网
Apache Spark:开源数据分析集群计算框架。官网
DeepDive:从非结构化数据建立结构化信息并集成到已有数据库的工具。官网
Deeplearning4j:分布式多线程深度学习开发库。官网
H2O:用作大数据统计的分析引擎。官网
Weka:用作数据挖掘的算法集合,包括从预处理到可视化的各个层次。官网
QuickML:高效机器学习库。官网、GitHub
消息传递
在客户端之间进行消息传递,确保协议独立性的工具。
Aeron:高效可扩展的单播、多播消息传递工具。官网
Apache ActiveMQ:实现JMS的开源消息代理(broker),可将同步通讯转为异步通讯。官网
Apache Camel:通过企业级整合模式(Enterprise Integration Pattern EIP)将不同的消息传输API整合在一起。官网
Apache Kafka:高吞吐量分布式消息系统。官网
Hermes:快速、可靠的消息代理(Broker),基于Kafka构建。官网
JBoss HornetQ:清晰、准确、模块化,可以方便嵌入的消息工具。官网
JeroMQ:ZeroMQ的纯Java实现。官网
Smack:跨平台XMPP客户端函数库。官网
Openfire:是开源的、基于XMPP、采用Java编程语言开发的实时协作服务器。 Openfire安装和使用都非常简单,并可利用Web界面进行管理。 官网GitHub
Spark:是一个开源,跨平台IM客户端。它的特性支持集组聊天,电话集成和强大安全性能。如果企业内部部署IM使用Openfire+Spark是最佳的组合。 官网 GitHub
Tigase: 是一个轻量级的可伸缩的 Jabber/XMPP 服务器。无需其他第三方库支持,可以处理非常高的复杂和大量的用户数,可以根据需要进行水平扩展。 官网
杂项
未分类其它资源。
Design Patterns:实现并解释了最常见的设计模式。官网
Jimfs:内存文件系统。官网
Lanterna:类似curses的简单console文本GUI函数库。官网
LightAdmin:可插入式CRUD UI函数库,可用来快速应用开发。官网
OpenRefine:用来处理混乱数据的工具,包括清理、转换、使用Web Service进行扩展并将其关联到数据库。官网
RoboVM:Java编写原生iOS应用。官网
Quartz:强大的任务调度库.官网
应用监控工具
监控生产环境中应用程序的工具。
AppDynamics:性能监测商业工具。官网
JavaMelody:性能监测和分析工具。官网
Kamon:Kamon用来监测在JVM上运行的应用程序。官网
New Relic:性能监测商业工具。官网
SPM:支持对JVM应用程序进行分布式事务追踪的性能监测商业工具。官网
Takipi:产品运行时错误监测及调试商业工具。官网
原生开发库
用来进行特定平台开发的原生开发库。
JNA:不使用JNI就可以使用原生开发库。此外,还为常见系统函数提供了接口。官网
自然语言处理
用来专门处理文本的函数库。
Apache OpenNLP:处理类似分词等常见任务的工具。官网
CoreNLP:斯坦佛CoreNLP提供了一组基础工具,可以处理类似标签、实体名识别和情感分析这样的任务。官网
LingPipe:一组可以处理各种任务的工具集,支持POS标签、情感分析等。官网
Mallet:统计学自然语言处理、文档分类、聚类、主题建模等。官网
网络
网络编程函数库。
Async Http Client:异步HTTP和WebSocket客户端函数库。官网
Grizzly:NIO框架,在Glassfish中作为网络层使用。官网
Netty:构建高性能网络应用程序开发框架。官网
OkHttp:一个Android和Java应用的HTTP+SPDY客户端。官网
Undertow:基于NIO实现了阻塞和非阻塞API的Web服务器,在WildFly中作为网络层使用。官网
ORM
处理对象持久化的API。
Ebean:支持快速数据访问和编码的ORM框架。官网
EclipseLink:支持许多持久化标准,JPA、JAXB、JCA和SDO。官网
Hibernate:广泛使用、强健的持久化框架。Hibernate的技术社区非常活跃。官网
MyBatis:带有存储过程或者SQL语句的耦合对象(Couples object)。官网
OrmLite:轻量级开发包,免除了其它ORM产品中的复杂性和开销。官网
Nutz:另一个SSH。官网,Github
JFinal:JAVA WEB + ORM框架。官网,Github
用来帮助创建PDF文件的资源。
Apache FOP:从XSL-FO创建PDF。官网
Apache PDFBox:用来创建和操作PDF的工具集。官网
DynamicReports:JasperReports的精简版。官网
flyingsaucer:XML/XHTML和CSS 2.1渲染器。官网
iText:一个易于使用的PDF函数库,用来编程创建PDF文件。注意,用于商业用途时需要许可证。官网
JasperReports:一个复杂的报表引擎。官网
性能分析
性能分析、性能剖析及基准测试工具。
jHiccup:提供平台中JVM暂停的日志和记录。官网
JMH:JVM基准测试工具。官网
JProfiler:商业分析器。官网
LatencyUtils:测量和报告延迟的工具。官网
VisualVM:对运行中的应用程序信息提供了可视化界面。官网
YourKit Java Profiler:商业分析器。官网
响应式开发库
用来开发响应式应用程序的开发库。
Reactive Streams:异步流处理标准,支持非阻塞式反向压力(backpressure)。官网
Reactor:构建响应式快速数据(fast-data)应用程序的开发库。官网
RxJava:通过JVM可观察序列(observable sequence)构建异步和基于事件的程序。官网
REST框架
用来创建RESTful 服务的框架。
Dropwizard:偏向于自己使用的Web框架。用来构建Web应用程序,使用了Jetty、Jackson、Jersey和Metrics。官网
Feign:受Retrofit、JAXRS-2.0和WebSocket启发的HTTP客户端连接器(binder)。官网
Jersey:JAX-RS参考实现。官网
RESTEasy:经过JAX-RS规范完全认证的可移植实现。官网
RestExpress:一个Java类型安全的REST客户端。官网
RestX:基于注解处理和编译时源码生成的框架。官网
Retrofit:类型安全的REST客户端。官网
Spark:受到Sinatra启发的Java REST框架。官网
Swagger:Swagger是一个规范且完整的框架,提供描述、生产、消费和可视化RESTful Web Service。官网
Blade:国人开发的一个轻量级的MVC框架. 它拥有简洁的代码,优雅的设计。官网
科学计算与分析
用于科学计算和分析的函数库。
DataMelt:用于科学计算、数据分析及数据可视化的开发环境。官网
JGraphT:支持数学图论对象和算法的图形库。官网
JScience:用来进行科学测量和单位的一组类。官网
搜索引擎
文档索引引擎,用于搜索和分析。
Apache Solr:一个完全的企业搜索引擎。为高吞吐量通信进行了优化。官网
Elasticsearch:一个分布式、支持多租户(multitenant)全文本搜索引擎。提供了RESTful Web接口和无schema的JSON文档。官网
Apache Lucene:是一个开放源代码的全文检索引擎工具包,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎。官网
安全
用于处理安全、认证、授权或会话管理的函数库。
Apache Shiro:执行认证、授权、加密和会话管理。官网
Bouncy Castle,涵盖了从基础的帮助函数到PGP/SMIME操作。官网:多途加密开发库。支持JCA提供者(JCA provider)
Cryptomator:在云上进行客户端跨平台透明加密。官网
Keycloak:为浏览器应用和RESTful Web Service集成SSO和IDM。目前还处于beta版本,但是看起来非常有前途。官网
PicketLink:PicketLink是一个针对Java应用进行安全和身份认证管理的大型项目(Umbrella Project)。官网
序列化
用来高效处理序列化的函数库。
FlatBuffers:高效利用内存的序列化函数库,无需解包和解析即可高效访问序列化数据。官网
Kryo:快速、高效的对象图形序列化框架。官网
FST:提供兼容JDK的高性能对象图形序列化。官网
MessagePack:一种高效的二进制序列化格式。官网
应用服务器
用来部署应用程序的服务器。
Apache Tomcat:针对Servlet和JSP的应用服务器,健壮性好且适用性强。官网
Apache TomEE:Tomcat加Java EE。官网
Jetty:轻量级、小巧的应用服务器,通常会嵌入到项目中。官网
WebSphere Liberty:轻量级、模块化应用服务器,由IBM开发。官网
WildFly:之前被称作JBoss,由Red Hat开发。支持很多Java EE功能。官网
模板引擎
在模板中替换表达式的工具。
Apache Velocity:提供HTML页面模板、email模板和通用开源代码生成器模板。官网
FreeMarker:通用模板引擎,不需要任何重量级或自己使用的依赖关系。官网
Handlebars.java:使用Java编写的模板引擎,逻辑简单,支持语义扩展(semantic Mustache)。官网
Thymeleaf:旨在替换JSP,支持XML文件的工具。官网
测试
测试内容从对象到接口,涵盖性能测试和基准测试工具。
Apache JMeter:功能性测试和性能评测。官网
Arquillian:集成测试和功能行测试平台,集成Java EE容器。官网
AssertJ:支持流式断言提高测试的可读性。官网
Awaitility:用来同步异步操作的DSL。官网
Cucumber:BDD测试框架。官网
Gatling:设计为易于使用、可维护的和高性能负载测试工具。官网
Hamcrest:可用来灵活创建意图(intent)表达式的匹配器。官网
JMockit:用来模拟静态、final方法等。官网
JUnit:通用测试框架。官网
Mockito:在自动化单元测试中创建测试对象,为TDD或BDD提供支持。官网
PowerMock: 支持模拟静态方法、构造函数、final类和方法、私有方法以及移除静态初始化器的模拟工具。官网
REST Assured:为REST/HTTP服务提供方便测试的Java DSL。官网
Selenide:为Selenium提供精准的周边API,用来编写稳定且可读的UI测试。官网
Selenium:为Web应用程序提供可移植软件测试框架。官网
Spock:JUnit-compatible framework featuring an expressive Groovy-derived specification language.官网兼容JUnit框架,支持衍生的Groovy范的语言。
TestNG:测试框架。官网
Truth:Google的断言和命题(proposition)框架。官网
Unitils:模块化测试函数库,支持单元测试和集成测试。官网
WireMock:Web Service测试桩(Stub)和模拟函数。官网
通用工具库
通用工具类函数库。
Apache Commons:提供各种用途的函数,比如配置、验证、集合、文件上传或XML处理等。官网
args4j:命令行参数解析器。官网
CRaSH:为运行进行提供CLI。官网
Gephi:可视化跨平台网络图形化操作程序。官网
Guava:集合、缓存、支持基本类型、并发函数库、通用注解、字符串处理、I/O等。官网
JADE:构建、调试多租户系统的框架和环境。官网
javatuples:正如名字表示的那样,提供tuple支持。尽管目前tuple的概念还有留有争议。官网
JCommander:命令行参数解析器。官网
Protégé:提供存在论(ontology)编辑器以及构建知识系统的框架。官网
网络爬虫
用于分析网站内容的函数库。
Apache Nutch:可用于生产环境的高度可扩展、可伸缩的网络爬虫。官网
Crawler4j:简单的轻量级网络爬虫。官网
JSoup:刮取、解析、操作和清理HTML。官网
Web框架
用于处理Web应用程序不同层次间通讯的框架。
Apache Tapestry:基于组件的框架,使用Java创建动态、强健的、高度可扩展的Web应用程序。官网
Apache Wicket:基于组件的Web应用框架,与Tapestry类似带有状态显示GUI。官网
Google Web Toolkit:一组Web开发工具集,包含在客户端将Java代码转为JavaScript的编译器、XML解析器、RCP 官网API、JUnit集成、国际化支持和GUI控件。
Grails:Groovy框架,旨在提供一个高效开发环境,使用约定而非配置、没有XML并支持混入(mixin)。官网
Ninja:Java全栈Web开发框架。非常稳固、快速和高效。官网
Pippo:小型、高度模块化的类Sinatra框架。官网
Play:使用约定而非配置,支持代码热加载并在浏览器中显示错误。官网
PrimeFaces:JSF框架,提供免费和带支持的商业版本。包括若干前端组件。官网
Ratpack:一组Java开发函数库,用于构建快速、高效、可扩展且测试完备的HTTP应用程序。官网
Spring Boot:微框架,简化了Spring新程序的开发过程。官网
Spring:旨在简化Java EE的开发过程,提供依赖注入相关组件并支持面向切面编程。官网
Vaadin:基于GWT构建的事件驱动框架。使用服务端架构,客户端使用Ajax。官网
Blade:国人开发的一个轻量级的MVC框架. 它拥有简洁的代码,优雅的设计。官网
业务流程管理套件
流程驱动的软件系统构建。
jBPM:非常灵活的业务流程管理框架,致力于构建开发与业务分析人员之间的桥梁。官网
Activity:轻量级工作流和业务流程管理框架。官网 github
资源
社区
相关问答
Q1: 求这道Java题的源代码文件 必有重赏
您好java经典案例源代码:
代码如下java经典案例源代码,供参考java经典案例源代码:
代码示意图
运行结果示意图
public class Human {
private String name;
private char sex;
private int age;
// 无参
public Human(){
}
// 有参
public Human(String name,char sex,int age){
this.name = name;
this.sex = sex;
this.age = age;
}
// 结果方法
public void showResult(){
System.out.print("姓名java经典案例源代码:"+name+";性别:"+sex+";年龄:"+age);
}
public static void main(String[] args) {
// 无参构造函数
Human hu1 = new Human();
hu1.showResult();
System.out.println();
// 有参构造函数
Human hu2 = new Human("张三",'女',20);
hu2.showResult();
}
}
请点击输入图片描述
Q2: 求几个JAVA小项目源代码,供自己学习参考
package com.test01;
import java.util.Scanner;
public class oop5 { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// int x = 9;
// int y = 1;
int x = sc.nextInt();
int y = sc.nextInt();
int z;
z = add(x, y);
System.out.println("x的值为:" + x);
System.out.println("y的值为:" + y);
System.out.println("二者之和为:" + z);
}
/** 四种小算法 */
// 加法运算
public static int add(int a, int b) {
int c;
c = a + b;
return c;
}
// 减法运算
public static int jian(int d, int v) {
int m;
m = d - v;
return m;
}
// 乘积运算
public static int addAdd(int q, int w) {
int e;
e = q * w;
return e;
}
// 除法运算
public static int chu(int p, int k) {
int f;
f = p / k;
return f;
}
}
Q3: java简单记事本源代码 带解释
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
class test implements ActionListener
{
JFrame frame;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27,b28,b29,b30,b31;
JTextArea ta;
JPanel p1,p2,p3,p4;
JMenuBar mb;
JMenu m1,m2,m3;
JMenuItem mt1,mt2,mt3,mt4,mt5,mt6,mt7;
JRadioButton rb1,rb2;
ButtonGroup bg;
Double d1=0.0,d2=0.0,d3=0.0,d4=1.0,d5=1.0;
String s1="",s2="",s3="",s4="";
int a=0;
char c1;
int i=0;
public static void main(String[] args)
{
test that=new test();
that.go();
}
public void go()
{
frame=new JFrame("计算器");
Container cp= frame.getContentPane();
cp.setLayout(new FlowLayout());
b1=new JButton("7");b2=new JButton("8");b3=new JButton("9");b4=new JButton("/");b5=new JButton("1/x");b6=new JButton("sin");b7=new JButton("log");
b8=new JButton("4");b9=new JButton("5");b10=new JButton("6");b11=new JButton("*");b12=new JButton("x^y");b13=new JButton("cos");b14=new JButton("ln");
b15=new JButton("1");b16=new JButton("2");b17=new JButton("3");b18=new JButton("-");b19=new JButton(new ImageIcon("lanying.gif"));b20=new JButton("tan");b21=new JButton("x^3");
b22=new JButton("0");b23=new JButton("+/-");b24=new JButton(".");b25=new JButton("+");b26=new JButton("√x");b27=new JButton("cot");b28=new JButton("x^2");
b29=new JButton("Backspace");b30=new JButton("C");b31=new JButton("=");
mb=new JMenuBar();
m1=new JMenu("文件(F)");m2=new JMenu("编辑(E)");m3=new JMenu("帮助(H)");
mt1=new JMenuItem("清零");mt2=new JMenuItem("退出");mt3=new JMenuItem("复制");mt4=new JMenuItem("粘贴");mt5=new JMenuItem("版本");mt6=new JMenuItem("标准型");mt7=new JMenuItem("科学型");
ta=new JTextArea(1,30);
p1=new JPanel();p2=new JPanel();p3=new JPanel();p4=new JPanel();
rb1=new JRadioButton("科学型");rb2=new JRadioButton("标准型");
bg=new ButtonGroup();
b1.setForeground(Color.blue);b1.setBackground(Color.white);b2.setForeground(Color.blue);b2.setBackground(Color.white);
b3.setForeground(Color.blue);b3.setBackground(Color.white);b8.setForeground(Color.blue);b8.setBackground(Color.white);
b9.setForeground(Color.blue);b9.setBackground(Color.white);b10.setForeground(Color.blue);b10.setBackground(Color.white);
b15.setForeground(Color.blue);b15.setBackground(Color.white);b16.setForeground(Color.blue);b16.setBackground(Color.white);
b17.setForeground(Color.blue);b17.setBackground(Color.white);b22.setForeground(Color.blue);b22.setBackground(Color.white);
b23.setForeground(Color.blue);b23.setBackground(Color.white);b24.setForeground(Color.blue);b24.setBackground(Color.white);
b4.setForeground(Color.red);b4.setBackground(Color.white);b11.setForeground(Color.red);b11.setBackground(Color.white);
b18.setForeground(Color.red);b18.setBackground(Color.white);b25.setForeground(Color.red);b25.setBackground(Color.white);
b5.setForeground(Color.blue);b5.setBackground(Color.white);b6.setForeground(Color.blue);b6.setBackground(Color.white);
b7.setForeground(Color.blue);b7.setBackground(Color.white);b12.setForeground(Color.blue);b12.setBackground(Color.white);
b13.setForeground(Color.blue);b13.setBackground(Color.white);b14.setForeground(Color.blue);b14.setBackground(Color.white);
b19.setForeground(Color.blue);b19.setBackground(Color.white);b20.setForeground(Color.blue);b20.setBackground(Color.white);
b21.setForeground(Color.blue);b21.setBackground(Color.white);b26.setForeground(Color.blue);b26.setBackground(Color.white);
b27.setForeground(Color.blue);b27.setBackground(Color.white);b28.setForeground(Color.blue);b28.setBackground(Color.white);
b29.setForeground(Color.red);b29.setBackground(Color.white);b30.setForeground(Color.red);b30.setBackground(Color.white);
b31.setForeground(Color.red);b31.setBackground(Color.white);
bg.add(rb1);bg.add(rb2);
p1.setBackground(Color.yellow);
cp.setBackground(Color.CYAN);
m1.setMnemonic(KeyEvent.VK_F);m2.setMnemonic(KeyEvent.VK_E);m3.setMnemonic(KeyEvent.VK_H);
m1.add(mt6);m1.add(mt7);m1.addSeparator();m1.add(mt1);m1.addSeparator();m1.add(mt2);m2.add(mt3);m2.addSeparator();m2.add(mt4);m3.add(mt5);
mb.add(m1);mb.add(m2);mb.add(m3);
frame.setJMenuBar(mb);
p2.setLayout(new GridLayout(4,7));
p3.setLayout(new GridLayout(1,3));
ta.setEditable(false);
p1.add(ta);
p2.add(b1);p2.add(b2);p2.add(b3);p2.add(b4);p2.add(b5);p2.add(b6);p2.add(b7);
p2.add(b8);p2.add(b9);p2.add(b10);p2.add(b11);p2.add(b12);p2.add(b13);p2.add(b14);
p2.add(b15);p2.add(b16);p2.add(b17);p2.add(b18);p2.add(b19);p2.add(b20);p2.add(b21);
p2.add(b22);p2.add(b23);p2.add(b24);p2.add(b25);p2.add(b26);p2.add(b27);p2.add(b28);
p3.add(b29);p3.add(b30);p3.add(b31);
Border etched=BorderFactory.createEtchedBorder();
Border border=BorderFactory.createTitledBorder(etched,"计算类型");
p4.add(rb1);p4.add(rb2);
p4.setBorder(border);
b2.setActionCommand("8");
b2.addActionListener(this);
cp.add(p1);cp.add(p4);cp.add(p2);cp.add(p3);
frame.setSize(400,330);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.setActionCommand("7");
b1.addActionListener(this);
b2.setActionCommand("8");
b2.addActionListener(this);
b3.setActionCommand("9");
b3.addActionListener(this);
b4.setActionCommand("/");
b4.addActionListener(this);
b5.setActionCommand("1/x");
b5.addActionListener(this);
b6.setActionCommand("sin");
b6.addActionListener(this);
b7.setActionCommand("log");
b7.addActionListener(this);
b8.setActionCommand("4");
b8.addActionListener(this);
b9.setActionCommand("5");
b9.addActionListener(this);
b10.setActionCommand("6");
b10.addActionListener(this);
b11.setActionCommand("*");
b11.addActionListener(this);
b12.setActionCommand("x^y");
b12.addActionListener(this);
b13.setActionCommand("cos");
b13.addActionListener(this);
b14.setActionCommand("ln");
b14.addActionListener(this);
b15.setActionCommand("1");
b15.addActionListener(this);
b16.setActionCommand("2");
b16.addActionListener(this);
b17.setActionCommand("3");
b17.addActionListener(this);
b18.setActionCommand("-");
b18.addActionListener(this);
b19.setActionCommand("x!");
b19.addActionListener(this);
b20.setActionCommand("tan");
b20.addActionListener(this);
b21.setActionCommand("x^3");
b21.addActionListener(this);
b22.setActionCommand("0");
b22.addActionListener(this);
b23.setActionCommand("+/-");
b23.addActionListener(this);
b24.setActionCommand(".");
b24.addActionListener(this);
b25.setActionCommand("+");
b25.addActionListener(this);
b26.setActionCommand("√x");
b26.addActionListener(this);
b27.setActionCommand("cot");
b27.addActionListener(this);
b28.setActionCommand("x^2");
b28.addActionListener(this);
b29.setActionCommand("Backspace");
b29.addActionListener(this);
b30.setActionCommand("C");
b30.addActionListener(this);
b31.setActionCommand("=");
b31.addActionListener(this);
rb1.setActionCommand("kxx");
rb1.addActionListener(this);
rb2.setActionCommand("bzx");
rb2.addActionListener(this);
}
public void actionPerformed(ActionEvent e) //throws Exception
{
if (e.getActionCommand()=="bzx")
{
b5.setEnabled(false);b6.setEnabled(false);b7.setEnabled(false);
b12.setEnabled(false);b13.setEnabled(false);b14.setEnabled(false);
b19.setEnabled(false);b20.setEnabled(false);b21.setEnabled(false);
b26.setEnabled(false);b27.setEnabled(false);b28.setEnabled(false);
}
if (e.getActionCommand()=="kxx")
{
b5.setEnabled(true);b6.setEnabled(true);b7.setEnabled(true);
b12.setEnabled(true);b13.setEnabled(true);b14.setEnabled(true);
b19.setEnabled(true);b20.setEnabled(true);b21.setEnabled(true);
b26.setEnabled(true);b27.setEnabled(true);b28.setEnabled(true);
}
if (e.getActionCommand()=="1")
{
ta.append("1");
}
if (e.getActionCommand()=="2")
{
ta.append("2");
}
if (e.getActionCommand()=="3")
{
ta.append("3");
}
if (e.getActionCommand()=="4")
{
ta.append("4");
}
if (e.getActionCommand()=="5")
{
ta.append("5");
}
if (e.getActionCommand()=="6")
{
ta.append("6");
}
if (e.getActionCommand()=="7")
{
ta.append("7");
}
if (e.getActionCommand()=="8")
{
ta.append("8");
}
if (e.getActionCommand()=="9")
{
ta.append("9");
}
if (e.getActionCommand()=="0")
{
ta.append("0");
}
if (e.getActionCommand()=="+")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=1;
}
if (e.getActionCommand()=="-")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=2;
}
if (e.getActionCommand()=="*")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=3;
}
if (e.getActionCommand()=="/")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=4;
}
if (e.getActionCommand()=="=")
{
s2=ta.getText();
d2=Double.parseDouble(s2);
if(i==1)
{
d3=d1+d2;
ta.setText( d3.toString());
}
if(i==2)
{
d3=d1-d2;
ta.setText( d3.toString());
}
if(i==3)
{
d3=d1*d2;
ta.setText( d3.toString());
}
if(i==4)
{
if(d2==0.0)
ta.setText("ERROR");
else
{
d3=d1/d2;
ta.setText( d3.toString());
}
}
if (i==5)
{
s2=ta.getText();
d2 = Double.parseDouble(s2);
for (int l=1;l=d2 ; l++)
{
d5=d5*d1;
}
ta.setText( d5.toString());
}
}
if (e.getActionCommand()=="C")
{
ta.setText("");
d4=1.0;
d5=1.0;
}
/*if (e.getActionCommand()=="Backspace")
{
s3=ta.getText();
a=s3.length();
//ta.cut(ta.select(a-1,a));
s4=ta.getText(1,3);
ta.setText(s4);
}
*/
if (e.getActionCommand()=="1/x")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=1/d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()==".")
{
ta.append(".");
}
if (e.getActionCommand()=="+/-")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=0-d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x^2")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=d1*d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x^3")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=d1*d1*d1;
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x^y")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
ta.setText("");
i=5;
// d2=d1*d1*d1;
// ta.setText( d2.toString());
}
if (e.getActionCommand()=="√x")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=Math.sqrt(d1);
ta.setText( d2.toString());
}
if (e.getActionCommand()=="x!")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
if (d10)
{
ta.setText( "error");
}
else if (d1==0)
{
ta.setText( "0.0");
}
else {
for (int k=1;k=d1 ;k++ )
d4=d4*k;
ta.setText( d4.toString());
}
}
if (e.getActionCommand()=="sin")
{
s1=ta.getText();
d1 = Double.parseDouble(s1);
d2=Math.sin(3.1415926*d1/180);
ta.setText( d2.toString());
}
}
}
Q4: 求java小游戏源代码
表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盘上每个小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盘的尺寸 – 包括黑色的轮廓线 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的颜色为深绿色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖动标记 --当用户在棋子上按下鼠标按键时设为true, // 释放鼠标按键时设为false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盘左上角的左方向坐标 int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盘左上角的上方向坐标 int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的左方向坐标 int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的上方向坐标 int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移 int rely; // Width of applet drawing area. // applet绘图区域的宽度 int width; // Height of applet drawing area. // applet绘图区域的高度 int height; // Image buffer. // 图像缓冲 Image imBuffer; // Graphics context associated with image buffer. // 图像缓冲相关联的图形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 获取applet绘图区域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 创建图像缓冲 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出图像缓冲相关联的图形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盘的原点,使棋盘在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 获取按键时的鼠标坐标 int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按键时如果鼠标位于可拖动的棋子上方 // (也就是contains (x, y)返回true),则保存当前 // 鼠标坐标与棋子的原点之间的距离(始终为正值)并且 // 将拖动标志设为true(用来表明正处在拖动过程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 计算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍处于棋子范围内则返回true // CHECKERDIM / 2为半径 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 当鼠标按键被释放时,如果inDrag已经为true, // 则将其置为false(用来表明不在拖动过程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 计算棋子新的原点(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)没有被 // 移出棋盘,则将之前计算的原点 // (tmpox, tmpoy)赋值给永久性的原点(ox, oy), // 并且刷新显示区域(此时的棋子已经位于新坐标上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子将要被拖动的位置上绘制棋盘 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 绘制即将被拖动的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 绘制图像缓冲的内容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 设置棋子阴影的颜色 g.setColor (Color.black); // Paint checker shadow. // 绘制棋子的阴影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 设置棋子颜色 g.setColor (Color.red); // Paint checker. // 绘制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 绘制棋盘轮廓线 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 绘制棋盘 for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。该方法从 // Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除 // 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来 // 防止背景被清除,从而消除了闪烁。 public void update (Graphics g) { paint (g); }}
Q5: 跪地求好玩的JAVA 源代码~
连连看java源代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出,重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols+1][rows+1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins=15;twins++) {
randoms=(int)(Math.random()*25+1);
for(int alike=1;alike=2;alike++) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())+100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i=6;i++) {
for(int j=0;j=5;j++) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n++;
}
}
}
n=n-1;
this.grid=grid;
while(n=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6+1);
rows=(int)(Math.random()*5+1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //这里一定要将按钮点击信息归为初始
init();
for(int i = 0;i 6;i++){
for(int j = 0;j 5;j++ ){
if(grid[i+1][j+1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsg secondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情况下能不能消去。仔细分析,不一条条注释
if((x0==x (y0==y+1||y0==y-1)) || ((x0==x+1||x0==x-1)(y0==y))){ //判断是否相邻
remove();
}
else{
for (j=0;j7;j++ ) {
if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (yj) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i=y-1;i=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i=y+1;i=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0x) {
for (n=x0;n=x-1;n++ ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x-1) {
remove();
}
}
}
if (x0x) {
for (n=x0;n=x+1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0 n==x+1) {
remove();
}
}
}
}
}
for (i=0;i8;i++ ) { //列
if (grid[i][y0]==0) {
if (xi) {
for (j=x-1;j=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
for (j=x+1;j=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0y) {
for (n=y0;n=y-1 ;n++ ) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y-1) {
remove();
}
}
}
if (y0y) {
for (n=y0;n=y+1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0 n==y+1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0j){ //第一按钮同行空按钮在左边
for (i=y0-1;i=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2说明通过了第二次验证
}
}
if (y0j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0+1;i=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0i) {
for (j=x0+1;j=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols 6;cols++){
for(int rows = 0;rows 5;rows++ ){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols+1,rows+1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
基于JAVA的3D坦克游戏源代码
JAVA猜数字小游戏源代码
/*1、编写一个猜数字的游戏,由电脑随机产生一个100以内的整数,让用户去猜,如果用户猜的比电脑大,则输出“大了,再小点!”,反之则输出“小了,再大点!”,用户总共只能猜十次,并根据用户正确猜出答案所用的次数输出相应的信息,如:只用一次就猜对,输出“你是个天才!”,八次才猜对,输出“笨死了!”,如果十次还没有猜对,则游戏结束!*/
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("请输入一个100以内的整数:");
for (int i=0;i=9;i++){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shunum)
System.out.println("输入的数大了,输小点的!");
else if (shunum)
System.out.println("输入的数小了,输大点的!");
else {
System.out.println("恭喜你,猜对了!");
if (i=2)
System.out.println("你真是个天才!");
else if (i=6)
System.out.println("还将就,你过关了!");
else if (i=8)
System.out.println("但是你还……真笨!");
else
System.out.println("你和猪没有两样了!");
break;}
}
}
}
java经典案例源代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java编程经典案例、java经典案例源代码的信息别忘了在本站进行查找喔。







