简体   繁体   English

尝试使用鼠标侦听器在数组中显示图像

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

I have trying to learn about GUI and I created an array of ImageIcon that contains 3 pictures. 我试图了解GUI,并创建了一个包含3张图片的ImageIcon数组。 I want to make it so the user can keep clicking and a new picture will show up. 我要这样做,以便用户可以继续单击并显示新图片。 I am trying to use actionMouseListener but it only displays one picture that wont change. 我正在尝试使用actionMouseListener,但它只显示一张不会改变的图片。 I'm pretty new to this so any advice on the most efficient way would be great because once this works I am hoping to add more pictures. 我对此还很陌生,因此任何有关最有效方式的建议都将非常有用,因为一旦可行,我希望添加更多图片。 I am also getting a really weird output in my console that I have never seen before and I have no idea what it means: 我还在控制台中得到了一个非常奇怪的输出,这是我以前从未见过的,我也不知道这意味着什么:

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"}
}
>}}

My code looks like 我的代码看起来像

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 don't know about your error, but the problem with your images not swapping is because i is a local variable which ALWAYS starts at 0 , so it can never be more then 1 我不知道您的错误,但是您的图片无法交换的问题是因为i是一个始终从0开始的局部变量,所以它永远不能超过1

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

Instead, i should be an instance variable, so on each invocation of picture , the value will be maintained 相反, i应该是一个实例变量,因此在每次调用picture ,值都将保留

private int i = 0;

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

I modified the method slightly, so that when it's first called, the first image is returned, rather then the second 我稍微修改了方法,以便在第一次调用该方法时,返回第一个图像,而不是第二个

You should also be creating/modifying your UI only from within the context of the Event Dispatching Thread, see Initial Threads for more details. 您还应该仅在事件调度线程的上下文内创建/修改UI,有关更多详细信息,请参见初始线程

You might like to have a read through Code Conventions for the Java TM Programming Language , it will make it easier for people to read your code and for you to read others 您可能希望通读Java TM编程语言的代码约定 ,这将使人们更容易阅读您的代码,并使您阅读其他代码更容易

I "think" the error is related to aubs.setTitle(" title️ "); 我“认为”该错误与aubs.setTitle(" title️ "); , which "seems" to have a Unicode character \️ at the end of the text, which the system or font is unable to find a mapping for, but I'm really just having a guess ,它“似乎”在文本的末尾带有Unicode字符\️ ,系统或字体找不到对应的映射,但我实际上只是在猜测

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM