Skip to main content

Posts

Showing posts with the label Java

Replace JFrame Icon

By using setIconImage(image) can be used to replace the Java icon. However, you cannot prevent Java icon displaying by using setIconImage(null) since null will be ignored inside the setIconImage function.          // replace the default icon         URL url = getClass().getResource("/tmp/icon.jpg");         ImageIcon imageIcon = new ImageIcon(url);         Image image = imageIcon.getImage();         this.setIconImage(image); If null is returned by getResource(), please make sure that the icon.jpg can be found in the bin folder which stores the compiled java classes. The source code of setIconImage(Image imag) is:   public static ImageIcon loadImageIcon ( String filename ) {     URL url = Config . class . getClassLoader (). getResource ( IMAGE_DIR + filename );     i...

Java: Syntax error, parameterized types are only available if source level is 5.0

HOW TO FIX IT: It is because eventhough you have Java 5 installed, the JVM running may be different. Try typing this in the console window: java -version. It will give you the Version of the Runtime Environment. Or, if you are using an IDE, then you need to enable Java 5 support. In Eclipse, You can add Java 5 in Window > Preferences > Java > Installed JRE’s. and/or, Set the compiler compliance level to 5.0 (Window > Preferences > Java > Compiler) In Netbeans, you can use (Tools -> Java Platform Manager) to check your default platform. If you use javac commond line, try: javac -1.5 xxx.java Reference: http://laksmono.wordpress.com/2007/05/27/syntax-error-parameterized-types-are-only-available-if-source-level-is-50/

ant error: unable to find a javac compiler

When tomcat starts in service mode, it will call tomcat52.exe to start tomcat. By default, tomcat5w.exe uses java runtime as vm, and the vm will use its own JAVA_HOME setting which is stored in system registry data. For example, when you use JDK 1.4.2_06, java_home can be found in HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\1.4.2_06. Its default value is "C:\Program Files\Java\j2re1.4.2_06" which is java run time directory. You can change this to point to JDK home directory, like "C:\j2sdk1.4.2_06".

Comparison == vs equals() in Java

In java, we have two operations (" == " and " equals() ") to check the equivalence of String s. It is important to understand that the difference between them.    == The == operator compares the two objects and checks to see if two objects are exactly the same object (refer to the same instance). .equals() The equals() method compares the characters in the two different objects. Two strings may be different objects , but can contains the same values (exactly the same characters). public class Main {     public static void main(String[] args) {         String s1 = "Shangshu";         String s2 = new String(s1);         System.out.println(s1 + " == " + s2 + " is " + (s1 == s2));         System.out.println(s1 + ".equals(" + s2 + ") is " + (s1.equals(s2)));     } } The output of the above code is:...

Java函数参数的传递机制

Java函数参数的传递是以 传递值 ,或者说 副本 来进行的。 看到上面这句话可能从C/C++转过来的程序员可能会有些疑惑,那岂不是没有引用传递这一说了。那有些函数我需要用到传递引用怎么办呢? 其实不然,Java函数也是可以传递引用的(是不是更迷惑了:))。 其实这要设计到Java变量和类型的存储引用机制。Java对原始变量存值,而对其它对象采用引用操作,类似C/C++的指针。如果我们称后者为 引用类型 的话。那么但 引用类型 传递给函数时,Java函数复制一个引用的副本 (这时这个引用的副本可以理解为 指向原对象指针的副本 )。这样对这个引用的操作就会实时的反映到原对象上面。也即完成了原来C/C++中传递指针或者引用的功能。 这样就不难理解为什么Java函数参数是传递值的了。 下面给出一个小小的例子: class Ob { int value = 0; public Ob(int i) { value = i; } } public class Test { public void increaseValue(int i){ i += 1; System.out.println("In function i = " + i); } public void increaseValue(Ob ob){ ob.value += 1; System.out.println("In function ob = " + ob.value); } public void increaseValue(Integer it){ it += 1; System.out.println("In function it = " + it); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Test test = new Test(); System.out.println("This is a Test"); int i = 0; Integer it = new Inte...