额外的点击在JFrame中

这是我用于解码24位TIFF文件所做的代码页之间的导航....额外的点击在JFrame中

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

}

}

}

}

当我点击旁边的第一时刻,它工作正常。 ...

但一旦它走到最后一页,然后它需要两次点击才能返回到上一页.... 一旦它进入第一页,然后它需要两次点击才能转到下一页.. ..

请帮助....任何帮助将不胜感激...

回答:

你需要休息的count回最大允许值时,它是无效的

想想看这样的...

  • 单击下一步
  • count递增
  • 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;

//...

}

}

举个例子

伟大的事情是,你拥有了它现在的样子,我可以继续点击“下一步”,并保持递增count值...它甚至可能是值得的禁用按钮时count达到上限或下限...

回答:

或者......我改变它像这样在我的代码.....

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

}

}

}

以上是 额外的点击在JFrame中 的全部内容, 来源链接: utcz.com/qa/257228.html

回到顶部