Domanda

So che la soluzione utilizza un ciclo a passo attraverso la matrice e la visualizzazione in un riquadro. Tuttavia non sto trovando una spiegazione semplice su questo. Ho bisogno di un successivo e un pulsante precedente che mostra ogni elemento dell'array, e solo ritorna al primo elemento, una volta raggiunta la fine quando si preme il tasto successivo.

for ( int i = 0; i < initem.length; i++ ){
          JOptionPane.showMessageDialog( null, initem[i]);
}

initem è il nome del mio array.

Incorporated ActionListerner

class RecordViewer extends JDialog
{
private JButton next;
private JButton prev;
private JLabel label = new JLabel();
private int current = 0;
private CDinventoryItem [] items;



 public RecordViewer(CDinventoryItem [] array){

super();
items = array;

label = this.setLabel(items[Current()]);
next = new JButton("Next");
next.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){
        if( getCurrent() == items.length ){
                setCurrent(0);
        }else{
                setCurrent(getCurrent() + 1);
        }
            setTitle("Inventory Item");
            setSize(1200, 300);
            setLocation(200,200);
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            getLabel().setText(items[getCurrent()].toString());
    }
});

prev = new JButton("Previous");
prev.addActionListener(new ActionListener(){

    public void actionPerformed(ActionEvent e){
        if( getCurrent() == 0){
                setCurrent(items.length - 1);
        }else{
                setCurrent(getCurrent() - 1);
        }
            setTitle("Inventory Item");
            setSize(1200, 300);
            setLocation(200,200);
            setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            getLabel().setText(items[getCurrent()].toString());
    }
});

setLayout(new FlowLayout());
add(label);
add(next);
add(prev);
pack();

this.setVisible(true);
}

public JButton getNext() {
    return next;
}

public void setNext(JButton next) {
    this.next = next;
}

public JButton getPrev() {
    return prev;
}

public void setPrev(JButton prev) {
    this.prev = prev;
}

public JLabel getLabel() {
    return label;
}


private int getCurrent() {
    return current;
}

public void setCurrent(int current) {
    this.current = current;
}

private JLabel setLabel(CDinventoryItem cDinventoryItem) {
    return label;
}

private int Current() {
    return current;

}}
È stato utile?

Soluzione

Ecco un semplice esempio che lascia fuori tutti i dettagli estranei.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;

public class RecordViewer extends JDialog {

 String items[] = {"One", "Two", "Three"};
 JButton next, prev;
 JLabel label;
 int current;

public RecordViewer(){
    super();
    current = 0;
    label = new JLabel(items[current]);
    next = new JButton("Next");
    next.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(current == items.length - 1){
                current = 0;
            }else{
                current++;
            }
            label.setText(items[current]);
        }
    });

    prev = new JButton("Previous");
    prev.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            if(current == 0){
                current = items.length - 1;
            }else{
                current--;
            }
            label.setText(items[current]);
        }
    });

    setLayout(new FlowLayout());
    add(label);
    add(next);
    add(prev);
    pack();
    this.setVisible(true);
  }
 }

Per visualizzare questa finestra di dialogo è possibile chiamare:

  new RecordViewer();

Questo esempio utilizza una matrice semplice stringa e semplicemente cicli attraverso la matrice che utilizza due pulsanti e un layout semplice. È possibile modificarlo per visualizzare l'array e utilizzare un layout più avanzato per renderla migliore.


UPDATE:

Si può provare questo:

 public class RecordViewer extends JDialog {

  JButton next, prev;
  JLabel label;
  int current;
  CDinventoryItem [] items;

  public RecordViewer(CDinventoryItem [] array)
  {
    super(); 
    current = 0; 
    items = array;
    ....

E vedere se questo aiuta.

Altri suggerimenti

Il ciclo sta per visualizzare il riquadro una volta per ogni elemento dell'array e che non è ciò che si desidera.

Sembra che si sta tentando di ottenere un valore selezionato. In tal caso, utilizzare un InputDialog invece di dialogo messaggio e passare l'intero array.

Ecco un esempio dalla API:

Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top