簡體   English   中英

嘗試使用鼠標偵聽器在數組中顯示圖像

[英]Trying to display images in an array using mouse listener

我試圖了解GUI,並創建了一個包含3張圖片的ImageIcon數組。 我要這樣做,以便用戶可以繼續單擊並顯示新圖片。 我正在嘗試使用actionMouseListener,但它只顯示一張不會改變的圖片。 我對此還很陌生,因此任何有關最有效方式的建議都將非常有用,因為一旦可行,我希望添加更多圖片。 我還在控制台中得到了一個非常奇怪的輸出,這是我以前從未見過的,我也不知道這意味着什么:

2015-11-12 17:49:33.656 java[22322:1488426] CoreText: *** Unmapped   
"e\uFE0F" <CTFont: 0x7fa89f0842a0>{name = .SFNSText-Regular, size = 
13.000000, matrix = 0x0, descriptor = <CTFontDescriptor: 
0x7fa89f084250>{attributes = <CFBasicHash 0x7fa89f0843f0 
[0x7fff77e2d390]>{type = mutable dict, count = 1,
entries =>
2 : <CFString 0x7fff7a657710 [0x7fff77e2d390]>{contents =  
"NSFontNameAttribute"} = <CFString 0x7fff7a64b5d0 [0x7fff77e2d390]>   
{contents = ".SFNSText-Regular"}
}
>}}

我的代碼看起來像

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
@SuppressWarnings("serial")
public class AubsGUI extends JFrame 
{
 private JLabel label, label1; // labels that is on our window  
 private JTextField textfield; //  the window will have writing 
 ImageIcon pic[] = new ImageIcon[3];
 JPanel panel;
 public AubsGUI() // constructor 
{
    setLayout (new FlowLayout()); 
    label = new JLabel (" title "); // creating a label 
    add(label); // adding label to the screen 
    textfield = new JTextField(" Heres a Picture ");//creating a text field
    add(textfield); // adding text field to the screen
    for(int i = 0; i < 3; i++)
    {
        pic[i] = new ImageIcon(getClass().getResource(i +".JPG"));
    }
    label1 = new JLabel();
    label1.setIcon(picture());
    add(label1);
    event e = new event(); 
    label1.addMouseListener(e);
}
public ImageIcon picture()
{
    int i = 0;
    i++;
    return  pic[i];
}
public class event implements MouseListener
{
    public void mouseClicked(MouseEvent e) 
    {
        label1.setIcon(picture());      
    }
    public void mousePressed(MouseEvent e) 
    {


    }
    public void mouseReleased(MouseEvent e) 
    {       
    }
    public void mouseEntered(MouseEvent e) 
    {   
    }
    public void mouseExited(MouseEvent e) 
    {   
    }
}
public static void main (String args [])
{
    AubsGUI aubs = new AubsGUI(); // creates an object aubs from class AubsGUi  
    aubs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // allows window to close and program to end  
    aubs.pack();
    aubs.setVisible(true); // allows you to see window while program runs
    aubs.setTitle(" title️ ");  
}
}

我不知道您的錯誤,但是您的圖片無法交換的問題是因為i是一個始終從0開始的局部變量,所以它永遠不能超過1

public ImageIcon picture()
{
    int i = 0;
    i++;
    return  pic[i];
}

相反, i應該是一個實例變量,因此在每次調用picture ,值都將保留

private int i = 0;

public ImageIcon picture() {
    if (i >= pic.length) {
        i = 0;
    }
    ImageIcon icon = pic[i];
    i++;
    return icon;
}

我稍微修改了方法,以便在第一次調用該方法時,返回第一個圖像,而不是第二個

您還應該僅在事件調度線程的上下文內創建/修改UI,有關更多詳細信息,請參見初始線程

您可能希望通讀Java TM編程語言的代碼約定 ,這將使人們更容易閱讀您的代碼,並使您閱讀其他代碼更容易

我“認為”該錯誤與aubs.setTitle(" title️ "); ,它“似乎”在文本的末尾帶有Unicode字符\️ ,系統或字體找不到對應的映射,但我實際上只是在猜測

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM