
正文
java源代码手册 java源代码是什么格式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
JAVA 高手请进
这个是我原来的笔记整理,送你java源代码手册了!
貌似有点长,删掉一些无关紧要的
第一课
HelloJava
1、Java开发工具JDK的安装
2、 JDK的命令工具
JDK的最重要命令行工具:
java: 启动JVM执行class
javac: Java编译器
jar: Java打包工具
javadoc: Java文档生成器
这些命令行必须要非常非常熟悉,对于每个参数都要很精通才行。对于这些命令的学习,JDK Documentation上有详细的文档。
二、 JDK Documentation
Documentation在JDK的下载页面也有下载连接,建议同时下载Documentation。Documentation是最最重要的编程手册,涵盖了整个Java所有方面的内容的描述。可以这样说,学习Java编程,大部分时间都是花在看这个Documentation上面的。我是随身携带的,写Java代码的时候,随时查看,须臾不离手。
四、 Java应用的运行环境
Java Learning Path(三)过程篇
学习Java的第一步是安装好JDK,写一个Hello World,? 其实JDK的学习没有那幺简单,关于JDK有两个问题是很容易一直困扰Java程序员的地方:一个是CLASSPATH的问题,其实从原理上来说,是要搞清楚JRE的ClassLoader是如何加载Class的java源代码手册;另一个问题是package和import问题,如何来寻找类的路径问题。把这两个问题摸索清楚了,就扫除了学习Java和使用JDK的最大障碍。推荐看一下王森的《Java深度历险》,对这两个问题进行了深入的探讨。
第二步是学习Java的语法。Java的语法是类C++的,基本上主流的编程语言不是类C,就是类C++的,没有什幺新东西,所以语法的学习,大概就是半天的时间足够了。唯一需要注意的是有几个不容易搞清楚的关键字的用法,public,protected,private,static,什幺时候用,为什幺要用,怎幺用,这可能需要有人来指点一下,我当初是完全自己琢磨出来的,花了很久的时间。不过后来我看到《Thinking in Java》这本书上面是讲了这些概念的。
第三步是学习Java的面向对象的编程语言的特性的地方。比如继承,构造器,抽象类,接口,方法的多态,重载,覆盖,Java的异常处理机制。对于一个没有面向对象语言背景的人来说,我觉得这个过程需要花很长很长时间,因为学习Java之前没有C++的经验,只有C的经验,我是大概花了一个月左右吧,才彻底把这些概念都搞清楚,把书上面的例子反复的揣摩,修改,尝试,把那几章内容反复的看过来,看过去,看了不下5遍,才彻底领悟了。不过我想如果有C++经验的话,应该一两天时间足够了。那幺在这个过程中,可以多看看《Thinking in Java》这本书,对面向对象的讲解非常透彻。可惜的是我学习的时候,并没有看到这本书,所以自己花了大量的时间,通过自己的尝试和揣摩来学会的。
第四步就是开始熟悉Java的类库。Java的基础类库其实就是JDK安装目录下面jrelibrt.jar这个包。学习基础类库就是学习rt.jar。基础类库里面的类非常非常多。据说有3000多个,我没有统计过。但是真正对于我们来说最核心的只有4个,分别是
java.lang.*;
java.io.*;
java.util.*;
java.sql.*;
这四个包的学习,每个包的学习都可以写成一本厚厚的教材,而O'reilly也确实是这样做的。我觉得如果时间比较紧,是不可能通过读四本书来学习。我觉得比较好的学习方法是这样的:
首先要通读整个package的框架,了解整个package的class,interface,exception的构成,最好是能够找到介绍整个包框架的文章。这些专门介绍包的书籍的前几章应该就是这些总体的框架内容介绍。
对包整体框架的把握并不是要熟悉每个类的用法,记住它有哪些属性,方法。想记也记不住的。而是要知道包有哪些方面的类构成的,这些类的用途是什幺,最核心的几个类分别是完成什幺功能的。我在给人培训的时候一般是一次课讲一个包,所以不可能详细的介绍每个类的用法,但是我反复强调,我给你们讲这些包的不是要告诉你们类的方法是怎幺调用的,也不要求你们记住类的方法调用,而是要你们了解,Java给我们提供了哪些类,每个类是用在什幺场合,当我遇到问题的时候,我知道哪个类,或者哪几个类的组合可以解决我的问题,That'all!,当我们具体写程序的时候,只要你知道该用哪个类来完成你的工作就足够了。编码的时候,具体的方法调用,是边写代码,边查Documentation,所有的东西都在Documentation里面,不要求你一定记住,实际你也记不住3000多个类的总共将近10万个方法调用。所以对每个包的总体框架的把握就变得极为重要。
第五步,通过上面的学习,如果学的比较扎实的话,就打好了Java的基础了,剩下要做的工作是扫清Documentation里面除了上面4个包之外的其它一些比较有用处的类。相信进展到这一步,Java的自学能力已经被培养出来了,可以到了直接学习Documentation的水平了。除了要做GUI编程之外,JDK里面其它会有用处的包是这些:
java.text.*;
java.net.*;
javax.naming.*;
这些包里面真正用的比较多的类其实很少,只有几个,所以不需要花很多时间。
第六步,Java Web 编程,Web编程的核心是HTTP协议,HTTP协议和Java无关,如果不熟悉HTTP协议的话,虽然也可以学好Servlet/JSP编程,但是达不到举一反三,一通百通的境界。所以HTTP协议的学习是必备的。如果熟悉了HTTP协议的话,又有了Java编程的良好的基础,学习Servlet/JSP简直易如反掌,我学习Servlet/JSP就用了不到一周的时间,然后就开始用JSP来做项目了。
在Servlet/JSP的学习中,重头仍然是Servlet Documentation。Servlet API最常用的类很少,花比较少的时间就可以掌握了。把这些类都看一遍,多写几个例子试试。Servlet/JSP编程本质就是在反复调用这些类来通过HTTP协议在Web Server和Brower之间交谈。另外对JSP,还需要熟悉几个常用JSP的标记,具体的写法记不住的话,临时查就是了。
此外Java Web编程学习的重点要放在Web Application的设计模式上,如何进行业务逻辑的分析,并且进行合理的设计,按照MVC设计模式的要求,运用Servlet和JSP分别完成不同的逻辑层,掌握如何在Servlet和JSP之间进行流程的控制和数据的共享,以及Web Application应该如何配置和部署。
第七步,J2EE编程
以上的学习过程如果是比较顺利的话,进行到这一步,难度又陡然提高。因为上面的知识内容都是只涉及一个方面,而像EJB,JMS,JTA等核心的J2EE规范往往是几种Java技术的综合运用的结晶,所以掌握起来难度比较大。
首先一定要学习好JNDI,JNDI是App Server定位服务器资源(EJB组件,Datasouce,JMS)查找方法,如果对JNDI不熟悉的话,EJB,JMS这些东西几乎学不下去。JNDI其实就是javax.naming.*这个包,运用起来很简单。难点在于服务器资源文件的配置。对于服务器资源文件的配置,就需要看看专门的文档规范了,比如web.xml的写法,ejb-jar.xml的写法等等。针对每种不同的App Server,还有自己的服务资源配置文件,也是需要熟悉的。
然后可以学习JTA,主要是要理解JTA对于事务的控制的方法,以及该在什幺场合使用JTA。这里可以简单的举个例子,我们知道一般情况可以对于一个数据库连接进行事务控制(conn.setAutoCommit(false),....,conn.commit()),做为一个原子操作,但是假设我的业务需求是要把对两个不同数据库的操作做为一个原子操作,你能做的到吗?这时候只能用JTA了。假设操作过程是先往A数据库插一条记录,然后删除B数据库另一个记录,我们自己写代码是控制不了把整个操作做为一个原子操作的。用JTA的话,由App Server来完成控制。
在学习EJB之前要学习对象序列化和RMI,RMI是EJB的基础。接着学习JMS和EJB,对于EJB来说,最关键是要理解EJB是如何通过RMI来实现对远端对象的调用的,以及在什幺情况下要用到EJB。
在学习完EJB,JMS这些东西之后,你可能会意识到要急不可待学习两个领域的知识,一个是UML,另一个是Design Pattern。Java企业软件的设计非常重视框架(Framework)的设计,一个好的软件框架是软件开发成功的必要条件。在这个时候,应该开始把学习的重点放在设计模式和框架的学习上,通过学习和实际的编程经验来掌握EJB的设计模式和J2EE的核心模式。
J2EE规范里面,除了EJB,JMS,JTA,Servlet/JSP,JDBC之外还有很多很多的企业技术,这里不一一进行介绍了。
另外还有一个最新领域Web Services。Web Services也完全没有任何新东西,它像是一种粘合剂,可以把不同的服务统一起来提供一个统一的调用接口,作为使用者来说,我只要获得服务提供者给我的WSDL(对服务的描述),就够了,我完全不知道服务器提供者提供的服务究竟是EJB组件,还是.Net组件,还是什幺CORBA组件,还是其它的什幺实现,我也不需要知道。Web Services最伟大的地方就在于通过统一的服务提供方式和调用方式,实现了整个Internet服务的共享,是一个非常令人激动的技术领域。Web Services好象目前还没有什幺很好的书籍,但是可以通过在网络上面查资料的方式来学习。
所以我觉得其实不是特别需要例程的,自己写的破坏例程就是最好的例子,如果你实在对自己写的代码不放心的话,我强烈推荐你看看JDK基础类库的Java源代码。在JDK安装目录下面会有一个src.zip,解开来就可以完整的看到整个JDK基础类库,也就是rt.jar的Java源代码,你可以参考一下Sun是怎幺写Java程序的,规范是什幺样子的。我自己在学习Java的类库的时候,当有些地方理解的不是很清楚的时候,或者想更加清晰的理解运作的细节的时候,往往会打开相应的类的源代码,通过看源代码,所有的问题都会一扫而空。
2、Java环境变量配置
1. 预备知识1.1安装JDK到 根据不同的操作系统,下载相应的J2SE JDK 版本 (写这篇文章时最新的JDK 为1.5 update 7)。假定我们把JDK安装到D:\JDK\1507 ,不安装public JRE.
1.2 设置path在[我的电脑]上右击,选择[属性].再选择[高级]-[环境变量],在[系统变量]中添加一个新的变量JAVA_HOME=D:\JDK\1507,然后再编辑path变量,在原来的path前面添加[ %JAVA_HOME%\BIN; ](方框中的文字)。这样就完成了JAVA的基本设置。(windows 98 下如何在autoexec.bat 中设置,请自己找资料)
1.3 演示程序
为了帮助理解,我写了一个JavaBootTest.java 的小程序来显示。
public class JavaBootTest {
public static void main(String[] args) {
System.out.println(" The Classpath are: ");
System.out.println(System.getProperty("java.class.path"));
//other program lines
}
}
用下面的两条命令分别编译和运行。
javac JavaBootTest.java
java JavaBootTest
2. JAVA程序运行时如何查找类文件======================================================================
根据JDK 文档说明, JAVA 程序以以下3种顺序查找运行的类文件。
1.Bootstrap classes (*)
2.Extension classes
3.Users classes
如何设置CLASSPATH,是初学者常提出的问题。网上有很多文章提到了许多如何设置的方法,但是都没有讲为什么要这样设置(令人感到悲哀的是:绝大部分的人的观点是不正确的)。
为了帮助大家更好的学习JAVA的基本知识,同时也是锻炼自己的Type水平,于是有了这篇文章的诞生。主要参考资料来自来自官方JDK文档,希望对大家有用(所说的观点针对1.4.2 and 5.0版本)。
------------------------------------------------------------------------------------------------------------------------------
1. 预备知识1.1安装JDK到 根据不同的操作系统,下载相应的J2SE JDK 版本 (写这篇文章时最新的JDK 为1.5 update 7)。假定我们把JDK安装到D:\JDK\1507 ,不安装public JRE.
1.2 设置path在[我的电脑]上右击,选择[属性].再选择[高级]-[环境变量],在[系统变量]中添加一个新的变量JAVA_HOME=D:\JDK\1507,然后再编辑path变量,在原来的path前面添加[ %JAVA_HOME%\BIN; ](方框中的文字)。这样就完成了JAVA的基本设置。(windows 98 下如何在autoexec.bat 中设置,请自己找资料)
1.3 演示程序
为了帮助理解,我写了一个JavaBootTest.java 的小程序来显示。
public class JavaBootTest { public static void main(String[] args) { System.out.println(" The Classpath are: ");System.out.println(System.getProperty("java.class.path"));//other program lines }
用下面的两条命令分别编译和运行。
javac JavaBootTest.java java JavaBootTest
2. JAVA程序运行时如何查找类文件======================================================================根据JDK 文档说明, JAVA 程序以以下3种顺序查找运行的类文件。
1.Bootstrap classes (*)
2.Extension classes 3.Users classes
2.1 Bootstrap classes就是JAVA在启动时载入的类文件,这些类文件主要是rt.jar 和 jre/lib 目录下的一些类文件。Bootstrap过程中的class path是保存在 sun.boot.class.path 系统属性中的。可以通过System.out.println(System.getProperty("sun.boot.class.path")); 来显示。同时Bootstrap classes 可以通过 -Xbootclasspath 命令行参数来指定。
下面列出了系统中默认的Bootstrap classes:
* Bootstrap bootstrap是皮鞋后部的一条小带子或一个小环,它可以使你方便地把鞋子穿起来。在计算机中,是指使用一个很小的程序将某个特定的程序(通常是指操作系统)载入计算机中。
2.2 Extension classes (扩展类文件)
Extension classes 主要是指的jre/lib/ext 目录下的类文件,这些文件必须在jar 文件或 zip 文件中。如果不同名字的jar 文件包含有相同的类文件,那么哪一个类文件被载入是不确定的。
2.3 User classes (用户自定义的类文件)
现在到了最重要的地方了,我们常说的设置CLASSPATH 其实就是指定 User classes.JAVA 按照以下四种顺序查找User classes. 2.3.1. 默认的User classes . (dot) 就是指当前目录。
2.3.2. 系统变量 CLASSPATH 所指定的类库,该变量覆盖(override)默认的User classes. 2.3.3. 用命令行参数 -cp 或 -classpath指定的类库。这个时候覆盖默认的User classes 和CLASSPATH变量。
2.3.4. 通过-jar 参数指定的jar文件。此时覆盖上面的三种情况,如果使用这种情况,所有的类文件必须来自指定的jar 文件。
了解了java如何查找User classes,我们接下来将要学习如何设置 CLASSPATH变量。
3 如何正确设置CLASSPATH如果只是一般运用java,只是用到java的基本库文件,练练手而已,则不需要设置CLASSPATH .但是在一般开发情况下,这是很少遇到的。所以我们要自己设置CLASSPATH .我们一般的方法就是按照2.3.2 来设置CLASSPATH 系统变量,像开始设置path一样,我们可以添加一个CLASSPATH的系统变量。网上有些文章所提到的添加rt.jar 和 tools.jar 等等是不需要的,因为这些类库是属于Bootstrap classes的。我们只要定义User classes ,如果我们要编译servlet 那么只要servlet-api.jar 和 jsp-api.jar 就可以实现基本的需要了。
另外要注意的是2.3.2 override 2.3.1.我们设置CLASSPATH 时要将 .(dot)[表示当前目录]放在CLASSPATH中,然后用 ;(semicolon)分隔开来。由于其他提供的类文件都是放在jar文件中,我们设置时一定要将完整的jar 文件包含在CLASSPATH 中,而不是将其目录添加到CLASSPATH 中(很重要的一点,我以前就是理解错误了)。
这个时候又一个问题出现了,当需要添加的jar 文件过多时,管理java 类库变得很麻烦。所以我认为在基础学习时用命令行编译调试,有利于JAVA的学习,但是到开发时则一定要用到 IDE 工具(现在比较流行的是eclipse 和 netbeans ,还有些人喜欢 jcreator) .在eclipse 中通过 [windows][Preferences][Java][Build Path]来指定。其中有两部分Classpath Variables User Libraries.如何设置在这里就不详细说明了,自己试一下就可以了,比较简单。在新建项目时,把自己定义的变量添加到Build Path 中就可以了。
3、HelloWorld的编写
Public Welcome
{
Public Static Void main(String[] args)
{
System.out.println("Hello Java");
}
}
4、Javac Java工具的使用
5、HelloJava中存在的问题:(网络转载)
* 错误1:
'javac' 不是内部或外部命令,也不是可运行的程序或批处理文件。
(javac: Command not found)
产生的原因是没有设置好环境变量path。Win98下在autoexce.bat中加入
path=%path%;c:jdk1.2in,Win2000下则控制面板-系统-高级-环境变量-系统变
量...看到了?双击Path,在后面加上c:jdk1.2in。当然我们假设JDK安装在了
c:jdk1.2目录下(有点唐僧了?)...好像还要重启系统才起作用...(//知道了!//西
红柿)
好,再试试!javac HelloWorld
* 错误2:
HelloWorld is an invalid option or argument.
拜托,给点专业精神,java的源程序是一定要存成.java文件的,而且编译时要写全
.java呀。
OK, javac HelloWorld.java (这回总该成了吧?)
* 错误3:
HelloWorld.java:1: Public class helloworld must be defined in a file called
"HelloWorld.java".
public class helloworld{
^
这个问题嘛,是因为你的类的名字与文件的名字不一致。(谁说的,明明看到人家都有
这样写的 ;( ) OK,准确地说,一个Java源程序中可以定义多个类,但是,具有public
属性的类只能有一个,而且要与文件名相一致。还有,main方法一定要放在这个public
的类之中,这样才能java(运行)这个类。另外一点是Java语言里面是严格区分大小写
的,初学者要注意呀。像上例中 helloworld 与 HelloWorld 就认为是不一样,因而...
oh... 好,改好了,嘻嘻... javac HelloWorld.java
...(咦,怎么什么也没有呀?)//faint 这就是编译通过了!看看是不是多了一个
HelloWorld.class ?
(hehe..按书上教的:) java HelloWorld (!! 这个我知道,不是java HelloWorld.class
哟)
* 错误4:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
呵呵,这个嘛,就是著名的类路径(classpath)问题啦。实际上,类路径是在编译过程就
涉及的Java中的概念。classpath就是指明去哪里找用到的类,就这么简单。由于我们的
HelloWorld没用到其它的(非java.lang包中的)类,所以编译时没遇到这个问题。运行
时呢,就要指明你的类在哪里了。解决方法嘛,可以用下面的命令运行:
java -classpath . HelloWorld
“.”就代表当前目录。当然这样做有点麻烦(是“太麻烦”!),我们可以在环境变量
中设置默认的classpath。方法就照上述设置path那样。将classpath设为:
classpath=.;c:jdk1.2libdt.jar;c:jdk1.2lib ools.jar 后面的两个建议也设上
,以后开发用的着。
java -classpath . HelloWorld(再不出来我就不学java了)
* 错误5:
Exception in thread "main" java.lang.NoSuchMethodError: main
(//咣当)别,坚持住。看看你的代码,问题出在main方法的定义上,写对地方了吗,
是这样写的吗:
public static void main(String args[]) { //一个字都不要差,先别问为什么了...
对,包括大小写!
java -classpath . HelloWorld (听天由命了!)
Hello World!
(faint!终于...)
欢迎来到Java世界!所以说,无法运行HelloWorld 真的并不是一个“最简单的问题”。
附:HelloWorld.java
// HelloWorld.java
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!") ;
}
}
相关问答
Q1: java源代码如何用
java源代码是用来关联jar中的编译代码的。
应用场景:通常在打开eclipse中的jar的时候,发现class文件不能被打开,此时出现下面的界面,此时就需要通过“Attach Source”,之后找到对应的java源代码来实现代码关联,即可正常的显示class类中的内容。
备注:如果此处ava源代码指的是源代码文件(“.java”),是用来进行代码维护和二次开发的必备东西。
Q2: 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());
}
}
}
Q3: 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; //退出java源代码手册,重列java源代码手册,重新开始按钮
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说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i=y+1;i=j ;i++ ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0){
k=0;
break;
}
else
}
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
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
for (j=x+1;j=i;j++ ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else
}
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说明通过了第二次验证
}
}
if (y0j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0+1;i=j ;i++){
if (grid[x0][i]!=0) {
k=0;
break;
}
else
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else
}
}
if (x0i) {
for (j=x0+1;j=i ;j++ ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else
}
}
}
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源代码手册的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java源代码是什么格式、java源代码手册的信息别忘了在本站进行查找喔。




