??xml version="1.0" encoding="utf-8" standalone="yes"?>
final—修饰符Q关键字Q如果一个类被声明ؓfinalQ意味着它不能再z出新的子c,不能作ؓ父类被ѝ因此一个类不能既被声明?abstract的,又被声明为final的。将变量或方法声明ؓfinalQ可以保证它们在使用中不被改变。被声明为final的变量必d声明时给定初|而在以后的引用中只能dQ不可修攏V被声明为final的方法也同样只能使用Q不能重载?
finally—再异常处理时提?finally 块来执行M清除操作。如果抛Z个异常,那么相匹配的 catch 子句׃执行Q然后控制就会进?finally 块(如果有的话)?
finalize—方法名。Java 技术允怋?finalize() Ҏ在垃圾收集器对象从内存中清除出M前做必要的清理工作。这个方法是由垃圾收集器在确定这个对象没有被引用时对q个对象调用的。它是在 Object cM定义的,因此所有的c都l承了它。子c覆?finalize() Ҏ以整理系l资源或者执行其他清理工作。finalize() Ҏ是在垃圾攉器删除对象之前对q个对象调用的?
W二QAnonymous Inner Class (匿名内部c? 是否可以extends(l承)其它c,是否可以implements(实现)interface(接口)?
匿名的内部类是没有名字的内部cR不能extends(l承) 其它c,但一个内部类可以作ؓ一个接口,由另一个内部类实现?
W三QStatic Nested Class ?Inner Class的不同,说得多好(面试题有的很W统)?
Nested Class Q一般是C++的说法)QInner Class (一般是JAVA的说?。Java内部cMC++嵌套cL大的不同在于是否有指向外部的引用上。具体可见http: //www.frontfree.net/articles/services/view.asp?id=704&page=1
注: 静态内部类QInner ClassQ意味着1创徏一个static内部cȝ对象Q不需要一个外部类对象Q?不能从一个static内部cȝ一个对象访问一个外部类对象
W四Q?amp;?amp;&的区别?
&是位q算W?amp;&是布逻辑q算W?
W五QHashMap和Hashtable的区别?
都属于Map接口的类Q实C惟一键映到特定的g?
HashMap cL有分cL者排序。它允许一?null 键和多个 null 倹{?
Hashtable cM?HashMapQ但是不允许 null 键和 null 倹{它也比 HashMap 慢,因ؓ它是同步的?
W六QCollection ?Collections的区别?
Collections是个java.util下的c,它包含有各种有关集合操作的静态方法?
Collection是个java.util下的接口Q它是各U集合结构的父接口?
W七Q什么时候用assert?
断言是一个包含布表辑ּ的语句,在执行这个语句时假定该表辑ּ?true。如果表辑ּ计算?falseQ那么系l会报告一?Assertionerror。它用于调试目的Q?
assert(a > 0); // throws an Assertionerror if a <= 0
断言可以有两UŞ式:
assert Expression1 ;
assert Expression1 : Expression2 ;
Expression1 应该L产生一个布倹{?
Expression2 可以是得Z个值的L表达式。这个值用于生成显C更多调试信息的 String 消息?
断言在默认情况下是禁用的。要在编译时启用断言Q需要?source 1.4 标记Q?
javac -source 1.4 Test.java
要在q行时启用断aQ可使用 -enableassertions 或?-ea 标记?
要在q行旉择用断言Q可使用 -da 或?-disableassertions 标记?
要系l类中启用断aQ可使用 -esa 或?-dsa 标记。还可以在包的基上启用或者禁用断a?
可以在预计正常情况下不会到达的Q何位|上攄断言。断a可以用于验证传递给U有Ҏ的参数。不q,断言不应该用于验证传递给公有Ҏ的参敎ͼ因ؓ不管是否启用了断aQ公有方法都必须查其参数。不q,既可以在公有Ҏ中,也可以在非公有方法中利用断言试后置条g。另外,断言不应该以M方式改变E序的状态?
W八QGC是什? Z么要有GC? (基础)?
GC是垃圾收集器。Java E序员不用担心内存管理,因ؓ垃圾攉器会自动q行理。要h垃圾攉Q可以调用下面的Ҏ之一Q?
System.gc()
Runtime.getRuntime().gc()
W九QString s = new String("xyz");创徏了几个String Object?
两个对象Q一个是“xyx?一个是指向“xyx”的引用对象s?
W十QMath.round(11.5){於多少? Math.round(-11.5){於多少?
Math.round(11.5)q回QlongQ?2QMath.round(-11.5)q回QlongQ?11;
W十一Qshort s1 = 1; s1 = s1 + 1;有什么错? short s1 = 1; s1 += 1;有什么错?
short s1 = 1; s1 = s1 + 1;有错Qs1是short型,s1+1是int?不能昑ּ转化为short型。可修改为s1 =(short)(s1 + 1) 。short s1 = 1; s1 += 1正确?
W十二,sleep() ?wait() 有什么区? 搞线E的最?
sleep()Ҏ是ɾU程停止一D|间的Ҏ。在sleep 旉间隔期满后,U程不一定立x复执行。这是因为在那个时刻Q其它线E可能正在运行而且没有被调度ؓ攑ּ执行Q除?a)“醒来”的U程h更高的优先?/p>
(b)正在q行的线E因为其它原因而阻塞?
wait()是线E交互时Q如果线E对一个同步对象x 发出一个wait()调用Q该U程会暂停执行,被调对象q入{待状态,直到被唤醒或{待旉到?
W十三,Java有没有goto?
Goto—java中的保留字,现在没有在java中用?
W十四,数组有没有length()q个Ҏ? String有没有length()q个ҎQ?
数组没有length()q个ҎQ有length的属性?
String有有length()q个Ҏ?
W十五,Overload和Override的区别。Overloaded的方法是否可以改变返回值的cd?
Ҏ的重写Overriding和重载Overloading是Java多态性的不同表现。重写Overriding是父cM子类之间多态性的一U表玎ͼ重蝲Overloading是一个类中多态性的一U表现。如果在子类中定义某Ҏ与其父类有相同的名称和参敎ͼ我们说该Ҏ被重?(Overriding)。子cȝ对象使用q个ҎӞ调用子cM的定义,对它而言Q父cM的定义如同被“屏蔽”了。如果在一个类中定义了多个同名的方法,它们或有不同的参C数或有不同的参数cdQ则UCؓҎ的重?Overloading)。Overloaded的方法是可以改变q回值的cd?
W十六,Set里的元素是不能重复的Q那么用什么方法来区分重复与否? 是用==q是equals()? 它们有何区别?
Set里的元素是不能重复的Q那么用iterator()Ҏ来区分重复与否。equals()是判M个Set是否相等?
equals()?=Ҏ军_引用值是否指向同一对象equals()在类中被覆盖Qؓ的是当两个分ȝ对象的内容和cd盔R的话Q返回真倹{?
W十七,l我一个你最常见到的runtime exception?
ArithmeticException, ArrayStoreException, BufferOverflowException, BufferUnderflowException, CannotRedoException, CannotUndoException, ClassCastException, CMMException, ConcurrentModificationException, DOMException, EmptyStackException, IllegalArgumentException, IllegalMonitorStateException, IllegalPathStateException, IllegalStateException,
ImagingOpException, MissingResourceException, NegativeArraySizeException, NoSuchElementException, NullPointerException, ProfileDataException, ProviderException, RasterFORMatException, SecurityException, SystemException, UndeclaredThrowableException, UnmodifiableSetException, UnsupportedOperationException
W十八,error和exception有什么区?
error 表示恢复不是不可能但很困隄情况下的一U严重问题。比如说内存溢出。不可能指望E序能处理这L情况?
exception 表示一U设计或实现问题。也是_它表C如果程序运行正常,从不会发生的情况?
W十九,List, Set, Map是否l承自Collection接口?
ListQSet?
Map不是
W二十,abstract class和interface有什么区?
声明Ҏ的存在而不d现它的类被叫做抽象类Qabstract classQ,它用于要创徏一个体现某些基本行为的c,qؓ该类声明ҎQ但不能在该cM实现该类的情c不能创建abstract cȝ实例。然而可以创Z个变量,其类型是一个抽象类Qƈ让它指向具体子类的一个实例。不能有抽象构造函数或抽象静态方法。Abstract cȝ子类为它们父cM的所有抽象方法提供实玎ͼ否则它们也是抽象cMؓ。取而代之,在子cM实现该方法。知道其行ؓ的其它类可以在类中实现这些方法?
接口QinterfaceQ是抽象cȝ变体。在接口中,所有方法都是抽象的。多l承性可通过实现q样的接口而获得。接口中的所有方法都是抽象的Q没有一个有E序体。接口只可以定义static final成员变量。接口的实现与子cȝ|除了该实现类不能从接口定义中l承行ؓ。当cd现特D接口时Q它定义Q即程序体l予Q所有这U接口的Ҏ。然后,它可以在实现了该接口的类的Q何对象上调用接口的方法。由于有抽象c,它允怋用接口名作ؓ引用变量的类型。通常的动态联~将生效。引用可以{换到接口cd或从接口cd转换Qinstanceof q算W可以用来决定某对象的类是否实现了接口?
W二十一Qabstract的method是否可同时是static,是否可同时是nativeQ是否可同时是synchronized?
都不?
W二十二Q接口是否可l承接口? 抽象cL否可实现(implements)接口? 抽象cL否可l承实体c?concrete class)?
接口可以l承接口。抽象类可以实现(implements)接口Q抽象类是否可承实体类Q但前提是实体类必须有明的构造函数?
W二十三Q启动一个线E是用run()q是start()?
启动一个线E是调用start()ҎQɾU程所代表的虚拟处理机处于可运行状态,q意味着它可以由JVM调度q执行。这q不意味着U程׃立即q行。run()Ҏ可以产生必须退出的标志来停止一个线E?
W二十四Q构造器Constructor是否可被override?
构造器Constructor不能被承,因此不能重写OverridingQ但可以被重载Overloading?
W二十五Q是否可以承Stringc?
StringcLfinalcL不可以ѝ?
W二十六Q当一个线E进入一个对象的一个synchronizedҎ后,其它U程是否可进入此对象的其它方?
不能Q一个对象的一个synchronizedҎ只能׃个线E访问?
W二十七Qtry {}里有一个return语句Q那么紧跟在q个try后的finally {}里的code会不会被执行Q什么时候被执行Q在return前还是后?
会执行,在return前执行?
W二十八Q编E题: 用最有效率的Ҏ出2乘以8{於?
有C背景的程序员特别喜欢问这U问题?
W二十九Q两个对象值相?x.equals(y) == true)Q但却可有不同的hash codeQ这句话对不?
不对Q有相同的hash code?
W三十,当一个对象被当作参数传递到一个方法后Q此Ҏ可改变这个对象的属性,q可q回变化后的l果Q那么这里到底是g递还是引用传?
是g递。Java ~程语言只由g递参数。当一个对象实例作Z个参数被传递到Ҏ中时Q参数的值就是对该对象的引用。对象的内容可以在被调用的方法中改变Q但对象的引用是永远不会改变的?
W三十一Qswtich是否能作用在byte上,是否能作用在long上,是否能作用在String?
switchQexpr1Q中Qexpr1是一个整数表辑ּ。因此传递给 switch ?case 语句的参数应该是 int?short?char 或?byte。long,string 都不能作用于swtich?
W三十二Q编E题: 写一个Singleton出来?
Singleton模式主要作用是保证在Java应用E序中,一个类Class只有一个实例存在?/p>
(1) cd首字母应该大写。字Dc方法以及对象(句柄Q的首字母应写。对于所有标识符Q其中包含的所有单词都应紧靠在一P而且大写中间单词的首字母。例如:
ThisIsAClassName
thisIsMethodOrFieldName
若在定义中出C常数初始化字W,则大写static final基本cd标识W中的所有字母。这样便可标志出它们属于~译期的常数?
Java包(PackageQ属于一U特D情况:它们全都是小写字母,即便中间的单词亦是如此。对于域名扩展名Uͼ如comQorgQnet或者edu{,全部都应写Q这也是Java 1.1和Java 1.2的区别之一Q?
(2) Z常规用途而创Z个类Ӟ请采取“经典Ş式”,q包含对下述元素的定义:
equals()
hashCode()
toString()
clone()Qimplement CloneableQ?
implement Serializable
(3) 对于自己创徏的每一个类Q都考虑|入一个main()Q其中包含了用于试那个cȝ代码。ؓ使用一个项目中的类Q我们没必要删除试代码。若q行了Q何Ş式的改动Q可方便地返回测试。这些代码也可作为如何用类的一个示例用?
(4) 应将Ҏ设计成简要的、功能性单元,用它描述和实C个不q箋的类接口部分。理x况下Q方法应明扼要。若长度很大Q可考虑通过某种方式其分割成较短的几个Ҏ。这样做也便于类内代码的重复使用Q有些时候,Ҏ必须非常大,但它们仍应只做同L一件事情)?
(5) 设计一个类Ӟ误w处Cؓ客户E序员考虑一下(cȝ使用Ҏ应该是非常明的Q。然后,再设w处Cؓ理代码的h考虑一下(预计有可能进行哪些Ş式的修改Q想想用什么方法可把它们变得更单)?
(6) 使类可能短精悍,而且只解决一个特定的问题。下面是对类设计的一些徏议:
■一个复杂的开兌句:考虑采用“多形”机?
■数量众多的Ҏ涉及到类型差别极大的操作Q考虑用几个类来分别实?
■许多成员变量在特征上有很大的差别:考虑使用几个c?
(7) 让一切东襉K可能地“私有”——private。可使库的某一部分“公共化”(一个方法、类或者一个字D늭{)Q就永远不能把它拿出。若拿出Q就可能破坏其他人现有的代码Q他们不得不重新编写和设计。若只公布自己必d布的Q就可放心大胆地改变其他M东西。在多线E环境中Q隐U是特别重要的一个因素——只有private字段才能在非同步使用的情况下受到保护?
(8) 谨惕“巨大对象综合症”。对一些习惯于序~程思维、且初涉OOP领域的新手,往往喜欢先写一个顺序执行的E序Q再把它嵌入一个或两个巨大的对象里。根据编E原理,对象表达的应该是应用E序的概念,而非应用E序本n?
(9) 若不得已q行一些不太雅观的~程Q至应该把那些代码|于一个类的内部?
(10) M时候只要发现类与类之间l合得非常紧密,需要考虑是否采用内部c,从而改善编码及l护工作Q参见第14?4.1.2节的“用内部cLq代码”)?
(11) 可能细致地加上注释Qƈ用javadoc注释文档语法生成自己的程序文?
(12) 避免使用“魔术数字”,q些数字很难与代码很好地配合。如以后需要修改它Q无疑会成ؓ一场噩梦,因ؓҎ不知道?00”到底是指“数l大”还是“其他全然不同的东西”。所以,我们应创Z个常敎ͼqؓ其用具有说服力的描q性名Uͼq在整个E序中都采用常数标识W。这样可使程序更易理解以及更易维护?
(13) 涉及构徏器和异常的时候,通常希望重新丢弃在构建器中捕LM异常——如果它造成了那个对象的创徏p|。这样一来,调用者就不会以ؓ那个对象已正地创徏Q从而盲目地l箋?
(14) 当客L序员用完对象以后Q若你的c要求进行Q何清除工作,可考虑清除代码置于一个良好定义的Ҏ里,采用cM于cleanup()q样的名字,明确表明自己的用途。除此以外,可在cd攄一个booleanQ布)标记Q指出对象是否已被清除。在cȝfinalize()Ҏ里,L定对象已被清除,q已丢弃了从RuntimeExceptionl承的一个类Q如果还没有的话Q,从而指Z个编E错误。在采取象这LҎ之前Q请定finalize()能够在自qpȝ中工作(可能需要调用System.runFinalizersOnExit(true)Q从而确保这一行ؓQ?
(15) 在一个特定的作用域内Q若一个对象必L除(非由垃圾攉机制处理Q,请采用下q方法:初始化对象;若成功,则立卌入一个含有finally从句的try块,开始清除工作?
(16) 若在初始化过E中需要覆盖(取消Qfinalize()Q请C调用super.finalize()Q若Object属于我们的直接超c,则无此必要)。在对finalize()q行覆盖的过E中Q对super.finalize()的调用应属于最后一个行动,而不应是W一个行动,q样可确保在需要基cȝ件的时候它们依然有效?
(17) 创徏大小固定的对象集合时Q请它们传输至一个数l(若准备从一个方法里q回q个集合Q更应如此操作)。这样一来,我们可享受到数l在~译期进行类型检查的好处。此外,Z用它们,数组的接收者也许ƈ不需要将对象“造型”到数组里?
(18) 量使用interfacesQ不要用abstractcR若已知某样东西准备成ؓ一个基c,那么W一个选择应是其变成一个interfaceQ接口)。只有在不得不用方法定义或者成员变量的时候,才需要将其变成一个abstractQ抽象)cR接口主要描qC客户希望做什么事情,而一个类则致力于Q或允许Q具体的实施l节?
(19) 在构建器内部Q只q行那些对象设为正状态所需的工作。尽可能地避免调用其他方法,因ؓ那些Ҏ可能被其他h覆盖或取消,从而在构徏q程中生不可预知的l果Q参见第7章的详细说明Q?
(20) 对象不应只是单地容纳一些数据;它们的行Z应得到良好的定义?
(21) 在现成类的基上创建新cLQ请首先选择“新建”或“创作”。只有自q设计要求必须l承Ӟ才应考虑q方面的问题。若在本来允许新建的场合使用了承,则整个设计会变得没有必要地复杂?
(22) 用承及Ҏ覆盖来表C为间的差异,而用字段表示状态间的区别。一个非常极端的例子是通过对不同类的承来表示颜色Q这是绝对应该避免的Q应直接使用一个“颜艜y字Dc?
(23) 为避免编E时遇到ȝQ请保证在自q路径指到的Q何地方,每个名字都仅对应一个类。否则,~译器可能先扑ֈ同名的另一个类Qƈ报告出错消息。若怀疑自qCc\径问题,误试在c\径的每一个v点,搜烦一下同名的.class文g?
(24) 在Java 1.1 AWT中用事件“适配器”时Q特别容易碰C个陷阱。若覆盖了某个适配器方法,同时拼写Ҏ没有特别讲究Q最后的l果是新添加一个方法,而不是覆盖现成方法。然而,׃q样做是完全合法的,所以不会从~译器或q行期系l获得Q何出错提C——只不过代码的工作就变得不正怺?
(25) 用合理的设计Ҏ消除“伪功能”。也是_假若只需要创建类的一个对象,׃要提前限制自׃用应用程序,q加上一条“只生成其中一个”注释。请考虑其装成一个“独生子”的形式。若在主E序里有大量散ؕ的代码,用于创徏自己的对象,误虑采纳一U创造性的ҎQ将些代码封装v来?
(26) 警惕“分析瘫痪”。请CQ无论如何都要提前了解整个项目的状况Q再去考察其中的细节。由于把握了全局Q可快速认识自己未知的一些因素,防止在考察l节的时候陷入“死逻辑”中?
(27) 警惕“过早优化”。首先让它运行v来,再考虑变得更快——但只有在自己必这样做、而且l证实在某部分代码中的确存在一个性能瓉的时候,才应q行优化。除非用专门的工具分析瓶颈,否则很有可能是在费自己的时间。性能提升的隐含代h自己的代码变得难于理解,而且难于l护?
(28) 误住,阅读代码的时间比写代码的旉多得多。思\清晰的设计可获得易于理解的程序,但注释、细致的解释以及一些示例往往h不可估量的h倹{无论对你自己,q是对后来的人,它们都是相当重要的。如Ҏ仍有怀疑,那么误惌p图从联机Java文档里找出有用信息时到的挫折,q样或许能将你说服?
(29) 如认己已q行了良好的分析、设计或者实施,那么L微更换一下思维角度。试试邀请一些外来h士——ƈ不一定是专家Q但可以是来自本公司其他部门的h。请他们用完全新鲜的眼光考察你的工作Q看看是否能扑և你一度熟视无睹的问题。采取这U方式,往往能在最适合修改的阶D|Z些关键性的问题Q避免品发行后再解决问题而造成的金钱及_֊斚w的损失?
(30) 良好的设计能带来最大的回报。简a之,对于一个特定的问题Q通常会花较长的时间才能找CU最恰当的解x案。但一旦找C正确的方法,以后的工作就L多了Q再也不用经历数时、数天或者数月的痛苦挣扎。我们的努力工作会带来最大的回报Q甚x可估量)。而且׃自己倾注了大量心血Q最l获得一个出色的设计ҎQ成功的快感也是令h心动的。坚持抵制草草完工的诱惑——那样做往往得不偿失?
(31) 可在Web上找到大量的~程参考资源,甚至包括大量新闻l、讨论组、邮寄列表等。 ?
import java.util.*;
import java.sql.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class MyAccess extends JFrame implements ActionListener,ListSelectionListener
{
private static Connection conn;
private static Statement comm;
private static ResultSet rs;
private static int total=0,k=10,row=0,i=0;
JLabel jl,jl2;
JTable jt;
JTextField jid,jname,jscore,se;
static JButton go,add,drop,del,jbse,flush;
JFrame jf;
JScrollPane s;
Vector vect=new Vector();
String[] data = {"学号=","学号>","学号<", "姓名=", "分数=","分数>=","分数<"};
JComboBox dataList = new JComboBox(data);
String[] columnNames = {"学号","姓名","分数"};
AbstractTableModel tm = new AbstractTableModel()
{
public int getColumnCount(){ return 3;}
public int getRowCount(){ return k;}
public Object getValueAt(int row, int col){ return ((Vector)vect.get(row)).get(col); }
public String getColumnName(int column){ return columnNames[column]; }
};
public MyAccess()
{
jf=new JFrame("考感学院04UJAVA成W理pȝ");
jf.setSize(490,570);
jf.locate(200,200);
jf.setResizable(false);
jf.getContentPane().setLayout(new FlowLayout());
jl=new JLabel("考感学院04UJAVA成W");
jid=new JTextField(6);
jname=new JTextField(5);
jscore=new JTextField(3);
se=new JTextField(9);
go=new JButton("分数(?>?");
go.addActionListener(this);
add=new JButton("d");
add.addActionListener(this);
drop=new JButton("修改");
drop.addActionListener(this);
del=new JButton("删除");
del.addActionListener(this);
jbse=new JButton("搜烦");
jbse.addActionListener(this);
flush=new JButton("h");
flush.addActionListener(this);
jl2=new JLabel("");
jl2.setForeground(Color.red);
jf.getContentPane().add(jl);
jt=new JTable(tm);
jt.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
this.jt.getSelectionModel().addListSelectionListener(this);
s = new JScrollPane(jt);
jf.getContentPane().add(s);
jf.getContentPane().add(new JLabel("学号:"));
jf.getContentPane().add(jid);
jf.getContentPane().add(new JLabel("姓名:"));
jf.getContentPane().add(jname);
jf.getContentPane().add(new JLabel("分数:"));
jf.getContentPane().add(jscore);
jf.getContentPane().add(add);
jf.getContentPane().add(drop);
jf.getContentPane().add(del);
jf.getContentPane().add(new JLabel("搜烦学生:"));
jf.getContentPane().add(dataList);
jf.getContentPane().add(se);
jf.getContentPane().add(jbse);
jf.getContentPane().add(flush);
jf.getContentPane().add(go);
jf.getContentPane().add(jl2);
}
public void exce(String exce)
{
try
{ this.jt.getSelectionModel().removeListSelectionListener(this);
rs = execQuery(exce);
vect.removeAllElements();
tm.fireTableDataChanged();
total=0;
while(rs.next())
{
Vector rec_vector=new Vector();
rec_vector.addElement(rs.getString(1));
rec_vector.addElement(rs.getString(2));
rec_vector.addElement(rs.getString(3));
vect.addElement(rec_vector);
total++;
}
k=total;
this.closeDB();
System.out.println("vect是面?+total+"记录执行操作");
System.out.println("成功执行:"+exce);
this.jt.getSelectionModel().addListSelectionListener(this);
}
catch(Exception ee)
{
jl2.setText("无法执行,请填入正的数据");
System.out.println("执行p|,可能查询为空");
k=0;
}
}
public void excesql(String sql)
{
try
{
conBuild();
Statement stm=conn.createStatement();
stm.executeUpdate(sql);
jf.repaint();
jl2.setText("操作已执?);
System.out.println("成功执行:"+sql);
this.closeDB();
}
catch(Exception ee)
{
jl2.setText("无法执行,请填入正的数据");
System.out.println(sql+"无法执行");
}
}
public static void regDriver()//register JDBC 桥接 driver
{
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();//关键?
System.out.println("驱动注册成功");
}
catch(Exception e)
{
System.out.println("无法创徏驱动E序实体!");
}
}
//建立数据库连?br />public static void conBuild()//建立JDBCq接
{
try{
MyAccess.regDriver();
conn=DriverManager.getConnection("jdbc:odbc:myDSN","","");//关键?
conn.setAutoCommit(true);
System.out.println("成功q接数据?);
}
catch(Exception e)
{
System.out.println(e.getMessage()) ;
System.out.println("无法q接数据库Connection!Q运行之前请先设|数据源 MyDSN");
}
}
public static ResultSet execQuery(String stmt1)//执行查询语句
{
try{
conBuild();
comm=conn.createStatement();
rs=comm.executeQuery(stmt1);
return rs;
}
catch(Exception e)
{
System.out.println("无法创徏Statement!");return null;
}
}
public static void closeDB()
{
try{
comm.close();
conn.close();
System.out.println("关闭记录集,断开数据?);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public void actionPerformed(ActionEvent ee)// 实现ActionListener中的唯一Ҏ
{
if(ee.getSource()==go)
{
try
{
if(i==0)
{
exce("SELECT * FROM 成W order by 分数");
i=1;
go.setText("分数(?>?");
}
else
{
exce("SELECT * FROM 成W order by 分数 desc");
i=0;
go.setText("分数(?>?");
}
jl2.setText("已按分数排序");
}
catch(Exception go)
{
System.out.println("出现错误");
jl2.setText("提示:出现错误");
}
}
if(ee.getSource()==add)
{
this.excesql("insert into 成W values('"+jid.getText()+"','"+jname.getText()+"','"+jscore.getText()+"')");
exce("SELECT * FROM 成W order by 学号");
jid.setText("");
jname.setText("");
jscore.setText("");
jf.repaint();
System.out.println("执行d操作");
}
if(ee.getSource()==drop)
{
this.excesql("update 成W set 姓名='"+jname.getText()+"' ,分数="+jscore.getText()+" where 学号='"+jid.getText()+"'" );
exce("SELECT * FROM 成W order by 学号");
}
if(ee.getSource()==del)
{
this.excesql("delete from 成W where 学号='"+jid.getText()+"'");
exce("SELECT * FROM 成W order by 学号");
jl2.setText("提示:成功删除ID?+jid.getText()+"的数?);
System.out.println("执行删除操作");
}
if(ee.getSource()==jbse)
{
if(dataList.getSelectedIndex()>=4)
{
String sql="SELECT * FROM 成W WHERE "+dataList.getSelectedItem()+se.getText();
exce(sql);
}
else
{
String sql="SELECT * FROM 成W WHERE "+dataList.getSelectedItem()+"'"+se.getText()+"'";
exce(sql);
}
jl2.setText("搜烦l果:?+k+"条数?);
System.out.println(k+"执行搜烦操作");
jf.repaint();
}
if(ee.getSource()==flush)
{
exce("SELECT * FROM 成W order by 学号");
jid.setText("");
jname.setText("");
jscore.setText("");
se.setText("");
jf.repaint();
jl2.setText("提示:h成功");
}
}
public void valueChanged(ListSelectionEvent el) //每当选择值发生更Ҏ调用?
{
row=0;
row=jt.getSelectedRow();
if(row<0) row=0;
Object row_id=jt.getValueAt(row,0);
Object row_name=jt.getValueAt(row,1);
Object row_score=jt.getValueAt(row,2);
jid.setText(row_id.toString());
jname.setText(row_name.toString());
jscore.setText(row_score.toString());
System.out.println("选择已改?+k+" "+total);
}
public static void main(String s[])
{
MyAccess ma=new MyAccess();
ma.exce("SELECT * FROM 成W order by 学号");
ma.jf.setVisible(true);
}
}
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class yc extends JFrame
{
public static JLabel jl1,jl2;
public static TextArea ta1,ta2;
public static JButton jb1,jb2;
public static Container cp1;
static Socket svr;
public static PrintWriter out;
public static void main(String arg[])
{
JFrame jf=new JFrame("与小新聊?-VIP 客户?);
jf.setSize(500,450);
jf.setResizable(false);
jf.locate(100,100);
// cp1=getContentPane();
jl1=new JLabel("q接?...");
ta1=new TextArea();
ta2=new TextArea();
jb1=new JButton("发?);
jb1.addActionListener(new Listener());
jb2=new JButton("清空");
jb2.addActionListener(new Listener());
jf.getContentPane().setLayout(new FlowLayout());
jf.getContentPane().add(jl1);
jf.getContentPane().add(ta1);
jf.getContentPane().add(ta2);
jf.getContentPane().add(jb1);
jf.getContentPane().add(jb2);
jf.setVisible(true);
try
{
target2 outmsg2=new target2();
Thread outthread2=new Thread(outmsg2);
outthread2.start();
QQ();
}
catch(Exception e)
{
}
}
static void QQ()throws Exception
{
System.out.println("正在q接服务?L?..");
//与指定地址的服务器相连?br /> svr=new Socket("127.0.0.1",3300);//要连接远E主填IP
//svr=new Socket(InetAddress.getLocalHost(),3300)
jl1.setText("?+svr.getInetAddress()+"q接成功!误传送的信息...");
}
}
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==yc.jb2)
{
yc.ta1.setText("");
}
else
{
try
{
yc.out=new PrintWriter(yc.svr.getOutputStream());
yc.out.println(yc.ta2.getText());
yc.out.flush();
yc.ta1.append("[自己]?\n"+yc.ta2.getText()+"\n");
yc.ta2.setText("");
}
catch(Exception ee)
{
}
}
}
}
class target2 implements Runnable
{
public void run()
{
while(true)
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(yc.svr.getInputStream()));
String str=in.readLine();
str="[服务?+yc.svr.getInetAddress()+"]?\n"+str;
yc.ta1.append(str+"\n");
}
catch(Exception ee)
{
;
}
}
}
}
———————————————————————————————?/p>
//QQ服务?/p>
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class ycserver extends JFrame
{
public static JLabel jl1;
public static TextArea ta1,ta2;
public static JButton jb1,jb2;
public static Container cp1;
public static ServerSocket svr;
public static Socket clt;
public static Thread thread;
public static int i=0;
public static void main(String arg[])
{
JFrame jf=new JFrame("与杨彩聊?-VIP 服务?);
jf.setSize(500,450);
jf.locate(200,200);
jf.setResizable(false);
ta1=new TextArea();
ta2=new TextArea();
jb1=new JButton("发?);
jb2=new JButton("清空");
jl1=new JLabel("{待q接");
jb1.addActionListener(new jb1Listener());
jb2.addActionListener(new jb1Listener());
jf.getContentPane().setLayout(new FlowLayout());
jf.getContentPane().add(jl1);
jf.getContentPane().add(ta1);
jf.getContentPane().add(ta2);
jf.getContentPane().add(jb1);
jf.getContentPane().add(jb2);
jf.setVisible(true);
try
{
target outmsg=new target();
Thread outthread=new Thread(outmsg);
outthread.start();
wait wait1=new wait();
Thread waitthread=new Thread(wait1);
waitthread.start();
QQ();
}
catch(Exception e)
{
}
}
static void QQ()throws Exception
{/*
//建立服务器套节字
svr=new ServerSocket(3300);
System.out.println("{待q接....");
//{待客户机接?br /> clt=svr.accept();
i++;
ta1.setText(i+"");
//获得客户IP地址
System.out.println("q接h来自:"+clt.getInetAddress());
jl1.setText("q接h来自:"+clt.getInetAddress());
//建立I/O?br /> */
}
}
class jb1Listener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ycserver.jb2)
{
ycserver.ta1.setText("");
}
else
{
try
{
PrintWriter out=new PrintWriter(ycserver.clt.getOutputStream());
out.println(ycserver.ta2.getText());
out.flush();
ycserver.ta1.append("[自己]?\n"+ycserver.ta2.getText()+"\n");
ycserver.ta2.setText("");
}
catch(Exception ee)
{
}
}
}
}
class target implements Runnable
{
public void run()
{
while(true)
{
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(ycserver.clt.getInputStream()));
String str=in.readLine();
str="[客户端在"+ycserver.clt.getInetAddress()+"]?\n"+str;
ycserver.ta1.append(str+"\n");
}
catch(Exception ee)
{
;
}
}
}
}
class wait implements Runnable
{
public void run()
{
while(true)
{
try
{
//建立服务器套节字
ycserver.svr=new ServerSocket(3300);
System.out.println("{待q接....");
//{待客户机接?br /> ycserver.clt=ycserver.svr.accept();
ycserver.i+=2;
ycserver.ta1.setText(ycserver.i+"");
//获得客户IP地址
System.out.println("q接h来自:"+ycserver.clt.getInetAddress());
ycserver.jl1.setText("q接h来自:"+ycserver.clt.getInetAddress());
//建立I/O?br /> }
catch(Exception ee)
{
;
}
}
}
}
public class calculator extends JFrame implements ActionListener
{//q里我把JFrame写成FrameQ这个错误找了好?br /> JFrame frame;
private JButton jia=new JButton("+");
private JButton jian=new JButton("-");
private JButton cheng=new JButton("*");
private JButton chu=new JButton("/");
private JButton qiuyi=new JButton("%");
private JButton deng=new JButton("=");
private JButton fu=new JButton("+/-");
private JButton dian=new JButton(".");
private JButton kai=new JButton("sqrt");
private JButton diao=new JButton("1/x");
private JButton aa=new JButton("A");
private JButton bb=new JButton("B");
private JButton cc=new JButton("C");
private JButton dd=new JButton("D");
private JButton ee=new JButton("E");
private JButton ff=new JButton("F");
private TextField k1=new TextField();
private objConversion convert = new objConversion();
JMenuItem copy,paste,s,t,help,about,me;
JRadioButton sixteen,ten,eight,two;
JButton backspace,ce,c,num0,num1,num2,num3,num4,num5,num6,num7,num8,num9;
Container cp;
JTextField text;
String copycontent="";
boolean clickable=true,clear=true;
int all=0;
double qian;
String fuhao;
int jin=10,first=1;
public calculator()
{
setTitle("计算器-杨彩制作");
setSize(400,300);
setLocation(250,200);
text=new JTextField(25);
// text.setEnabled(false);
text.setText("0.");
text.setHorizontalAlignment(JTextField.RIGHT);//从右到左
JPanel cp1=new JPanel();
JPanel cp2=new JPanel();
JPanel cp3=new JPanel();
cp=getContentPane();
cp.add(cp1,"North");
cp.add(cp2,"Center");
cp.add(cp3,"South");
cp1.setLayout(new GridLayout(1,6));
cp2.setLayout(new GridLayout(2,4));
cp3.setLayout(new GridLayout(6,6));
sixteen=new JRadioButton("十六q制");
sixteen.setVisible(false);
ten=new JRadioButton("十进?,true);
ten.setVisible(false);
eight=new JRadioButton("八进?);
eight.setVisible(false);
two=new JRadioButton("二进?);
two.setVisible(false);
sixteen.addActionListener(this);
ten.addActionListener(this);
eight.addActionListener(this);
two.addActionListener(this);
ButtonGroup btg=new ButtonGroup();
btg.add(sixteen);
btg.add(ten);
btg.add(eight);
btg.add(two);
JTextField t3=new JTextField(25);
cp1.add(text);
// text.setEnabled(false);
text.setEditable(false);
text.setBackground(new Color(255, 255, 255));
cp2.add(sixteen);
cp2.add(ten);
cp2.add(eight);
cp2.add(two);
backspace=new JButton("Backspace");
backspace.setForeground(new Color(255,0,0));
backspace.addActionListener(this);
ce=new JButton("CE");
ce.setForeground(new Color(255,0,0));
ce.addActionListener(this);
c=new JButton("C");
c.setForeground(new Color(255,0,0));
c.addActionListener(this);
k1.setVisible(false);
cp2.add(k1);
cp2.add(backspace);
cp2.add(ce);
cp2.add(c);
num0=new JButton("0");
num1=new JButton("1");
num2=new JButton("2");
num3=new JButton("3");
num4=new JButton("4");
num5=new JButton("5");
num6=new JButton("6");
num7=new JButton("7");
num8=new JButton("8");
num9=new JButton("9");
cp3.add(num7);
num7.addActionListener(this);
cp3.add(num8);
num8.addActionListener(this);
cp3.add(num9);
num9.addActionListener(this);
cp3.add(chu);
chu.setForeground(new Color(255,0,0));
chu.addActionListener(this);
cp3.add(kai);
kai.addActionListener(this);
cp3.add(num4);
num4.addActionListener(this);
cp3.add(num5);
num5.addActionListener(this);
cp3.add(num6);
num6.addActionListener(this);
cp3.add(cheng);
cheng.setForeground(new Color(255,0,0));
cheng.addActionListener(this);
cp3.add(qiuyi);
qiuyi.addActionListener(this);
cp3.add(num1);
num1.addActionListener(this);
cp3.add(num2);
num2.addActionListener(this);
cp3.add(num3);
num3.addActionListener(this);
cp3.add(jian);
jian.setForeground(new Color(255,0,0));
jian.addActionListener(this);
cp3.add(diao);
diao.addActionListener(this);
cp3.add(num0);
num0.addActionListener(this);
cp3.add(fu);
fu.addActionListener(this);
cp3.add(dian);
dian.addActionListener(this);
cp3.add(jia);
jia.setForeground(new Color(255,0,0));
jia.addActionListener(this);
cp3.add(deng);
deng.setForeground(new Color(255,0,0));
deng.addActionListener(this);
cp3.add(aa);
aa.addActionListener(this);
cp3.add(bb);
bb.addActionListener(this);
cp3.add(cc);
cc.addActionListener(this);
cp3.add(dd);
dd.addActionListener(this);
cp3.add(ee);
ee.addActionListener(this);
cp3.add(ff);
ff.addActionListener(this);
aa.setVisible(false);
bb.setVisible(false);
cc.setVisible(false);
dd.setVisible(false);
ee.setVisible(false);
ff.setVisible(false);
JMenuBar mainMenu = new JMenuBar();
setJMenuBar(mainMenu);
JMenu editMenu = new JMenu("~辑");
JMenu viewMenu = new JMenu("查看");
JMenu helpMenu = new JMenu("帮助");
mainMenu.add(editMenu);
mainMenu.add(viewMenu);
mainMenu.add(helpMenu);
copy = new JMenuItem(" 复制");
paste = new JMenuItem(" _脓");
KeyStroke copyks=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK);
copy.setAccelerator(copyks);//讄退单选项加上快捷?br /> KeyStroke pasteks=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK);
paste.setAccelerator(pasteks);//讄退单选项加上快捷?br /> editMenu.add(copy);
editMenu.add(paste);
copy.addActionListener(this);
paste.addActionListener(this);
t = new JMenuItem("●标准型");
s = new JMenuItem(" U学?);
viewMenu.add(t);
viewMenu.add(s);
t.addActionListener(this);
s.addActionListener(this);
help = new JMenuItem(" 帮助主题");
about = new JMenuItem(" 关于计算?);
me = new JMenuItem(" 作者主?);
helpMenu.add(help);
helpMenu.add(about);
helpMenu.add(me);
help.addActionListener(this);
about.addActionListener(this);
me.addActionListener(this);
addWindowListener(new WindowDestroyer());//l束H口
}
public void actionPerformed(ActionEvent e)
{//响应动作代码
if(first==1)
text.setText("");
first=0;//W一ơ把文本?.清空
Object temp = e.getSource();
if(temp==copy)
{
copycontent = text.getText();
}
if(temp==paste)
{
text.setText(text.getText()+copycontent);
}
if(temp==t)
{//标准
sixteen.setVisible(false);
ten.setVisible(false);
eight.setVisible(false);
two.setVisible(false);
t.setText("●标准型");
s.setText(" U学?);
aa.setVisible(false);
bb.setVisible(false);
cc.setVisible(false);
dd.setVisible(false);
ee.setVisible(false);
ff.setVisible(false);
}
if(temp==s)
{//U学
sixteen.setVisible(true);
ten.setVisible(true);
eight.setVisible(true);
two.setVisible(true);
t.setText(" 标准?);
s.setText("●科学型");
aa.setVisible(true);
bb.setVisible(true);
cc.setVisible(true);
dd.setVisible(true);
ee.setVisible(true);
ff.setVisible(true);
aa.setEnabled(false);
bb.setEnabled(false);
cc.setEnabled(false);
dd.setEnabled(false);
ee.setEnabled(false);
ff.setEnabled(false);
}
if(temp==help)
{ //打开pȝ帮助文g 要查资料
try
{
String filePath = "C:/WINDOWS/Help/calc.chm";
Runtime.getRuntime().exec("cmd.exe /c "+filePath);
}
catch(Exception eeee)
{
System.out.println("打开pȝ的计器出错");
}
}
if(temp==about)
{
JOptionPane.showMessageDialog(frame," Java计算器\n 杨彩 制作\n\n }
if(temp==me)
{
try
{
Process p = Runtime.getRuntime().exec("explorer }
catch(Exception eeee)
{
System.out.println("打开指定|页出错");
}
}
try
{
if(temp==sixteen)
{
String hex=text.getText();
int inthex=Integer.parseInt(hex,jin);//先把数变?0q制
text.setText(convert.decHex(inthex)) ;
jin=16;
aa.setEnabled(true);
bb.setEnabled(true);
cc.setEnabled(true);
dd.setEnabled(true);
ee.setEnabled(true);
ff.setEnabled(true);
num2.setEnabled(true);
num3.setEnabled(true);
num4.setEnabled(true);
num5.setEnabled(true);
num6.setEnabled(true);
num7.setEnabled(true);
num8.setEnabled(true);
num9.setEnabled(true);
}
if(temp==eight)
{
String oct =text.getText();
int intoct=Integer.parseInt(oct,jin);
text.setText(convert.decOct(intoct)) ;
jin=8;
aa.setEnabled(false);
bb.setEnabled(false);
cc.setEnabled(false);
dd.setEnabled(false);
ee.setEnabled(false);
ff.setEnabled(false);
num2.setEnabled(true);
num3.setEnabled(true);
num4.setEnabled(true);
num5.setEnabled(true);
num6.setEnabled(true);
num7.setEnabled(true);
num8.setEnabled(false);
num9.setEnabled(false);
}
if(temp==two)
{
String bin=text.getText();
int intbin=Integer.parseInt(bin,jin);
text.setText(convert.decBin(intbin));
jin=2;
aa.setEnabled(false);
bb.setEnabled(false);
cc.setEnabled(false);
dd.setEnabled(false);
ee.setEnabled(false);
ff.setEnabled(false);
num2.setEnabled(false);
num3.setEnabled(false);
num4.setEnabled(false);
num5.setEnabled(false);
num6.setEnabled(false);
num7.setEnabled(false);
num8.setEnabled(false);
num9.setEnabled(false);
}
if(temp==ten)
{
String dec=text.getText();
int intdec=Integer.parseInt(dec,jin);
// text.setText(convert.decDec(intdec)); //本句会把123变成321
text.setText(intdec+"");
jin=10;
aa.setEnabled(false);
bb.setEnabled(false);
cc.setEnabled(false);
dd.setEnabled(false);
ee.setEnabled(false);
ff.setEnabled(false);
num2.setEnabled(true);
num3.setEnabled(true);
num4.setEnabled(true);
num5.setEnabled(true);
num6.setEnabled(true);
num7.setEnabled(true);
num8.setEnabled(true);
num9.setEnabled(true);
}
}
catch(Exception ee)
{
System.out.println("转换出错,可能你没有输入Q何字W?);
text.setText("转换出错");
clear=false;
}
if(temp==backspace)
{//退?br /> String s = text.getText();
text.setText("");
for (int i = 0; i < s.length() - 1; i++)
{
char a = s.charAt(i);
text.setText(text.getText() + a);
}
}
if (temp==ce)
{
text.setText("0.");
clear=true;
first=1;
}
if (temp==c)
{
text.setText("0."); ;
clear=true;
first=1;
}
if(temp==num0)
{
if(clear==false)//判断是否点击了符号位
text.setText("");
text.setText(text.getText()+"0");
}
if(temp==num1)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"1");
clear=true;//W二ơ不在清I(前二句)
}
if(temp==num2)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"2");
clear=true;
}
if(temp==num3)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"3");
clear=true;
}
if(temp==num4)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"4");
clear=true;
}
if(temp==num5)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"5");
clear=true;
}
if(temp==num6)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"6");
clear=true;
}
if(temp==num7)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"7");
clear=true;
}
if(temp==num8)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"8");
clear=true;
}
if(temp==num9)
{
if(clear==false)
text.setText("");
text.setText(text.getText()+"9");
clear=true;
}
if(temp==aa)
{
text.setText(text.getText()+"A");
}
if(temp==bb)
{
text.setText(text.getText()+"B");
}
if(temp==cc)
{
text.setText(text.getText()+"C");
}
if(temp==dd)
{
text.setText(text.getText()+"D");
}
if(temp==ee)
{
text.setText(text.getText()+"E");
}
if(temp==ff)
{
text.setText(text.getText()+"F");
}
if(temp==dian)
{
clickable=true;
for (int i = 0; i < text.getText().length(); i++)
if ('.' == text.getText().charAt(i))
{
clickable=false;
break;
} //W一层判断是否里面含有小数点;
if(clickable==true)//W二坛判?br /> text.setText(text.getText()+".");
}
try
{
if(temp==jia)
{//加法
qian=Double.parseDouble(text.getText());
fuhao="+";
clear=false;
}
if(temp==jian)
{
qian=Double.parseDouble(text.getText());
fuhao="-";
clear=false;
;
}
if(temp==cheng)
{
qian=Double.parseDouble(text.getText());
fuhao="*";
clear=false;
}
if(temp==chu)
{
qian=Double.parseDouble(text.getText());
fuhao="/";
clear=false;
}
if(temp==deng)
{
double ss=Double.parseDouble(text.getText());
text.setText("");
if(fuhao=="+")
text.setText(qian+ss+"");
if(fuhao=="-")
text.setText(qian-ss+"");
if(fuhao=="*")
text.setText(qian*ss+"");
if(fuhao=="/")
text.setText(qian/ss+"");
clear=false;//要清I前一ơ的数据
;
}
if(temp==kai)
{
String s = text.getText();
if (s.charAt(0) == '-')
{
text.setText("负数不能开根号");
}
else
text.setText(Double.toString(java.lang.Math.sqrt(Double.parseDouble(text.getText()))));
clear=false;
}
if(temp==diao)
{
if (text.getText().charAt(0) == '0'&&text.getText().length() == 1)
{
text.setText("除数不能为零");
}
else
{
boolean isDec = true;
int i, j, k;
String s = Double.toString(1 / Double.parseDouble(text.getText()));
for (i = 0; i < s.length(); i++)
if (s.charAt(i) == '.')
break;
for (j = i + 1; j < s.length(); j++)
if (s.charAt(j) != '0')
{
isDec = false;
break;
}
if (isDec == true)
{
String stemp = "";
for (k = 0; k < i; k++)
stemp += s.charAt(k);
text.setText(stemp);
}
else
text.setText(s);
}
clear=false;
}
if(temp==qiuyi)
{
text.setText("0");
clear=false;
}
if (temp == fu)
{ //导师Q此Ҏ参考书中例?br /> boolean isNumber = true;
String s = text.getText();
for (int i = 0; i < s.length(); i++)
if (! (s.charAt(i) >= '0' && s.charAt(i) <= '9' || s.charAt(i) == '.' ||
s.charAt(i) == '-'))
{
isNumber = false;
break;
}
if (isNumber == true)
{
//如果当前字符串首字母?-'?代表现在是个负数,再按下时,则将首符号去?
if (s.charAt(0) == '-')
{
text.setText("");
for (int i = 1; i < s.length(); i++)
{
char a = s.charAt(i);
text.setText(text.getText() + a);
}
}
//如果当前字符串第一个字W不是符P则添加一个符号在首字母处
else
text.setText('-' + s);
}
}
}
catch(Exception eee)
{
System.out.println("q算?首先输入数字或字W?);
text.setText("q算出错");
clear=false;
}
}
class WindowDestroyer extends WindowAdapter
{//退出窗口动?br /> public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class objConversion
{//导师,本进制类参考了CSMDc{换例?br />
public void objConversion ()
{
}
public String decDec (int decNum)
{//10
String strDecNum = Integer.toString(decNum);
for (int i = strDecNum.length(); i < 3; i++)
{
strDecNum = "0" + strDecNum;
}
// return strDecNum;
return invert (strDecNum, 5);
}
public String decHex (int decNum)
{//10 to 16
String strHexNum = "";
int currentNum = 0;
while (decNum != 0)
{
if (decNum > 15)
{
currentNum = decNum % 16;
decNum /= 16;
}
else
{
currentNum = decNum;
decNum = 0;
}
switch (currentNum)
{
case 15: strHexNum += "F";
break;
case 14: strHexNum += "E";
break;
case 13: strHexNum += "D";
break;
case 12: strHexNum += "C";
break;
case 11: strHexNum += "B";
break;
case 10: strHexNum += "A";
break;
default: strHexNum += Integer.toString(currentNum);
break;
}
}
return invert (strHexNum, 2);
}
public String decOct (int decNum)
{//10 to 8
String strOctNum = "";
while (decNum != 0)
{
if (decNum > 7)
{
strOctNum += Integer.toString(decNum % 8);
decNum /= 8;
}
else
{
strOctNum += Integer.toString(decNum);
decNum = 0;
}
}
return invert (strOctNum, 3);
}
public String decBin (int decNum)
{//10 to 2
String strBinNum = "";
while (decNum != 0)
{
if (decNum > 1)
{
strBinNum += Integer.toString(decNum % 2);
decNum /= 2;
}
else
{
strBinNum += Integer.toString(decNum);
decNum = 0;
}
}
return invert (strBinNum, 8);
}
private String invert (String strNum, int minLength) //转换长度
{
String answer = "";
int length = strNum.length();
if (length < minLength)
{
for (int padding = (minLength - length); padding > 0; padding--)
{
answer += "0";
}
}
for (int i = length; i > 0; i--)
{
answer += strNum.charAt (i - 1);
}
return answer;
}
}
public static void main(String arg[])//产生H口
{
calculator win = new calculator();
win.setVisible(true);
}
}