简体   繁体   中英

Extra click to navigate between pages in Jframe

This is the code i have made for decoding a 24-Bit tiff file....

package decoding.tiff;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.RandomAccessFile;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class TiffMultiPage24Bit extends javax.swing.JFrame implements
        ActionListener {

    private static final long serialVersionUID = -4935096415846083312L;

    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jLabel1;
    JScrollPane logScrollPane;

    static ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
    static int count = 0;
    static int minvalue = -1;
    static int totalimages = 0;

    public TiffMultiPage24Bit() {
        initComponents();

        jButton1.addActionListener(this);
        jButton2.addActionListener(this);

    }

    private void initComponents() {
//Code for frame view
    }

    @SuppressWarnings({ "resource", "unused" })
    public static void main(String args[]) throws Throwable {


        {

        //Code for image decoding

            images.add(buff); // adding the image to array list

        }

        totalimages = images.size();

        TiffMultiPage24Bit mp = new TiffMultiPage24Bit();

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TiffMultiPage24Bit().setVisible(true);
            }
        });

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton1) {
            count--;
            if (count > minvalue) {
                jLabel1.setIcon(new ImageIcon(images.get(count)));
            } else {
                JOptionPane.showMessageDialog(null, "No Previous Image");
            }
        }

        if (e.getSource() == jButton2) {
            count++;
            if (count < totalimages) {
                jLabel1.setIcon(new ImageIcon(images.get(count)));
            } else {
                JOptionPane.showMessageDialog(null, "No Next Image");
            }
        }

    }

}

When I click next for the first time instance it works fine....

But after once it goes to last page then it takes two clicks to return to previous page.... And once when it goes to 1st page then it takes two clicks to go to next page....

please help.... any help will be appreciated...

You need to rest the count back to the maximum allowable value when it is invalid

Think about it like this...

  • You click next
  • count is incremented
  • count >= totalimages , show error message ( count is now equal to (at least) totalimages
  • Click previous
  • count is decremented and is now equal to totalimages - 1 , which is the last (and current) image...

Each time count is invalid, you need to reset it back to it's valid range...

if (e.getSource() == jButton1) {
    count--;
    if (count > minvalue) {
        //...
    } else {
        count = minvalue;
        //...
    }
} else if (e.getSource() == jButton2) {
    count++;
    if (count < totalimages) {
        //...
    } else {
        count = totalimages - 1;
        //...
    }
}

As an example

The great thing is, the way you have it right now, I could keep clicking "next" and keep increment the count value...It might even be worth while to disable the buttons when count reaches the upper or lower limit...

Alternatively... i changed it like this in my code.....

public void actionPerformed(ActionEvent e) {

    if (e.getSource() == jButton1) {
        count--;
        if(count==minvalue || count<minvalue)
        {
            JOptionPane.showMessageDialog(null, "No Previous Image");
            count=minvalue+1;
        }
        if (count > minvalue && count < totalimages) {
            jLabel1.setIcon(new ImageIcon(images.get(count)));
        } 
    }

    if (e.getSource() == jButton2) {
        count++;
        if(count==totalimages || count >totalimages)
        {
            count=totalimages-1;

            JOptionPane.showMessageDialog(null, "No Next Image");
        }
        if (count < totalimages && count > minvalue) {
            jLabel1.setIcon(new ImageIcon(images.get(count)));
        } 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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