Windows上でのJRadioButtonのアイコンを変更する7
-
23-09-2019 - |
質問
私はButtonGroupをからのすべてのJRadioButtonのアイコンを変更する方法を作成しました
public void setRadioButtonIcons(final ButtonGroup gruppe){
Enumeration<AbstractButton> gruppeEnum = gruppe.getElements();
while (gruppeEnum.hasMoreElements()){
AbstractButton radio = gruppeEnum.nextElement();
Icon unselIcon = new ImageIcon( Thread.currentThread().getContextClassLoader().getResource("checkbox0.jpg").getPath());
Icon selIcon = new ImageIcon( Thread.currentThread().getContextClassLoader().getResource("checkbox1.jpg").getPath());
radio.setIcon(unselIcon);
radio.setSelectedIcon(selIcon);
}
}
このは、Java 1.6.0_16とUbuntuの下に罰金を動作します。
私は、Java 1.6.0_18を使用してWindows 7の下Methodeのを使用する場合は、、アイコンはつくれません。彼らは、単に表示されません。プログラムの開発がnullpointerを投げる...それは、アイコンを見つけていないが、それらは表示されません。何か案は?私が窓の下にこのような単純な機能を使用できないことを信じることはやや難しいようです。
私は、GIFやJPGでそれを試してみました。私はまた、jarファイル内の画像を入れて、ファイルシステムからそれらをロードしようとした - 。>同じ結果
編集:この設定では、ファイルはjarファイルからロードされます。
解決
Icon unselIcon = new ImageIcon( Thread.currentThread().getContextClassLoader().getResource("checkbox0.jpg").getPath());
Icon selIcon = new ImageIcon( Thread.currentThread().getContextClassLoader().getResource("checkbox1.jpg").getPath());
あなたがある、getPath()を呼び出すべきではありませんが、ちょうどである必要があります:
Icon unselIcon = new ImageIcon( Thread.currentThread().getContextClassLoader().getResource("checkbox0.jpg"));
Icon selIcon = new ImageIcon( Thread.currentThread().getContextClassLoader().getResource("checkbox1.jpg"));
これは、パスやイメージアイコンで瓶内のリソースにアクセスすることはできませんうまくURLを使用して画像を読み込むことができます。
あなたはまだあなたのアイコンが表示されない場合は、それはあなたが使用しているL&Fは、これらのアイコンを使用して、代わりに独自のを使用していないことかもしれません。おそらく、別のL&Fを使用してコードをテストしてみます。
他のヒント
このように、getPath()
への呼び出しを削除してください
public void setRadioButtonIcons(final ButtonGroup gruppe) {
Enumeration<AbstractButton> gruppeEnum = gruppe.getElements();
while (gruppeEnum.hasMoreElements()){
AbstractButton radio = gruppeEnum.nextElement();
Icon unselIcon = new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("checkbox0.jpg"));
Icon selIcon = new ImageIcon(Thread.currentThread().getContextClassLoader().getResource("checkbox1.jpg"));
radio.setIcon(unselIcon);
radio.setSelectedIcon(selIcon);
}
}
問題がURL.getPath()
はあなたにImageIcon
文字列コンストラクタを期待することを必ずしもソートの有効な文字列のファイル名ではない文字列のURLを、与えることです。幸い、ImageIcon
はURL
オブジェクトを理解し、別のコンストラクタを持っており、そのコールgetPath()
する必要はありません。
所属していません StackOverflow