本文介绍了无法在未修饰的JFrame中混合轻量级和重量级组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据指南。

我期待以下代码将整个窗口绘制为绿色。相反,它将屏幕的一半呈现红色:

I am expecting the following code to paint the entire window green. Instead, it renders half the screen red:

import java.awt.Canvas;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class Testcase extends Canvas
{
  private static final long serialVersionUID = 0L;

  public static void main(String[] arg)
  {
    final boolean enableWorkaround = false;
    EventQueue.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JLayeredPane layers = new JLayeredPane();
        frame.getContentPane().add(layers);

        JPanel green = new JPanel();
        green.setBackground(Color.GREEN);
        green.setBounds(0, 0, 800, 600);
        layers.add(green, Integer.valueOf(1));

        Canvas red = new Canvas();
        red.setBackground(Color.RED);
        red.setBounds(0, 0, 400, 600);
        layers.add(red, Integer.valueOf(0));

        frame.setSize(800, 600);
        if (!enableWorkaround)
          frame.setUndecorated(true);
        frame.setVisible(true);
      }
    });
  }
}

转载于:

java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) Client VM (build 19.0-b09, mixed mode, sharing)

java version "1.7.0-ea"
Java(TM) SE Runtime Environment (build 1.7.0-ea-b124)
Java HotSpot(TM) Client VM (build 20.0-b05, mixed mode, sharing)

我的代码包含任何错误或这是一个合法的JDK错误?

Does my code contain any bugs or is this a legitimate JDK bug?

推荐答案

根据Oracle的说法,这是一个已知的限制,将来修复:

According to Oracle, this is a known limitation that will be fixed in the future: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7012806

这篇关于无法在未修饰的JFrame中混合轻量级和重量级组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 02:32