【问题标题】:java.awt.Frame.setBackground() not working in OS Xjava.awt.Frame.setBackground() 在 OS X 中不起作用
【发布时间】:2010-10-31 08:43:15
【问题描述】:

我正在尝试解决我在 OS X 中的 java 小程序中的一些 UI 渲染错误,但我遇到了一个我无法解决的问题。

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

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

有没有办法为在 OS X 中工作的 Frame 设置背景颜色?有没有其他人以前见过这个?

请注意,我被困在 Java 1.1 规范的编码中,因为我需要支持 Microsoft JVM(不要让我开始...)。

【问题讨论】:

  • Swing 在 1.1 上运行,不是吗?
  • Swing 至少在 1.2 之前才存在。我不能使用它,而且现在这样重写 UI 是不可能的。
  • Swing 不是 1.1 的一部分,但可用于它。从 AWT 到 Swing 的大部分转换是搜索和替换操作。
  • 我们正在慢慢切换到 Flex 应用程序,所以我们真的不想在应用程序上做任何重要的工作,而是切换到 Swing(即使只是查找/替换)将是一个大项目。

标签: java macos awt


【解决方案1】:

我找到了解决方法。我为 Frame 创建了一个包装类,它创建了一个子面板并将其所有内容放在该面板中。 Panel 明确设置了背景颜色(而不是让它从其父 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);
  }
}

【讨论】:

  • 由于这似乎可行,而且没有其他人提出任何建议,我想我会将其标记为答案
猜你喜欢
  • 2015-06-15
  • 2023-03-14
  • 2015-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多