下面列出了怎么用sun.awt.OSInfo的API类实例代码及写法,或者点击链接到github查看源代码。
ActiveWindowsIcon(String desktopPropertyName,
String nativeImageName, String fallbackName) {
this.nativeImageName = nativeImageName;
this.fallbackName = fallbackName;
if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS &&
OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_XP) < 0) {
// This desktop property is needed to trigger reloading the icon.
// It is kept in member variable to avoid GC.
this.desktopProperty = new TriggerDesktopProperty(desktopPropertyName) {
@Override protected void updateUI() {
icon = null;
super.updateUI();
}
};
}
}
ActiveWindowsIcon(String desktopPropertyName,
String nativeImageName, String fallbackName) {
this.nativeImageName = nativeImageName;
this.fallbackName = fallbackName;
if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS &&
OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_XP) < 0) {
// This desktop property is needed to trigger reloading the icon.
// It is kept in member variable to avoid GC.
this.desktopProperty = new TriggerDesktopProperty(desktopPropertyName) {
@Override protected void updateUI() {
icon = null;
super.updateUI();
}
};
}
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
/**
* Obtains the appropriate <code>Popup</code> based on
* <code>popupType</code>.
*/
private Popup getPopup(Component owner, Component contents,
int ownerX, int ownerY, int popupType) {
if (GraphicsEnvironment.isHeadless()) {
return getHeadlessPopup(owner, contents, ownerX, ownerY);
}
switch(popupType) {
case LIGHT_WEIGHT_POPUP:
return getLightWeightPopup(owner, contents, ownerX, ownerY);
case MEDIUM_WEIGHT_POPUP:
return getMediumWeightPopup(owner, contents, ownerX, ownerY);
case HEAVY_WEIGHT_POPUP:
Popup popup = getHeavyWeightPopup(owner, contents, ownerX, ownerY);
if ((AccessController.doPrivileged(OSInfo.getOSTypeAction()) ==
OSInfo.OSType.MACOSX) && (owner != null) &&
(EmbeddedFrame.getAppletIfAncestorOf(owner) != null)) {
((HeavyWeightPopup)popup).setCacheEnabled(false);
}
return popup;
}
return null;
}
/**
* Obtains the appropriate <code>Popup</code> based on
* <code>popupType</code>.
*/
private Popup getPopup(Component owner, Component contents,
int ownerX, int ownerY, int popupType) {
if (GraphicsEnvironment.isHeadless()) {
return getHeadlessPopup(owner, contents, ownerX, ownerY);
}
switch(popupType) {
case LIGHT_WEIGHT_POPUP:
return getLightWeightPopup(owner, contents, ownerX, ownerY);
case MEDIUM_WEIGHT_POPUP:
return getMediumWeightPopup(owner, contents, ownerX, ownerY);
case HEAVY_WEIGHT_POPUP:
Popup popup = getHeavyWeightPopup(owner, contents, ownerX, ownerY);
if ((AccessController.doPrivileged(OSInfo.getOSTypeAction()) ==
OSInfo.OSType.MACOSX) && (owner != null) &&
(EmbeddedFrame.getAppletIfAncestorOf(owner) != null)) {
((HeavyWeightPopup)popup).setCacheEnabled(false);
}
return popup;
}
return null;
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test is suitable only for Windows, skipped.");
return;
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
File[] files = (File[]) ShellFolder.get("fileChooserComboBoxFolders");
for (File file : files) {
if (file instanceof ShellFolder && ((ShellFolder) file).isLink()) {
throw new RuntimeException("Link shouldn't be in FileChooser combobox, " + file.getPath());
}
}
}
});
}
ActiveWindowsIcon(String desktopPropertyName,
String nativeImageName, String fallbackName) {
this.nativeImageName = nativeImageName;
this.fallbackName = fallbackName;
if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS &&
OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_XP) < 0) {
// This desktop property is needed to trigger reloading the icon.
// It is kept in member variable to avoid GC.
this.desktopProperty = new TriggerDesktopProperty(desktopPropertyName) {
@Override protected void updateUI() {
icon = null;
super.updateUI();
}
};
}
}
public static void main(String[] args) throws Exception {
OSType type = OSInfo.getOSType();
if (type != OSType.WINDOWS) {
System.out.println("This test is for Windows only... skipping!");
return;
}
SwingUtilities.invokeAndWait(() -> {
try {
UIManager.setLookAndFeel(new WindowsLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
System.out.println("Creating JFileChooser...");
JFileChooser fileChooser = new JFileChooser();
System.out.println("Test passed: chooser = " + fileChooser);
});
// Test fails if creating JFileChooser hangs
}
/**
* Obtains the appropriate <code>Popup</code> based on
* <code>popupType</code>.
*/
@SuppressWarnings("deprecation")
private Popup getPopup(Component owner, Component contents,
int ownerX, int ownerY, int popupType) {
if (GraphicsEnvironment.isHeadless()) {
return getHeadlessPopup(owner, contents, ownerX, ownerY);
}
switch(popupType) {
case LIGHT_WEIGHT_POPUP:
return getLightWeightPopup(owner, contents, ownerX, ownerY);
case MEDIUM_WEIGHT_POPUP:
return getMediumWeightPopup(owner, contents, ownerX, ownerY);
case HEAVY_WEIGHT_POPUP:
Popup popup = getHeavyWeightPopup(owner, contents, ownerX, ownerY);
if ((AccessController.doPrivileged(OSInfo.getOSTypeAction()) ==
OSInfo.OSType.MACOSX) && (owner != null) &&
(EmbeddedFrame.getAppletIfAncestorOf(owner) != null)) {
((HeavyWeightPopup)popup).setCacheEnabled(false);
}
return popup;
}
return null;
}
public static void main(String[] args) throws Exception {
OSType type = OSInfo.getOSType();
if (type != OSType.WINDOWS) {
System.out.println("This test is for Windows only... skipping!");
return;
}
SwingUtilities.invokeAndWait(() -> {
try {
UIManager.setLookAndFeel(new WindowsLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
System.out.println("Creating JFileChooser...");
JFileChooser fileChooser = new JFileChooser();
System.out.println("Test passed: chooser = " + fileChooser);
});
// Test fails if creating JFileChooser hangs
}
public static void main(String[] args) throws Exception {
OSType type = OSInfo.getOSType();
if (type != OSType.WINDOWS) {
System.out.println("This test is for Windows only... skipping!");
return;
}
SwingUtilities.invokeAndWait(() -> {
try {
UIManager.setLookAndFeel(new WindowsLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
System.out.println("Creating JFileChooser...");
JFileChooser fileChooser = new JFileChooser();
System.out.println("Test passed: chooser = " + fileChooser);
});
// Test fails if creating JFileChooser hangs
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
return;
}
toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Robot robot = new Robot();
robot.setAutoDelay(50);
createAndShowGUI();
// Pressed keys: Alt + E + A
// Results: ALT + VK_DEAD_ACUTE + a with accute accent
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
if (state != 3) {
throw new RuntimeException("Wrong number of key events.");
}
}
@Override
public void init() {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
Sysout.createDialogWithInstructions(new String[]{
"Press PASS, this test is for MacOS X only."});
return;
}
System.setProperty("apple.awt.use-file-dialog-packages", "true");
setLayout(new GridLayout(1, 1));
fd = new FileDialog(new Frame(), "Open");
fd.setDirectory(APPLICATIONS_FOLDER);
showBtn = new Button("Show File Dialog");
showBtn.addActionListener(this);
add(showBtn);
String[] instructions = {
"1) Click on 'Show File Dialog' button. A file dialog will come up.",
"2) Navigate to the Applications folder if not already there",
"3) Check that the application bundles can be selected and can not be navigated",
"4) If it's true then the test passed, otherwise it failed."};
Sysout.createDialogWithInstructions(instructions);
}
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Robot robot = new Robot();
robot.setAutoDelay(50);
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
fileChooser = new JFileChooser();
fileChooser.showOpenDialog(null);
}
});
toolkit.realSync();
if (OSInfo.OSType.MACOSX.equals(OSInfo.getOSType())) {
Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_ALT, KeyEvent.VK_L);
} else {
Util.hitKeys(robot, KeyEvent.VK_ALT, KeyEvent.VK_L);
}
checkFocus();
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
return;
}
toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Robot robot = new Robot();
robot.setAutoDelay(50);
createAndShowGUI();
// Pressed keys: Alt + E + A
// Results: ALT + VK_DEAD_ACUTE + a with accute accent
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
if (state != 3) {
throw new RuntimeException("Wrong number of key events.");
}
}
public static void main(String[] args) throws Exception {
OSType type = OSInfo.getOSType();
if (type != OSType.WINDOWS) {
System.out.println("This test is for Windows only... skipping!");
return;
}
SwingUtilities.invokeAndWait(() -> {
try {
UIManager.setLookAndFeel(new WindowsLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
System.out.println("Creating JFileChooser...");
JFileChooser fileChooser = new JFileChooser();
System.out.println("Test passed: chooser = " + fileChooser);
});
// Test fails if creating JFileChooser hangs
}
public static void main(final String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
System.out.println("This test is for OSX, considered passed.");
return;
}
// Default window appearance
showFrame();
step++;
// apple.awt.brushMetalLook appearance
showFrame();
step++;
// Window.style appearance
showFrame();
// images on step 1 and 2 should be same
testImages(images[1], images[2], false);
// images on step 1 and 2 should be different from default
testImages(images[0], images[1], true);
testImages(images[0], images[2], true);
}
public void initialize() {
super.initialize();
// Set the flag which determines which version of Windows should
// be rendered. This flag only need to be set once.
// if version <= 4.0 then the classic LAF should be loaded.
if (OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_95) <= 0) {
isClassicWindows = true;
} else {
isClassicWindows = false;
XPStyle.invalidateStyle();
}
// Using the fonts set by the user can potentially cause
// performance and compatibility issues, so allow this feature
// to be switched off either at runtime or programmatically
//
String systemFonts = java.security.AccessController.doPrivileged(
new GetPropertyAction("swing.useSystemFontSettings"));
useSystemFontSettings = (systemFonts == null ||
Boolean.valueOf(systemFonts).booleanValue());
if (useSystemFontSettings) {
Object value = UIManager.get("Application.useSystemFontSettings");
useSystemFontSettings = (value == null ||
Boolean.TRUE.equals(value));
}
KeyboardFocusManager.getCurrentKeyboardFocusManager().
addKeyEventPostProcessor(WindowsRootPaneUI.altProcessor);
}
public static void main(String[] args) {
if(OSInfo.getOSType() != OSInfo.OSType.WINDOWS){
return;
}
bug6416920 test = new bug6416920();
test.layout.padSelectedTab(SwingConstants.TOP, 0);
if (test.rects[0].width < 0) {
throw new RuntimeException("A selected tab isn't painted properly " +
"in the scroll tab layout under WindowsLookAndFeel " +
"in Windows' \"Windows XP\" theme.");
}
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
return;
}
String icon = "NSImage://NSApplicationIcon";
final Image image = Toolkit.getDefaultToolkit().getImage(icon);
if (!(image instanceof MultiResolutionImage)) {
throw new RuntimeException("Icon does not have resolution variants!");
}
MultiResolutionImage multiResolutionImage = (MultiResolutionImage) image;
int width = 0;
int height = 0;
for (Image resolutionVariant : multiResolutionImage.getResolutionVariants()) {
int rvWidth = resolutionVariant.getWidth(null);
int rvHeight = resolutionVariant.getHeight(null);
if (rvWidth < width || rvHeight < height) {
throw new RuntimeException("Resolution variants are not sorted!");
}
width = rvWidth;
height = rvHeight;
}
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test is suitable only for Windows OS. Skipped.");
return;
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new bug4847375("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
new bug4847375("javax.swing.plaf.metal.MetalLookAndFeel");
}
});
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test is suitable only for Windows OS. Skipped.");
return;
}
// Init toolkit because it shouldn't be interrupted while initialization
Toolkit.getDefaultToolkit();
// Init the sun.awt.shell.Win32ShellFolderManager2.drives field
ShellFolder.get("fileChooserComboBoxFolders");
// To get NPE the path must obey the following rules:
// path.length() == 3 && path.charAt(1) == ':'
final File tempFile = new File("c:\\");
for (int i = 0; i < 10000; i++) {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final Thread thread = new Thread() {
public void run() {
countDownLatch.countDown();
ShellFolder.isFileSystemRoot(tempFile);
}
};
thread.start();
countDownLatch.await();
thread.interrupt();
}
}
/**
* Returns the name of the <code>LookAndFeel</code> class that implements
* the native system look and feel if there is one, otherwise
* the name of the default cross platform <code>LookAndFeel</code>
* class. This value can be overriden by setting the
* <code>swing.systemlaf</code> system property.
*
* @return the <code>String</code> of the <code>LookAndFeel</code>
* class
*
* @see #setLookAndFeel
* @see #getCrossPlatformLookAndFeelClassName
*/
public static String getSystemLookAndFeelClassName() {
String systemLAF = AccessController.doPrivileged(
new GetPropertyAction("swing.systemlaf"));
if (systemLAF != null) {
return systemLAF;
}
OSInfo.OSType osType = AccessController.doPrivileged(OSInfo.getOSTypeAction());
if (osType == OSInfo.OSType.WINDOWS) {
return "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
} else {
String desktop = AccessController.doPrivileged(new GetPropertyAction("sun.desktop"));
Toolkit toolkit = Toolkit.getDefaultToolkit();
if ("gnome".equals(desktop) &&
toolkit instanceof SunToolkit &&
((SunToolkit) toolkit).isNativeGTKAvailable()) {
// May be set on Linux and Solaris boxs.
return "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
}
if (osType == OSInfo.OSType.MACOSX) {
if (toolkit.getClass() .getName()
.equals("sun.lwawt.macosx.LWCToolkit")) {
return "com.apple.laf.AquaLookAndFeel";
}
}
if (osType == OSInfo.OSType.SOLARIS) {
return "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
}
}
return getCrossPlatformLookAndFeelClassName();
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test is suitable only for Windows OS. Skipped.");
return;
}
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new bug4847375("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
new bug4847375("javax.swing.plaf.metal.MetalLookAndFeel");
}
});
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
System.out.println("The test is suitable only for Windows OS. Skipped.");
return;
}
// Init toolkit because it shouldn't be interrupted while initialization
Toolkit.getDefaultToolkit();
// Init the sun.awt.shell.Win32ShellFolderManager2.drives field
ShellFolder.get("fileChooserComboBoxFolders");
// To get NPE the path must obey the following rules:
// path.length() == 3 && path.charAt(1) == ':'
final File tempFile = new File("c:\\");
for (int i = 0; i < 10000; i++) {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final Thread thread = new Thread() {
public void run() {
countDownLatch.countDown();
ShellFolder.isFileSystemRoot(tempFile);
}
};
thread.start();
countDownLatch.await();
thread.interrupt();
}
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
return;
}
toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Robot robot = new Robot();
robot.setAutoDelay(50);
createAndShowGUI();
// Pressed keys: Alt + E + A
// Results: ALT + VK_DEAD_ACUTE + a with accute accent
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_E);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
toolkit.realSync();
if (state != 3) {
throw new RuntimeException("Wrong number of key events.");
}
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS ||
OSInfo.getWindowsVersion() != OSInfo.WINDOWS_VISTA) {
System.out.println("This test is only for Windows Vista. Skipped.");
return;
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
JSlider slider = new JSlider(JSlider.VERTICAL, 0, 100, 0);
Dimension prefferdSize = slider.getPreferredSize();
slider.setPaintTrack(false);
slider.putClientProperty("Slider.paintThumbArrowShape", Boolean.TRUE);
if (prefferdSize.equals(slider.getPreferredSize())) {
throw new RuntimeException();
}
}
});
}
public static void main(String[] args) throws Exception {
if (OSInfo.getOSType() == OSInfo.OSType.MACOSX) {
System.out.println("This test is not applicable for MacOS. Passed.");
return;
}
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createAndShowGUI();
}
});
toolkit.realSync();
test.waitTestResult();
}
public void initialize() {
super.initialize();
// Set the flag which determines which version of Windows should
// be rendered. This flag only need to be set once.
// if version <= 4.0 then the classic LAF should be loaded.
if (OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_95) <= 0) {
isClassicWindows = true;
} else {
isClassicWindows = false;
XPStyle.invalidateStyle();
}
// Using the fonts set by the user can potentially cause
// performance and compatibility issues, so allow this feature
// to be switched off either at runtime or programmatically
//
String systemFonts = java.security.AccessController.doPrivileged(
new GetPropertyAction("swing.useSystemFontSettings"));
useSystemFontSettings = (systemFonts == null ||
Boolean.valueOf(systemFonts).booleanValue());
if (useSystemFontSettings) {
Object value = UIManager.get("Application.useSystemFontSettings");
useSystemFontSettings = (value == null ||
Boolean.TRUE.equals(value));
}
KeyboardFocusManager.getCurrentKeyboardFocusManager().
addKeyEventPostProcessor(WindowsRootPaneUI.altProcessor);
}