简体   繁体   English

额外单击即可在Jframe中的页面之间导航

[英]Extra click to navigate between pages in Jframe

This is the code i have made for decoding a 24-Bit tiff file.... 这是我为解码24位tiff文件而编写的代码。

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.... 当我第一次单击“ next”时,它工作正常。

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 count无效时,您需要将count恢复为最大允许值

Think about it like this... 这样想吧...

  • You click next 您单击下一步
  • count is incremented count增加
  • count >= totalimages , show error message ( count is now equal to (at least) totalimages count > = totalimages ,显示错误消息( count现在等于(至少) totalimages
  • Click previous 点击上一个
  • count is decremented and is now equal to totalimages - 1 , which is the last (and current) image... count递减,现在等于totalimages - 1 ,这是最后一个(和当前的)图像...

Each time count is invalid, you need to reset it back to it's valid range... 每次count无效时,您需要将其重置回有效范围...

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... 很棒的是,您现在拥有的方式是,我可以继续单击“下一步”并继续增加count数值...当count达到上限或下限时,甚至可能值得禁用按钮...

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)));
        } 
    }
}

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

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