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

Java 额外单击可在Jframe中的页面之间导航,java,swing,Java,Swing,这是我用来解码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;

这是我用来解码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");
            }
        }

    }

}
package decoding.tiff;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.image.buffereImage;
导入java.awt.image.DataBufferInt;
导入java.io.RandomAccessFile;
导入java.util.ArrayList;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JOptionPane;
导入javax.swing.JScrollPane;
公共类TiffMultipage24位扩展javax.swing.JFrame实现
ActionListener{
私有静态最终长serialVersionUID=-4935096415846083312;
私有javax.swing.JButton jButton1;
私有javax.swing.JButton jButton2;
私有javax.swing.JButton jLabel1;
JScrollPane日志滚动窗格;
静态ArrayList图像=新建ArrayList();
静态整数计数=0;
静态int minvalue=-1;
静态整数totalimages=0;
公共TIFF多页24位(){
初始化组件();
jButton1.addActionListener(这个);
jButton2.addActionListener(这个);
}
私有组件(){
//帧视图代码
}
@SuppressWarnings({“资源”,“未使用的”})
公共静态void main(字符串args[])抛出可丢弃的{
{
//图像解码代码
images.add(buff);//将图像添加到数组列表
}
totalimages=images.size();
TiffMultiPage24Bit mp=新的TiffMultiPage24Bit();
invokeLater(new Runnable()){
公开募捐{
新的TiffMultiPage24Bit().setVisible(true);
}
});
}
已执行的公共无效操作(操作事件e){
如果(例如getSource()==jButton1){
计数--;
如果(计数>最小值){
jLabel1.setIcon(新的ImageIcon(images.get(count));
}否则{
showMessageDialog(null,“没有以前的图像”);
}
}
如果(例如getSource()==jButton2){
计数++;
如果(计数<总图像){
jLabel1.setIcon(新的ImageIcon(images.get(count));
}否则{
showMessageDialog(null,“无下一个图像”);
}
}
}
}
当我第一次单击“下一步”时,它工作正常

但一旦它进入最后一页,就需要点击两下才能返回上一页。。。。 当它进入第一页时,需要点击两下才能进入下一页


请帮忙。。。。任何帮助都将不胜感激……

当计数无效时,您需要将计数恢复到允许的最大值

像这样想

  • 单击“下一步”
  • 计数
    递增
  • count
    =
    totalimages
    ,显示错误消息(
    count
    现在至少等于
    totalimages
  • 单击上一步
  • count
    递减,现在等于
    totalimages-1
    ,这是最后一个(也是当前)图像
每次
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;
        //...
    }
}
if(例如getSource()==jButton1){
计数--;
如果(计数>最小值){
//...
}否则{
计数=最小值;
//...
}
}else if(例如getSource()==jButton2){
计数++;
如果(计数<总图像){
//...
}否则{
计数=totalimages-1;
//...
}
}
例如


最棒的是,按照您现在的方式,我可以一直单击“下一步”并不断增加
计数
值……甚至在
计数
达到上限或下限时禁用按钮也可能是值得的……

或者……我在代码中这样更改了它

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)));
        } 
    }
}
public void actionPerformed(ActionEvent e){
如果(例如getSource()==jButton1){
计数--;
if(count==minvalue | | count minvalue&&counttotalimages)
{
计数=totalimages-1;
showMessageDialog(null,“无下一个图像”);
}
if(countminvalue){
jLabel1.setIcon(新的ImageIcon(images.get(count));
} 
}
}