作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有一个关于 java 中的图像图标的小问题,以及如何限制可以从我的 swing 应用程序的下拉菜单中选择哪些图像图标。
我正在开发一个多平台java程序,但一直无法弄清楚如何根据所使用的操作系统来限制您可以从下拉菜单中选择的图标。
例如。如果我在基于 Windows 的 PC 上选择 Linux 图标,用户应该会收到一条小消息,说明这不是 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);
}
}
到目前为止,当您选择它显示的图标时,它可以正常工作,并且您会在 JTextArea 中收到一条小消息,告诉您操作系统是什么...只需要以某种方式提醒用户他们选择了错误的操作系统图标如果它们在另一个操作系统上!
如有任何建议,我们将不胜感激!
最佳答案
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
在我看来,如果您不希望用户选择错误的值并且您知道自己所在的操作系统,那么您的组合框是不必要的。
关于java - 根据使用的操作系统在 Swing 中加载图像图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14396487/
我正在开发一个需要能够平均三个数字的 Facebook 应用程序。但是,它总是返回 0 作为答案。这是我的代码: $y = 100; $n = 250; $m = 300; $number = ($y
我只是无法弄清楚这一点,也找不到任何对我来说有意义的类似问题。我的问题:我从数据库中提取记录,并在我的网页上以每个面板 12 条的倍数显示它们。因此,我需要知道有多少个面板可以使用 JavaScrip
我是一名优秀的程序员,十分优秀!