【问题标题】:Icon images not loading fast enough图标图像加载速度不够快
【发布时间】:2015-05-12 13:16:34
【问题描述】:

我有一组JPanelJLabel 以及代表剧院座位的图标,所有这些都是使用循环生成的。 加载座位后,已经预订的座位需要有不同的图像图标。所以if(){}检查在所有座位上执行,如果座位已被预订,则在生成后更改标签图标。

但我磁盘上的图像图标加载速度不够快,因此有时面板仅添加到最后一个预订的图标或根本不添加。 每个代表椅子的面板还添加了MouseListener 接口。因此,在鼠标悬停或单击ImageIcon 时添加到面板的对象也会发生变化,发生这种情况时会有太多延迟。我认为这与磁盘上的图像有关!。

  1. 如何在内存中加载和存储这些 2,78 KB 大小的图标图像并在内存中引用它,这样才不会延迟读取它们?

  2. 单击座位时,我需要更改该座位的标签图像并从该座位上删除鼠标侦听器。有没有一种方法可以将鼠标侦听器移至该特定座位,而无需引用特定的鼠标侦听器。我需要在鼠标侦听器本身之外执行此操作!

    panel.removeAll();
    

    不删除生成面板时添加的鼠标侦听器。

public void drawSeats(int ammountSeat, int localLength, int localWidth) {

        pnlSeatsHolder = new JPanel();
        pnlSeatsHolder.setPreferredSize(new Dimension(localLength * 40,localLength * 45)); 
        pnlSeatsHolder.setLayout(new FlowLayout());

        for (int d = 0; d <= (ammountSeat); d++) {
            imgIconYellow = new ImageIcon("seatYellow.png");
            imgIconBlue = new ImageIcon("seatBlue.png");
            imgIconRed = new ImageIcon("seatRed.png");

            JButton chairs = new JButton();
            chairs.setPreferredSize(new Dimension(30, 40));    
            pnlSeatsHolder.add(chairs);

            chairs.addMouseListener(new MouseListener() {
                public void mouseClicked(MouseEvent e) {
                    for (int i = 0; i < listSeatsObjects.size(); i++) {
                        if (listSeatsObjects.get(i).equals(e.getSource())) {
                /*I need to do this also outside of this method! how can i refer to this MouseListener
                 * to forexample do the equivalent of chairs.removeMouseListener(this);*/
                            chairs.removeAll();
                            chairs.setIcon(imgIconRed);
                            chairs.repaint();
                            chairs.removeMouseListener(this);
                            // send information of the chair somewhere else
                        }
                    }
                }
                public void mouseEntered(MouseEvent e) {
                    // chairs.setBackground(Color.blue);
                    chairs.removeAll();
                    chairs.setIcon(imgIconBlue);
                    chairs.repaint();
                }
                public void mouseExited(MouseEvent e) {
                    // chairs.setBackground(Color.BLACK);
                    chairs.removeAll();
                    chairs.setIcon(imgIconYellow);
                    chairs.repaint();
                }
                public void mousePressed(MouseEvent e) {
                }
                public void mouseReleased(MouseEvent e) {
            }
        });
    }
}

所以这是它自己在调用时绘制座位的方法。我按照@AndrewThompson 的建议做了一些修改,而不是我现在使用的 JButtons 的 JPanel,但是发生的情况是图像根本没有加载到按钮上。我错过了什么?也没有鼠标悬停..如果我有例如 charis.setBackgroundColor(); 它确实有效在悬停或单击时.. 所以我现在需要在单击和悬停时更改按钮图像,我尝试过 chairs.chairs.setRolloverIcon();和 .setIcon();两者都不能正常工作。怎么了。我的图像与类文件位于同一目录中.. 所以这不是问题..

int localLength, int localWidth 是座位将进入的房间的大小。大约 1m^2/seat

【问题讨论】:

  • “我如何加载和存储这些图标图像” 总共有多少(不同)图像?
  • 您还应该考虑使用JButtonJToggleButtonActionListener,而不是JLabelMouseListener。按钮不仅可以对鼠标和键盘输入做出反应,而且该按钮还支持在悬停、按下、聚焦等时更改图标。
  • 3.红色座位,蓝色,&& 黄色,生成的座位数量因其他周边而异。但是这 3 个图像在生成的所有座位之间共享。黄色被添加到所有开始。鼠标悬停时变为蓝色,鼠标单击时变为红色。
  • 1) 只需在应用程序启动期间阅读它们,并在一个类(可能是单例)中保留一个引用,以便在需要时访问/使用它们。 2) 见@AndrewThompson cmets。
  • 我写了一个很长的学校项目shcool。我真的不想改变太多。解决了,它正在工作,但这种滞后很烦人。例如,这是鼠标悬停时需要做的事情: pnlChair.removeAll(); pnlChair.add(lblseatRed); pnlChair.repaint(); pnlChair.removeMouseListener(this);

标签: java swing load icons jpanel


【解决方案1】:

对于三个图像,在类初始化时加载它们,并将它们存储为类的属性。当然,这 3 个图像中的每一个都可以根据需要用于任意数量的图标。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 1970-01-01
    • 2020-05-09
    • 2016-11-20
    • 2021-02-03
    相关资源
    最近更新 更多