简体   繁体   English

按下JButton时尝试更改图像

[英]Trying to change image when JButton pressed

I am trying to change the Image on the panel when the any of the JButtons are pressed. 我试图在按下任何JButton时更改面板上的Image。 I have set up an array of images and need it to change to the next image in the array once it has been pressed. 我已经设置了一个图像数组,一旦按下它就需要将其更改为该数组中的下一个图像。 Here is my code: 这是我的代码:

public class SimpleGui implements ActionListener {
    JButton button = new JButton("Very Happy");
    JButton buttonTwo = new JButton("Happy");
    JButton buttonThree = new JButton("Neutral");
    JButton buttonFour = new JButton("Sad");
    JButton buttonFive = new JButton("Very Sad");
    static int[] ButtonArray = new int[5];
    private static String[] imageList = { "res/snow.jpg", "res/test-gm.jpg" };

    public int i = 0;

    public static void main(String[] args) throws FileNotFoundException {

        SimpleGui gui = new SimpleGui();
        gui.go();

        File file = new File("out.txt");
        FileOutputStream fos = new FileOutputStream(file);
        PrintStream ps = new PrintStream(fos);
        System.setOut(ps);

        ButtonArray[0] = 0;
        ButtonArray[1] = 0;
        ButtonArray[2] = 0;
        ButtonArray[3] = 0;
        ButtonArray[4] = 0;

    }

    public void go() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setBackground(Color.darkGray);

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        button.addActionListener(this);
        buttonTwo.addActionListener(this);
        buttonThree.addActionListener(this);
        buttonFour.addActionListener(this);
        buttonFive.addActionListener(this);
        panel.add(button);
        panel.add(buttonTwo);
        panel.add(buttonThree);
        panel.add(buttonFour);
        panel.add(buttonFive);

        frame.getContentPane().add(BorderLayout.EAST, panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(650, 600);
        frame.setVisible(true);

        ImageIcon image = new ImageIcon(imageList[i]);
        ImageIcon image1 = new ImageIcon(imageList[i + 1]);
        JLabel label = new JLabel("", image, JLabel.CENTER);
        JPanel panel2 = new JPanel(new BorderLayout());
        panel2.add(label, BorderLayout.CENTER);
        panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
        frame.add(panel2, BorderLayout.CENTER);

        frame.setVisible(true);
    }

    public void actionPerformed(ActionEvent event) {

        if (event.getSource() == button) {
            ButtonArray[0] = 1;
            ButtonArray[1] = 0;
            ButtonArray[2] = 0;
            ButtonArray[3] = 0;
            ButtonArray[4] = 0;
            System.out.println("Very Happy");

        }
        // buttonTwo = (JButton) event.getSource();
        if (event.getSource() == buttonTwo) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 1;
            ButtonArray[2] = 0;
            ButtonArray[3] = 0;
            ButtonArray[4] = 0;
            System.out.println("Happy");

        }
        // buttonThree = (JButton) event.getSource();
        if (event.getSource() == buttonThree) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 0;
            ButtonArray[2] = 1;
            ButtonArray[3] = 0;
            ButtonArray[4] = 0;
            System.out.println("Neutral");

        }
        // buttonFour = (JButton) event.getSource();
        if (event.getSource() == buttonFour) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 0;
            ButtonArray[2] = 0;
            ButtonArray[3] = 1;
            ButtonArray[4] = 0;
            System.out.println("Sad");

        }

        // buttonFive = (JButton) event.getSource();
        if (event.getSource() == buttonFive) {
            ButtonArray[0] = 0;
            ButtonArray[1] = 0;
            ButtonArray[2] = 0;
            ButtonArray[3] = 0;
            ButtonArray[4] = 1;
            System.out.println("Very Sad");

        }

        // System.out.println(Arrays.toString(ButtonArray));
        // ImageIcon image = (imageList[i]);

    }

}

I don't really see what most parts of you code are supposed to do. 我真的看不到您的大部分代码应该做什么。 So instead, here's a minimal example that should do what you are asking about: One label, and two buttons setting different images to that label. 因此,下面是一个最小的示例,该示例应满足您的要求:一个标签,以及两个将不同图像设置到该标签的按钮。

ImageIcon[] images = new ImageIcon[] {
        new ImageIcon("foo.gif"),
        new ImageIcon("bar.gif"),
        new ImageIcon("blub.gif")
};

JFrame frame = new JFrame("Test");
frame.getContentPane().setLayout(new FlowLayout());

JLabel label = new JLabel(images[0]);
frame.getContentPane().add(label);

JButton button1 = new JButton("Image 1");
button1.addActionListener(e -> label.setIcon(images[0]));
frame.getContentPane().add(button1);

JButton button2 = new JButton("Image 2");
button2.addActionListener(e -> label.setIcon(images[1]));
frame.getContentPane().add(button2);

frame.pack();
frame.setVisible(true);

Note that this is using Lambda functions (Java 8) but you can do the same with one or more "real" ActionListener classes. 请注意,这使用的是Lambda函数(Java 8),但是您可以对一个或多个“真实” ActionListener类执行相同的操作。 The important part is that you call label.setIcon(theImage) ; 重要的部分是您调用label.setIcon(theImage) ; this part seems to be missing in your code. 您的代码中似乎缺少此部分。


If instead you want to cycle through a list or array of pictures, you can do like this: 相反,如果您要循环浏览图片列表或数组,则可以执行以下操作:

AtomicInteger index = new AtomicInteger(0);
JButton buttonCycle = new JButton("Cycle");
buttonCycle.addActionListener(e -> label.setIcon(images[index.getAndIncrement() % images.length]));
frame.getContentPane().add(buttonCycle);

Here, the AtomicInteger is used so I can declare it as a local variable and use it in the lambda. 在这里,使用了AtomicInteger ,因此我可以将其声明为局部变量,并在lambda中使用它。 You can just as well use a regular int if you make it a member variable of the surrounding class. 如果将常规int设为周围类的成员变量,则也可以使用常规int

private int c = 0;
...
buttonCycle.addActionListener(e -> label.setIcon(images[c++ % images.length]));

The takeaway is: Create a counter variable, increment it each time the button is called and set the label's icon to the element with that count, module the size of the array. 要点是:创建一个计数器变量,每次调用按钮时将其递增,并将标签的图标设置为具有该计数的元素,对数组的大小进行模数化。

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

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