//获得屏幕的尺寸
(1) Toolkit tk=Toolkit.getDefaultToolkit();
(2) Dimession screenSize=tk.getScreenSize();
int height=screenSize.height;
.......我查了jdk,其中的Toolkit是abstract的,但是却有了对象,为什么?
下面的是getScreenSize()方法:
public abstract Dimension getScreenSize()
throws HeadlessException;
抽象的方法怎么实现的?,为什么可以返回屏幕的尺寸的啊?下面的是JDK中的Toolkit类的getDefaultToolkit()方法
刚学java,里面很多东西真的看不懂~~ public static synchronized Toolkit getDefaultToolkit() {
if (toolkit == null) {
try {
// We disable the JIT during toolkit initialization. This
// tends to touch lots of classes that aren't needed again
// later and therefore JITing is counter-productiive.
java.lang.Compiler.disable();
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
String nm = null;
Class cls = null;
try {
String defaultToolkit; if (System.getProperty("os.name").equals("Linux")) {
defaultToolkit = "sun.awt.X11.XToolkit";
}
else {
defaultToolkit = "sun.awt.motif.MToolkit";
}
nm = System.getProperty("awt.toolkit",
defaultToolkit);
try {
cls = Class.forName(nm);
} catch (ClassNotFoundException e) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
if (cl != null) {
try {
cls = cl.loadClass(nm);
} catch (ClassNotFoundException ee) {
throw new AWTError("Toolkit not found: " + nm);
}
}
}
if (cls != null) {
toolkit = (Toolkit)cls.newInstance();
if (GraphicsEnvironment.isHeadless()) {
toolkit = new HeadlessToolkit(toolkit);
}
}
} catch (InstantiationException e) {
throw new AWTError("Could not instantiate Toolkit: " +
nm);
} catch (IllegalAccessException e) {
throw new AWTError("Could not access Toolkit: " + nm);
}
return null;
}
});
loadAssistiveTechnologies();
} finally {
// Make sure to always re-enable the JIT.
java.lang.Compiler.enable();
}
}
return toolkit;
}
(1) Toolkit tk=Toolkit.getDefaultToolkit();
(2) Dimession screenSize=tk.getScreenSize();
int height=screenSize.height;
.......我查了jdk,其中的Toolkit是abstract的,但是却有了对象,为什么?
下面的是getScreenSize()方法:
public abstract Dimension getScreenSize()
throws HeadlessException;
抽象的方法怎么实现的?,为什么可以返回屏幕的尺寸的啊?下面的是JDK中的Toolkit类的getDefaultToolkit()方法
刚学java,里面很多东西真的看不懂~~ public static synchronized Toolkit getDefaultToolkit() {
if (toolkit == null) {
try {
// We disable the JIT during toolkit initialization. This
// tends to touch lots of classes that aren't needed again
// later and therefore JITing is counter-productiive.
java.lang.Compiler.disable();
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Object run() {
String nm = null;
Class cls = null;
try {
String defaultToolkit; if (System.getProperty("os.name").equals("Linux")) {
defaultToolkit = "sun.awt.X11.XToolkit";
}
else {
defaultToolkit = "sun.awt.motif.MToolkit";
}
nm = System.getProperty("awt.toolkit",
defaultToolkit);
try {
cls = Class.forName(nm);
} catch (ClassNotFoundException e) {
ClassLoader cl = ClassLoader.getSystemClassLoader();
if (cl != null) {
try {
cls = cl.loadClass(nm);
} catch (ClassNotFoundException ee) {
throw new AWTError("Toolkit not found: " + nm);
}
}
}
if (cls != null) {
toolkit = (Toolkit)cls.newInstance();
if (GraphicsEnvironment.isHeadless()) {
toolkit = new HeadlessToolkit(toolkit);
}
}
} catch (InstantiationException e) {
throw new AWTError("Could not instantiate Toolkit: " +
nm);
} catch (IllegalAccessException e) {
throw new AWTError("Could not access Toolkit: " + nm);
}
return null;
}
});
loadAssistiveTechnologies();
} finally {
// Make sure to always re-enable the JIT.
java.lang.Compiler.enable();
}
}
return toolkit;
}
private static Toolkit toolkit; //JDK1.5 中源代码第629行getDefaultToolkit方法结构:
public static synchronized Toolkit getDefaultToolkit() {
if (toolkit == null) { //需要建立一个toolkit对象
建立对象的代码
}
return toolkit;
}
可以保证程序中任何时候只能得到一个Toolkit的实例。
代码中下面几行创建toolkit对象啊
if (cls != null) {
toolkit = (Toolkit)cls.newInstance();
if (GraphicsEnvironment.isHeadless()) {
toolkit = new HeadlessToolkit(toolkit);
}其中,sun.awt.HeadlessToolkit 应该是一个非抽象类,
但是我找不能源码。高手继续