正在将数组读取到ui堆栈上的文本字段中



我的问题是我正在将整个数组读取到textField中。我需要把它们分开。目标是显示堆栈在运动中的ui。如何将它们分隔为单独的文本字段。有问题

面板如下:

public class StackPanel extends javax.swing.JPanel {
Random rnd = new Random();
MyStack stack = new MyStack();
private int top;
public StackPanel() {
    initComponents();
    clearLabels();
}
private void clearLabels() {
    label1.setText("");
    label2.setText("");
    label3.setText("");
    label4.setText("");
    label5.setText("");
}
private void setTopTextField(String topValue) {
    topValue = stack.toString();
    if (top == 0) {
        label1.setText(topValue);
    } else if (top == 1) {
        label2.setText(topValue);
    } else if (top == 2) {
        label3.setText(topValue);
    } else if (top == 3) {
        label4.setText(topValue);
    } else if (top == 4) {
        label5.setText(topValue);
    }
}
/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
                    private void initComponents() {
    jTextField6 = new javax.swing.JTextField();
    pushButton = new javax.swing.JButton();
    popButton = new javax.swing.JButton();
    label5 = new javax.swing.JTextField();
    label4 = new javax.swing.JTextField();
    label3 = new javax.swing.JTextField();
    label2 = new javax.swing.JTextField();
    label1 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jTextField6.setText("jTextField6");
    pushButton.setText("Push");
    pushButton.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            pushButtonMouseClicked(evt);
        }
    });
    popButton.setText("Pop");
    popButton.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            popButtonMouseClicked(evt);
        }
    });
    jLabel1.setText(" top");
    jLabel2.setText("top");
    jLabel3.setText("top");
    jLabel4.setText("top");
    jLabel5.setText("top");
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(87, 87, 87)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3)
                        .addComponent(jLabel4)
                        .addComponent(jLabel5))
                    .addGap(10, 10, 10)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(label1, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE)
                        .addComponent(label2)
                        .addComponent(label3))
                    .addGap(66, 66, 66)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(popButton)
                        .addComponent(pushButton))
                    .addGap(53, 53, 53))
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(label5, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
                        .addComponent(label4))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(93, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(label5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(29, 29, 29)
                    .addComponent(pushButton)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(popButton)
                    .addContainerGap())
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel5))
                    .addGap(43, 43, 43))))
    );
}// </editor-fold>   
private void pushButtonMouseClicked(java.awt.event.MouseEvent evt) {                                        
    stack.push(rnd.nextInt(99) + 1);
    String topValue = String.valueOf(rnd.nextInt());
    setTopTextField(topValue);
    top++;
    System.out.println(stack);
}                                       
private void popButtonMouseClicked(java.awt.event.MouseEvent evt) {                                       
    stack.pop();
    String topValue = String.valueOf(rnd.nextInt());
    setTopTextField(topValue);
    top--;
    System.out.println(stack);
    }                                      

// Variables declaration - do not modify                     
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JTextField jTextField6;
private javax.swing.JTextField label1;
private javax.swing.JTextField label2;
private javax.swing.JTextField label3;
private javax.swing.JTextField label4;
private javax.swing.JTextField label5;
private javax.swing.JButton popButton;
private javax.swing.JButton pushButton;
// End of variables declaration                   
}

这里是堆栈类:

public class MyStack {
private int[] values;
private int numberOfValues;
 public MyStack(){
  values= new int[5];
  numberOfValues = 0;
 }
public boolean isEmpty() {
    return numberOfValues == 0;
 }
public int peek(){
return values[values.length - numberOfValues];
 }
public void push(int value){
     if (numberOfValues <= 5) {
        values[numberOfValues] = value;
        numberOfValues++;
    }
   }
   public void pop(){
    for (int i = 1; i <= numberOfValues - 1; i++) {
        values[i - 1] = values[i];
    }
    numberOfValues--;
   }
  @Override
  public String toString() {
    String str = "";
    for (int i = 0; i <= numberOfValues - 1; i++) {
        str += String.valueOf(values[i]) + " ";
    }
   // str += "]";
    return str;
}
}

不是很好,但实际工作:

StackPanel:

