2013-01-18 96 views
1

有关于Java和如何图像图标把一个限制什么图像图标可以从我的Swing应用程序的下拉菜单中选择了一个小问题。加载图片图标依赖于操作系系统在使用

我开发一个多平台的Java程序和倾斜的生活工作如何限制你可以从下拉菜单中,根据所使用的操作系系统选择图标。

例如,如果我选择一个基于Windows的PC上的Linux图标,用户应该得到一个litte消息,说这不是Linux,但Windows再次选择。

到目前为止我的代码工作的,因为它会加载图标没有错误等

private String img[] = { 
    "default.png", 
    "window.png", 
    "linix.png", 
    "macos.png", 
    "solaris.png"}; 

private ImgIcon[] icon = { 
    new ImageIcon(getClass().getResource(img[0])), 
    new ImageIcon(getClass().getResource(img[1])), 
    new ImageIcon(getClass().getResource(img[2])), 
    new ImageIcon(getClass().getResource(img[3])), 
    new ImageIcon(getClass().getResource(img[4]))}; 

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) 
{ 
if (IsWin()) { 
    jTextArea1.setText("Detected OS : " + os); 
    } 
else if (IsMac()) { 
    jTextArea1.setText("Detected OS : " + os); 
    } 
else if (IsLin()) { 
    jTextArea1.setText("Detected OS : " + os); 
    } 
else if(IsSol()) { 
    jTextArea1.setText("\Detected OS : " + os); 
    } 
} 

enter image description here

到目前为止好,当你选择它显示的图标,它的作品,你会得到一点消息在JTextArea中告诉你你的操作系统是什么......只需要以某种方式提醒用户他们选择了错误的操作系统图标,如果他们在另一个操作系统上!

任何建议,将不胜感激!

+1

OS-检测是相当简单:'System.getProperty( “os.name”);'。现在,如果你事先知道操作系统,为什么你需要一个组合框来选择? –

+0

这种快速reply..I @GuillaumePolet感谢我使用System.getProperty( “os.name”);通过我的节目只是想将一个限制,如果用户即他们选择Linux的图标,并在Mac或基于Windows的机器 –

+0

不要让用户错误选择了错误的图像。让自己是正确的,并自动选择对他正确的图像:-) –

回答

2
String osName = System.getProperty("os.name"); 
    if (osName.indexOf("Mac OS") > -1) { 
     // Mac OS 
    } else if (osName.indexOf("Windows") > -1) { 
     // Windows 
    } else if (osName.indexOf("Linux") > -1) { 
     // Linux 
    } else if (osName.indexOf("Solaris") > -1) { 
     // Solaris 
    } else { 
     // Something else 
    } 

你可以得到这些值here

更完整的列表在我看来,你的组合框是不必要的,如果你不希望用户选择了错误的价值,你知道你是哪个操作系统上。