java.awt.Frame.setBackground()在OS X中不工作

java.awt.Frame.setBackground()在OS X中不工作,java,macos,awt,Java,Macos,Awt,我正在努力解决OSX中java小程序中的一些UI呈现错误,但我遇到了一个我无法解决的错误 我们打开的所有扩展java.awt.Frame的窗口似乎都忽略了setBackground()调用,而是使用OS X默认值(拉丝金属或灰色渐变,取决于OS版本)。不过,我们打开的任何扩展对话框都可以正常工作 我尝试重写paint()方法并在那里绘制背景色。然而,这只是部分有效。在某些地方,背景确实是正确的颜色,但框架的所有子组件仍然使用OSX背景绘制,而不是我设置的背景,因此现在看起来更糟。这些相同的组件类

我正在努力解决OSX中java小程序中的一些UI呈现错误,但我遇到了一个我无法解决的错误

我们打开的所有扩展java.awt.Frame的窗口似乎都忽略了setBackground()调用,而是使用OS X默认值(拉丝金属或灰色渐变,取决于OS版本)。不过,我们打开的任何扩展对话框都可以正常工作

我尝试重写paint()方法并在那里绘制背景色。然而,这只是部分有效。在某些地方,背景确实是正确的颜色,但框架的所有子组件仍然使用OSX背景绘制,而不是我设置的背景,因此现在看起来更糟。这些相同的组件类型(面板、复选框等)在扩展窗口的两个对话框中使用,它们在那里工作得很好,所以我猜一定是框架有什么东西把事情搞砸了

有没有办法为在OSX中工作的帧设置背景色?以前有人见过这个吗


请注意,我一直在按照Java 1.1规范编码,因为我需要支持Microsoft JVM(不要让我开始…。

我找到了一个解决方法。我为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);
  }
}

我找到了一个解决办法。我为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);
  }
}

Swing在1.1版上运行,不是吗?Swing至少在1.2版之前都不存在。我不能使用它,现在像这样重写UI是不可能的。Swing不是1.1的一部分,但它是可用的。从AWT到Swing的转换在很大程度上是一个搜索和替换操作。我们正在缓慢地切换到Flex应用程序,因此我们不想在应用程序上做任何重要的工作,而切换到Swing(即使只是查找/替换)将是一个大项目。Swing运行于1.1,不是吗?Swing至少在1.2之前不存在。我不能使用它,现在像这样重写UI是不可能的。Swing不是1.1的一部分,但它是可用的。从AWT到Swing的转换在很大程度上是一个搜索和替换操作。我们正在缓慢地切换到Flex应用程序,因此我们不想在应用程序上做任何重要的工作,也不想切换到Swing(即使只是查找/替换)这将是一个大项目。因为这似乎可行,没有人提出任何建议。我想我会将此标记为答案。因为这似乎可行,没有人提出任何建议。我想我会将此标记为答案