import java.util.Random;
public class StackPanel extends javax.swing.JPanel {
    Random rnd = new Random();
    MyStack stack = new MyStack();
    private int top;
    public StackPanel() {
        initComponents();
        clearLabels();
    }
    private void clearLabels() {
        label1.setText("");
        label2.setText("");
        label3.setText("");
        label4.setText("");
        label5.setText("");
    }
    private void setTopTextField(String topValue) {
        if (top == 0) {
            label1.setText(topValue);
        } else if (top == 1) {
            label2.setText(topValue);
        } else if (top == 2) {
            label3.setText(topValue);
        } else if (top == 3) {
            label4.setText(topValue);
        } else if (top == 4) {
            label5.setText(topValue);
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
        jTextField6 = new javax.swing.JTextField();
        pushButton = new javax.swing.JButton();
        popButton = new javax.swing.JButton();
        label5 = new javax.swing.JTextField();
        label4 = new javax.swing.JTextField();
        label3 = new javax.swing.JTextField();
        label2 = new javax.swing.JTextField();
        label1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextField6.setText("jTextField6");
        pushButton.setText("Push");
        pushButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pushButtonMouseClicked(evt);
            }
        });
        popButton.setText("Pop");
        popButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                popButtonMouseClicked(evt);
            }
        });
        jLabel1.setText(" top");
        jLabel2.setText("top");
        jLabel3.setText("top");
        jLabel4.setText("top");
        jLabel5.setText("top");
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addGap(87, 87, 87)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(
                                                                                        jLabel3)
                                                                                .addComponent(
                                                                                        jLabel4)
                                                                                .addComponent(
                                                                                        jLabel5))
                                                                .addGap(10, 10,
                                                                        10)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(
                                                                                        label1,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        88,
                                                                                        Short.MAX_VALUE)
                                                                                .addComponent(
                                                                                        label2)
                                                                                .addComponent(
                                                                                        label3))
                                                                .addGap(66, 66,
                                                                        66)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                .addComponent(
                                                                                        popButton)
                                                                                .addComponent(
                                                                                        pushButton))
                                                                .addGap(53, 53,
                                                                        53))
                                                .addGroup(
                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                        layout.createSequentialGroup()
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                                .addComponent(
                                                                                        jLabel2,
                                                                                        javax.swing.GroupLayout.Alignment.TRAILING)
                                                                                .addComponent(
                                                                                        jLabel1))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                                                false)
                                                                                .addComponent(
                                                                                        label5,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        103,
                                                                                        Short.MAX_VALUE)
                                                                                .addComponent(
                                                                                        label4))
                                                                .addContainerGap(
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        Short.MAX_VALUE)))));
        layout.setVerticalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        layout.createSequentialGroup()
                                .addContainerGap(93, Short.MAX_VALUE)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel1)
                                                .addComponent(
                                                        label5,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(
                                                        label4,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(jLabel2))
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addGap(29, 29,
                                                                        29)
                                                                .addComponent(
                                                                        pushButton)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(
                                                                        popButton)
                                                                .addContainerGap())
                                                .addGroup(
                                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                                        layout.createSequentialGroup()
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                .addComponent(
                                                                                        label3,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                .addComponent(
                                                                                        jLabel3))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                .addComponent(
                                                                                        label2,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                .addComponent(
                                                                                        jLabel4))
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addGroup(
                                                                        layout.createParallelGroup(
                                                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                                                .addComponent(
                                                                                        label1,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                                .addComponent(
                                                                                        jLabel5))
                                                                .addGap(43, 43,
                                                                        43)))));
    }// </editor-fold>
    private void pushButtonMouseClicked(java.awt.event.MouseEvent evt) {
        stack.push(rnd.nextInt(99));
        setTopTextField(new Integer(stack.peek()).toString());
        top++;
        System.out.println(stack);
    }
    private void popButtonMouseClicked(java.awt.event.MouseEvent evt) {
        stack.pop();
        top--;
        setTopTextField("");
        System.out.println(stack);
    }
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField label1;
    private javax.swing.JTextField label2;
    private javax.swing.JTextField label3;
    private javax.swing.JTextField label4;
    private javax.swing.JTextField label5;
    private javax.swing.JButton popButton;
    private javax.swing.JButton pushButton;
    // End of variables declaration
}

MyStack:

public class MyStack {
    private int[] values;
    private int numberOfValues;
    public MyStack() {
        values = new int[5];
        numberOfValues = 0;
    }
    public boolean isEmpty() {
        return numberOfValues == 0;
    }
    public int peek() {
        return values[numberOfValues - 1];
    }
    public void push(int value) {
        if (numberOfValues <= 5) {
            values[numberOfValues] = value;
            numberOfValues++;
        }
    }
    public void pop() {
        numberOfValues--;
    }
    @Override
    public String toString() {
        String str = "";
        for (int i = 0; i <= numberOfValues - 1; i++) {
            str += String.valueOf(values[i]) + " ";
        }
        // str += "]";
        return str;
    }
}

相关内容

  • 没有找到相关文章

最新更新