java中JLayeredPane的层数有限制吗?

发布于 2024-09-07 19:54:47 字数 3205 浏览 4 评论 0 原文

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

我已成功向 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 中将所有图像组合成一张大图像。但是,我想知道是否还有其他可用的修复程序。

I have a java application for an ATC. I just got started with the GUI. First I have a Mainframe, a JLayeredPane on this mainframe, and panels with labels(having ImageIcons) inside them on the JLayeredPane.

I have successfully added about 4 panels(panels have labels,and labels have ImageIcons) to the JLayeredPane. When I go to add the fifth panel, it gives me a wrong GUI display.

This what I get before adding pnlplane(4 layers):

This is what I get(when I try to add a pnlplane - 5 layers[problem]):

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. However, I would like to know if there is any other fix available out there.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

网名女生简单气质 2024-09-14 19:54:50

你的代码似乎对我来说工作得很好。也就是说我看到了 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 教程的方式/a> 有效。

Your code seems to work fine for me. That is I see 5 images.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文