【问题标题】:Is there a limit to the amount of layers for a JLayeredPane in java?java中JLayeredPane的层数有限制吗?
【发布时间】:2010-07-02 15:48:11
【问题描述】:

我有一个用于 ATC 的 Java 应用程序。我刚开始使用 GUI。首先我有一个大型机,在这个大型机上有一个JLayeredPane,在JLayeredPane 上有一个带有标签(有ImageIcons)的面板。

我已成功将大约 4 个面板(面板有标签,标签有 ImageIcons)添加到 JLayeredPane。当我去添加第五个面板时,它给了我一个错误的 GUI 显示。

这是我在添加 pnlplane(4 层)之前得到的:

这是我得到的(当我尝试添加 pnlplane - 5 层[问题]):

这是我应该得到的:

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

public class GUI extends JFrame {

JFrame main = new JFrame();

JLayeredPane jp = new JLayeredPane();

//Add JPanels here
JPanel pnlbackground = new JPanel();
JPanel pnlrunwayone = new JPanel();
JPanel pnlrunwaytwo = new JPanel();
JPanel pnlholding = new JPanel();
JPanel pnlplane = new JPanel();




//Add ImageIcons here
ImageIcon imgbackground = new ImageIcon("background.gif");
ImageIcon imgrunwayone = new ImageIcon("runway01.gif");
ImageIcon imgrunwaytwo = new ImageIcon("runway01.gif");
ImageIcon imgholding = new ImageIcon("holding01.gif");
ImageIcon imgplane = new ImageIcon("plane.gif");




//Add JLabels here
JLabel lblbackground = new JLabel(imgbackground);
JLabel lblrunwayone = new JLabel(imgrunwayone);
JLabel lblrunwaytwo = new JLabel(imgrunwaytwo);
JLabel lblholding = new JLabel(imgholding);
JLabel lblplane = new JLabel(imgplane);




public GUI() {

    //Background
    pnlbackground.setOpaque(false);
    pnlbackground.setBounds(0, -5, 1024, 768);
    pnlbackground.add(lblbackground);

    //Runway one
    pnlrunwayone.setOpaque(false);
    pnlrunwayone.setBounds(170, 404, 685, 39);
    pnlrunwayone.add(lblrunwayone);

    //Runway two
    pnlrunwaytwo.setOpaque(false);
    pnlrunwaytwo.setBounds(170, 443, 685, 39);
    pnlrunwaytwo.add(lblrunwaytwo);

    //        Holding pattern
    pnlholding.setOpaque(false);
    pnlholding.setBounds(0, 00, 330, 143);
    pnlholding.add(lblholding);

    //plane
    pnlholding.setOpaque(false);
    pnlholding.setBounds(0, 0, 48, 60);
    pnlholding.add(lblplane);


    //Adding them to each other
    add(jp);
    jp.add(pnlbackground, new Integer(0));
    jp.add(pnlrunwayone, new Integer(1));
    jp.add(pnlrunwaytwo, new Integer(2));
    jp.add(pnlholding, new Integer(3));
    jp.add(pnlplane, new Integer(4));




    //MainFrame properties
    setSize(1024, 768);
    setBackground(Color.BLACK);
    setTitle("Air Traffic Control");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);


}

public static void main(String[] args) {

    new GUI();

}
}

更简单的方法是将所有图像放在 Photoshop 中的一张大图像中。不过,我想知道是否有其他可用的修复方法。

【问题讨论】:

  • 我正在试图弄清楚你为什么要这样做,无论你在做什么 :) 我会亲自使用 Canvas 并听取点击等。
  • 但是如何使用标签将图像放置在画布上?或者是其他东西。我喜欢听听你的程序

标签: java image swing jlayeredpane


【解决方案1】:

您的代码对我来说似乎工作正常。也就是说,我看到了 5 张图片。

这看起来有点奇怪:

pnlholding.setOpaque(false); 
pnlholding.setBounds(0, 00, 330, 143); 
pnlholding.add(lblholding); 

//plane 
pnlholding.setOpaque(false); 
pnlholding.setBounds(0, 0, 48, 60); 
pnlholding.add(lblplane); 

我认为应该将 lblplane 添加到 pnlplane 中。

更简单的方法是直接将标签添加到分层窗格中,无需先将其添加到面板中。这就是 How to Use Layered Panes 上的 Swing 教程的工作方式。

【讨论】:

  • 我会尝试直接添加标签,然后我将标记为答案
猜你喜欢
  • 2017-09-19
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 2011-05-22
  • 2017-10-11
  • 1970-01-01
相关资源
最近更新 更多