1

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にオペレーティングシステムが何であるかを示す小さなメッセージが表示されます...ユーザーが間違ったオペレーティングシステムアイコンを選択した場合は、何らかの方法でユーザーに警告する必要があります。別のオペレーティングシステムで!

任意の提案をいただければ幸いです!

4

1 に答える 1

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
    }

これらの値のより完全なリストはここで入手できます

ユーザーに間違った値を選択させたくなく、使用しているOSがわかっている場合は、コンボボックスは不要のようです。

于 2013-01-18T10:13:20.373 に答える