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

发布于 2024-07-24 03:27:00 字数 510 浏览 3 评论 0原文

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

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

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

有没有办法为 OS X 中的框架设置背景颜色? 还有其他人以前见过这个吗?

请注意,我一直在根据 Java 1.1 规范进行编码,因为我需要支持 Microsoft JVM(别让我开始......)。

I'm trying to iron out some UI rendering bugs in my java applet in OS X, and I've hit one that I can't figure out.

All the windows that we open that extend java.awt.Frame seem to ignore the setBackground() calls, and instead use the OS X default (brushed metal or gray gradient, depending on the OS version). Anything we open that extends Dialog works fine though.

I tried overriding the paint() method and drawing the background color there. However, this only partially works. The background does end up as the correct color in some places, but all child components of the Frame still draw with the OS X background, not the one I set, and so now it looks even worse. Those same component types (Panel, Checkbox, etc) are used in a couple Dialog-extending windows and they work fine there, so I'm guessing there has to be something with Frame that's messing things up.

Is there a way to set the background color for a Frame that works in OS X? Has anyone else even seen this before?

Note that I'm stuck coding against the Java 1.1 spec, as I'm required to support the Microsoft JVM (don't get me started...).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2024-07-31 03:27:00

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

I found a workaround. I created a wrapper class for Frame that creates a child Panel and places all its contents in that panel. The Panel has the background color set explicitly (instead of letting it inherit from its parent Frame). I then changed the classes that extended Frame to extend my new FrameW wrapper class, and the problem went away.

My wrapper isn't really functionally complete, but it handles what I need it to handle for the usages I have. Here's the code I used, in case anyone else runs into this issue:

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);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文