简体   繁体   English

Java JOptionPane逐步遍历数组

[英]Java JOptionPane stepping through Array

I know the solution is using a for loop to step through the array and display in a pane. 我知道解决方案是使用for循环遍历数组并在窗格中显示。 However I am not finding an straight forward explanations on this. 但是,我对此没有找到直接的解释。 I need a next and a previous button that displays each array element, and just returns to the first element once it reaches the end when the next button is pressed. 我需要一个下一个和上一个按钮,用于显示每个数组元素,并且在按下下一个按钮时,它会在到达末尾时返回到第一个元素。

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

initem is the name of my array. initem是我的数组的名称。

Incorporated ActionListerner 合并的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;

}}

Your loop is going to display the pane once per item in the array and that is not what you want. 您的循环将在数组中的每个项目上显示一次窗格,这不是您想要的。

It sounds like you are trying to get a selected value. 听起来您正在尝试获取选定的值。 In that case use a inputDialog instead of message Dialog and pass the entire array. 在这种情况下,请使用inputDialog而不是消息Dialog并传递整个数组。

Here is an example from the API: 这是API的示例:

Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);

Here is a simple example that leaves out all the extraneous details. 这是一个简单的示例,省略了所有无关的细节。

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

To view this dialog you can call: 要查看此对话框,您可以调用:

  new RecordViewer();

This example uses a simple String array and simply cycles through the array using two buttons and a simple layout. 本示例使用一个简单的String数组,并使用两个按钮和一个简单的布局在整个数组中循环。 You can modify it to display your array and use a more advanced layout to make it look better. 您可以对其进行修改以显示您的数组,并使用更高级的布局使其看起来更好。


UPDATE: 更新:

You can try this: 您可以尝试以下方法:

 public class RecordViewer extends JDialog {

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

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

And see if that helps. 看看是否有帮助。

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

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