2015-05-12 57 views
1

我有一個JPanelJLabel的陣列,圖標代表劇院中的一個座位,所有這些都是使用循環生成的。 加載座位後,已預訂的座位需要有不同的圖像圖標。因此,if(){}檢查在所有座位上執行,以便在座位被預訂後更改標籤圖標,之後它們被生成。圖標圖像加載速度不夠快

但我的磁盤上的圖像圖標加載速度不夠快,所以有時面板只會添加到最後一個預訂或根本不加載。 代表座椅的每個面板也都添加了接口。因此,在鼠標懸停或單擊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建議,而不是JPanels我現在使用JButtons,但會發生的是,圖像不會加載到按鈕..我錯過了什麼?鼠標懸停無論是.. tho它確實工作,如果我有例如charis.setBackgroundColor();在懸停或點擊..所以我現在我需要寧願改變按鈕圖片點擊和懸停時,我已經試過chairs.chairs.setRolloverIcon();和.setIcon();兩者都沒有工作。哪裏不對。我的圖像與類文件位於同一目錄..所以不能成爲問題..

int localLength,int localWidth是座位將被繪製的房間的大小。約1立方米/座

+0

*「我如何加載和存儲這些圖標圖像」*總共有多少(不同)圖像? –

+2

您還應該考慮在'MouseListener'中使用帶有'ActionListener'的'JButton'或'JToggleButton',而不是'JLabel'。按鈕不僅對鼠標和鍵盤輸入有反應,而且該按鈕還支持在懸停,按下,對焦等情況下更改圖標。 –

+0

3.紅色座椅,藍色&&黃色座椅的生成量取決於其他周長。但是這3張圖片是由所有席位共享的。 黃色是在開始時添加到所有。在鼠標懸停時變成藍色,點擊鼠標時變成紅色。 – user2624724

回答

1

對於三個圖像,當類初始化時加載它們並將它們存儲爲類的屬性。當然,3個圖像中的每一個都可以根據需要用於多個圖標中。