java - JButton 需要显示图像数组

标签 java eclipse swing jbutton

我有一组图像存储在一个数组中,我需要像幻灯片放映一样显示它们。有两个 JButtons next 和 previous 允许用户查看图像。但是我无法让按钮工作。有什么建议吗?

谢谢

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class slides extends JPanel implements ActionListener {

    // Data Field
    private final ImageIcon imageArray[];
    private ImageIcon image;

    JButton nextButton;
    JButton prevButton;

    int page = 0;
    int nextPage = page + 1;
    int prevPage = page - 1;
    int numOfSlides = 28;


    // Obtains and stores slides in imageArray
    public slides() {
        imageArray = new ImageIcon[numOfSlides];
        for (int i = 0; i < imageArray.length; i++) {
            imageArray[i] = new ImageIcon("Slide " + (i + 1) + ".png");
        }
    }

    // Displays slides
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        boolean began = true;

        // creates next button
        nextButton = new JButton("next");
        nextButton.setBounds(400, 574, 70, 30);
        nextButton.addActionListener(this);
        add(nextButton);

        // creates previous button
        prevButton = new JButton("previous");
        prevButton.setBounds(0, 574, 85, 30);
        prevButton.addActionListener(this);
        add(prevButton);

        // displays slide 1
        if (began == true) {
            image = imageArray[page];
            image.paintIcon(this, g, 0, 0);
            began = false;
        }

        // displays other slides based on preference
        if (page == nextPage) {
            image = imageArray[page + 1];
            image.paintIcon(this, g, 0, 0);
            nextPage = page + 1;
            prevPage = page - 1;
        }
        if (page == prevPage) {
            nextPage = page + 1;
            prevPage = page - 1;
            image = imageArray[page - 1];
            image.paintIcon(this, g, 0, 0);
        }

        // Removes buttons accordingly
        if(page == imageArray.length - 1) {
            remove(nextButton);
        }
        if(page == 0){
            remove(prevButton);
        }
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == nextButton) {
            page++;
            image = imageArray[page];
        }
        if (event.getSource() == prevButton) {
            page--;
            image = imageArray[page];
        }
    }
}

最佳答案

Any suggestion?

  • 无需重写paintComponent();相反,在现有的 JLabel 上使用 setIcon()。引用了一个完整的例子here .

  • 不要使用setBounds();相反,pack() 封闭的 Window 让组件采用它们的首选大小。

image
(来源:drjohnbmatthews at sites.google.com)

关于java - JButton 需要显示图像数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27292117/

相关文章:

Java 包组织

java - 如何确定 .text 文件中整数列表的最小值和最大值?

java - 从word转换的pdf中读取复选框值

Java-Swing : A problem using layout managers!

java - 包含图像的小程序在浏览器中运行时不会显示它们

Java全屏独占模式

ReplaceAll 正则表达式引起的 Java OutOfMemoryError

java - Android Eclipse 开发中实现多个图像按钮时出错

java - 在扩展类中强制使用某个函数,可能吗?

eclipse - AVR - 无法在 Eclipse 上添加程序员