[英]Is there a limit to the amount of layers for a JLayeredPane in java?
I have a java application for an ATC. 我有一个ATC的Java应用程序。 I just got started with the GUI. 我刚开始使用GUI。 First I have a Mainframe, a JLayeredPane
on this mainframe, and panels with labels(having ImageIcons
) inside them on the JLayeredPane
. 首先,我有一台主机,一个JLayeredPane
这个主机,并以标签(有面板ImageIcons
)内他们在JLayeredPane
。
I have successfully added about 4 panels(panels have labels,and labels have ImageIcons
) to the JLayeredPane
. 我已经成功地向JLayeredPane
添加了大约4个面板(面板有标签,标签有ImageIcons
)。 When I go to add the fifth panel, it gives me a wrong GUI display. 当我添加第五个面板时,它给了我错误的GUI显示。
This what I get before adding pnlplane (4 layers): 这是我在添加pnlplane (4层)之前得到的:
This is what I get(when I try to add a pnlplane - 5 layers[problem]): 这就是我得到的(当我尝试添加pnlplane -5层[问题]):
This is what I should have got: 这是我应该得到的:
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();
}
}
The easier way out is to go and put all the images together to one big image in photoshop. 更简单的方法是将所有图像放到photoshop中的一个大图像中。 However, I would like to know if there is any other fix available out there. 但是,我想知道是否还有其他修复程序可用。
Your code seems to work fine for me. 您的代码对我来说似乎很好。 That is I see 5 images. 那就是我看到5张图片。
This looks a little strange: 这看起来有点奇怪:
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);
I think the lblplane should be added to pnlplane. 我认为lblplane应该添加到pnlplane中。
Even easier is to just add the label to the layered pane directly, there is no need to add it to a panel first. 甚至更简单的是直接将标签直接添加到分层窗格中,而无需先将其添加到面板中。 This is the way the Swing tutorial on How to Use Layered Panes works. 这是有关如何使用分层窗格的Swing教程的工作方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.