繁体   English   中英

Java中的JLayeredPane的层数是否有限制?

[英]Is there a limit to the amount of layers for a JLayeredPane in java?

我有一个ATC的Java应用程序。 我刚开始使用GUI。 首先,我有一台主机,一个JLayeredPane这个主机,并以标签(有面板ImageIcons )内他们在JLayeredPane

我已经成功地向JLayeredPane添加了大约4个面板(面板有标签,标签有ImageIcons )。 当我添加第五个面板时,它给了我错误的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中的一个大图像中。 但是,我想知道是否还有其他修复程序可用。

您的代码对我来说似乎很好。 那就是我看到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中。

甚至更简单的是直接将标签直接添加到分层窗格中,而无需先将其添加到面板中。 这是有关如何使用分层窗格的Swing教程的工作方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM