gpt4 book ai didi

java - 根据使用的操作系统在 Swing 中加载图像图标

转载 作者:行者123 更新时间:2023-11-30 04:32:04 27 4
gpt4 key购买 nike

有一个关于 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);
}
}

enter image description here

到目前为止,当您选择它显示的图标时,它可以正常工作,并且您会在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com