1

こんにちは、Java でスイング プログレス バーの色を変更しようとしています。最初に、次のコードを使用してみました。

UIManager.put("ProgressBar.selectionBackground", Color.black);
UIManager.put("ProgressBar.selectionForeground", Color.white);
UIManager.put("ProgressBar.background", Color.white);
UIManager.put("ProgressBar.foreground", Color.RED);
m_progressBar.setBackground(Color.BLACK);
m_progressBar.setForeground(Color.red);

しかし、どのコマンドも効果がないようです!!! 私が設定しない限りm_progressBar.stringPainted(true)、フォアグラウンドは変更されません。

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); とにかく、UI が初期化される前に呼び出される次のコマンドから問題が発生することはわかっています。

だから私の質問は: 1) 好奇心から:呼び出しがフォアグラウンド UI 設定をオーバーライドする
のはなぜですか? 2)コードの先頭を保持し、UI 設定を上書きするには どうすればよいですか?stringPainted(true)
UIManager.setLookAndFeel

ありがとうございました!!

4

1 に答える 1

2

これを試して.............

`Changing the Look and Feel After Startup`

You can change the L&F with setLookAndFeel even after the program's GUI is visible. To make existing components reflect the new L&F, invoke the SwingUtilities updateComponentTreeUI method once per top-level container. Then you might wish to resize each top-level container to reflect the new sizes of its contained components.

例えば:

UIManager.setLookAndFeel(lnfName);
SwingUtilities.updateComponentTreeUI(frame);
frame.pack();

つまり、トップレベル コンテナのルック アンド フィールを変更できます (トップレベル コンテナのみに限定されているのか、それともこのメソッドを任意のコンテナに適用できるのかはテストしていません)。または、あなたの質問にさらに正確に答えるには、単一のコンポーネントをスタイリングする方法はないと思いますが、(トップレベル) コンテナー内の単一のコンポーネントの方法はあります。

于 2012-09-19T09:43:03.293 に答える