Java Stack进入GUI

时间:2012-02-13 13:40:18

标签: java swing user-interface stack

现在我正在学习编程文凭课程,我们有一个设定计算总计,平均值,计数器,输入的最大值和最小值,撤消按钮和清除按钮的计算任务。我已经完成了所有工作,包括撤销按钮,但我的讲师通过在撤消方法中添加堆栈功能来设置额外的信用机会。我已经研究了如何推送对象,但我不确定如何弹出对象并将请求的对象添加到我的GUI中。我知道这不是完全必要的,但我想在余生中这样做,所以我真的很努力。如果有人能够帮助我,我的源文件在下面。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.util.*;
import java.io.*;

public class DataPanel extends JPanel {
    ArrayList<Data> dataStore = new ArrayList<Data>();
    Stack dataStack = new Stack(); 

    private double _number, _oldNumber;
    private double _total, _oldTotal;
    private int _count, _oldCount;
    private double _average, _oldAverage;
    private double _min, _oldMin;
    private double _max, _oldMax;

    private JTextField _inField = new JTextField();
    private JTextField _totalField = new JTextField(); 
    private JTextField _averageField = new JTextField(); 
    private JTextField _minField = new JTextField(); 
    private JTextField _maxField = new JTextField(); 
    private JTextField _countField = new JTextField(); 

    public DataPanel() {
        setLayout(new GridLayout(6, 2, 1, 1));
        add(new JLabel("Number"));
        add(_inField);
        _inField.addActionListener(new InFieldListener());

        add(new JLabel("Total"));
        add(_totalField);   
        _totalField.setEditable(false);

        add(new JLabel("Count"));
        add(_countField);
        _countField.setEditable(false);

        add(new JLabel("Average"));
        add(_averageField);
        _averageField.setEditable(false);

        add(new JLabel("Maximum"));
        add(_maxField);
        _maxField.setEditable(false);

        add(new JLabel("Minimum"));
        add(_minField);
        _minField.setEditable(false);
    }

    public void clear() {
      _total = 0;
      _count = 0;
      _average = 0;
      _max = 0;
      _min = 0;

        _inField.setText("");
        _totalField.setText("");
        _countField.setText("");
        _averageField.setText("");
        _maxField.setText("");
        _minField.setText("");
    }

    public void undo() {
       /*_number = _oldNumber;
        _total = _oldTotal;
        _count = _oldCount;
        _average = _oldAverage;
        _max = _oldMax;
        _min = _oldMin;

        _inField.setText(String.valueOf(_number));
        _totalField.setText(String.valueOf(_total));
        _countField.setText(String.valueOf(_count));
        _averageField.setText(String.valueOf(_average));
        _maxField.setText(String.valueOf(_max));
        _minField.setText(String.valueOf(_min));*/

        /*int size = dataStore.size();
        dataStore.remove(size-1);*/
    }

    private class Data {
        private Data(double _number, double _total, int _count, double _average, double _max, double _min) {
          _number = _number;
          _total = _total;
          _count = _count;
          _average = _average;
          _max = _max;
          _min = _min;
       }
   }


    private class InFieldListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if(_inField.getText().equals("exit")) {
                System.exit(0);
            }       
            try {
                  _number = Double.parseDouble(_inField.getText());
                  _inField.setText("");

                /*_oldNumber = _number;
                _oldTotal = _total;
                _oldAverage = _average;
                _oldCount = _count;
                _oldMax = _max;
                _oldMin = _min;*/

                  _count++;

              if(_count == 1) {
                _min = _number;
                _max = _number;
              } else if(_number >= _max) {
                _max = _number;
              } else if(_number <= _min) {
                _min = _number;
              }

                  _total += _number;
                  _average = _total/_count;

              _totalField.setText(String.valueOf(_total));
             _countField.setText(String.valueOf(_count));
             _averageField.setText(String.valueOf(_average));
             _maxField.setText(String.valueOf(_max));
             _minField.setText(String.valueOf(_min));

             //dataStore.add(new Data(_number, _total, _count, _average, _max, _min));
             dataStack.add(new Data(_number, _total, _count, _average, _max, _min));

         } catch(NumberFormatException nfe) {
           System.out.println("There was a problem: " + nfe);
           _inField.setText("");
         }
        }
    }
}

/ * * /包围的所有语法都是原始的撤消方法。

1 个答案:

答案 0 :(得分:3)

您的代码:

dataStack.add(new Data(_number, _total, _count, _average, _max, _min));

应该是push()而不是add()

然后在undo()中,您可以调用dataStack.pop()并将您从中获得的对象转换为Data。 (替代方案:使用泛型)

然后你取出每个Data的成员来恢复实例变量_number,依此类推。

编辑:&gt;我该怎么扫出来?

对于每次单击撤消按钮,您只需要恢复(= pop())一个数据对象。然后,您可以询问它的变量,并使用这些变量恢复DataPanel的变量。例如:

Data d = (Data)dataStack.pop();
_number = d._number;
...