java.awt.Frame.setBackground()在OS X不工作 [英] java.awt.Frame.setBackground() not working in OS X

查看:194
本文介绍了java.awt.Frame.setBackground()在OS X不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决一些UI显示bug在OS X我的Java小程序,我已经打了一个,我想不通。

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.

所有这一切我们打开扩展java.awt.Frame中的窗户似乎忽略了的setBackground()调用,而是使用OS X默认(金属拉丝或灰色渐变,这取决于操作系统版本)。凡是我们打开扩展对话框工作正常不过。

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.

我试图重写paint()方法和绘图的背景颜色那里。然而,这只是部分有效。背景并最终成为一些地方正确的颜色,但帧的所有子组件仍与OS X的背景,不是我设置,因此现在看起来更糟借鉴。同样是这些组件类型(面板,复选框,等)是一对夫妇扩展对话框窗口使用它们做工精细那里,所以我猜必须有一些与帧搞乱事情了。

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.

有没有办法来为在OS X中工作的框架的背景颜色?有没有其他人甚至见过这个?

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

请注意,我是在Java 1.1规范卡住编码,因为我需要支持微软的JVM(不要让我开始...)。

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...).

推荐答案

我找到了一个解决方法。我创建了一个框架包装类创建一个子面板及其所有内容放在该面板。专家组有背景颜色明确设置(而不是让它从其父继承帧)。然后,我改变了扩展帧致以新框架原理包装类的类,问题就走开了。

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.

我的包装是不是真的功能完整,但是它处理什么,我需要它来处理我的习惯。这里的code我用过,万一别人运行到这个问题:

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);
  }
}

这篇关于java.awt.Frame.setBackground()在OS X不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