![](/img/trans.png)
[英]java.awt.Frame.setBackground(Color arg0) not displaying PINK color
[英]java.awt.Frame.setBackground() not working in OS X
我試圖消除OS X中Java小程序中的一些UI渲染錯誤,但遇到了一個我不知道的錯誤。
我們打開的所有擴展java.awt.Frame的窗口似乎都忽略了setBackground()調用,而是使用OS X的默認設置(金屬拉絲或灰色漸變,具體取決於OS版本)。 我們打開擴展Dialog的所有內容都可以正常工作。
我嘗試覆蓋paint()方法並在那里繪制背景色。 但是,這僅部分起作用。 在某些地方,背景確實以正確的顏色顯示,但是Frame的所有子組件仍然使用OS X背景繪制,而不是我設置的背景,因此現在看起來更糟了。 這些相同的組件類型(Panel,Checkbox等)在幾個Dialog擴展窗口中使用,並且在其中可以正常工作,因此我猜測Frame一定有東西在搞亂。
有沒有一種方法可以為OS X中可用的框架設置背景顏色? 有人看過嗎?
請注意,由於我需要支持Microsoft JVM,所以我堅持按照Java 1.1規范進行編碼(不要讓我開始...)。
我找到了解決方法。 我為Frame創建了一個包裝器類,該包裝器類創建了一個子面板,並將其所有內容放置在該面板中。 面板具有明確設置的背景顏色(而不是讓其從其父框架繼承)。 然后,我更改了擴展Frame的類,以擴展新的FrameW包裝器類,問題消失了。
我的包裝器實際上並不是功能完整的,但是可以處理我需要的包裝,以適應我的用法。 這是我使用的代碼,以防其他任何人遇到此問題:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.LayoutManager;
import java.awt.Panel;
/**
* Wrapper for java.awt.Frame that wraps the contents in a Panel. This is done
* because Frames in OS X appear to ignore the background color, but if the
* contents are wrapped in a Panel and that Panel is given the background color
* then it works fine.
*/
public class FrameW extends Frame {
private Panel wrapper;
/** Constructs the Frame wrapper */
public FrameW() {
super();
init();
}
/**
* Constructs the Frame wrapper.
* @param title The title to give the frame.
*/
public FrameW(String title) {
super(title);
init();
}
public Component add(Component comp) {
return wrapper.add(comp);
}
public Component add(String name, Component comp) {
return wrapper.add(name, comp);
}
public Component add(Component comp, int index) {
return wrapper.add(comp, index);
}
public void add(Component comp, Object constraints) {
wrapper.add(comp, constraints);
}
public void add(Component comp, Object constraints, int index) {
wrapper.add(comp, constraints, index);
}
public LayoutManager getLayout() {
return wrapper.getLayout();
}
public void setLayout(LayoutManager mgr) {
/* setLayout is called by Frame's constructor before our init runs. */
if(this.wrapper == null) { return; }
wrapper.setLayout(mgr);
}
public void setBackground(Color c) {
super.setBackground(c);
wrapper.setBackground(c);
}
/**
* Overriding the insets of the frame will cause the panel used for the
* background color to not take up the entire frame's area. Instead, override
* FrameW.getContentInsets() for setting the insets of the content.
* @return The frame's insets
*/
public Insets getInsets() {
return super.getInsets();
}
/**
* Override this instead of getInsets() in order to set the insets of the
* FrameW.
* @return The insets for the content
*/
public Insets getContentInsets() {
return new Insets(0, 0, 0, 0);
}
private void init() {
this.wrapper = new Panel() {
public Insets getInsets() {
return FrameW.this.getContentInsets();
}
};
super.setLayout(new BorderLayout());
super.add(this.wrapper, BorderLayout.CENTER);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.