4
現在我正在學習我的程序設計文憑,我們有一個創建計算器來計算總計,平均值,計數器,輸入的最大值和最小值,撤消按鈕和清除按鈕。我已經完成了所有工作,包括撤銷按鈕,但是我的講師通過在撤消方法中添加了一個堆棧函數來設置額外的信用機會。我已經研究過如何推送對象,但我不確定如何彈出對象並將請求的對象添加到我的GUI中。我明白這不是完全必要的,但我想在我的餘生中這樣做,所以我真的很努力。如果有人能夠幫助我,我的源文件如下。Java Stack into 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("");
}
}
}
}
所有包圍在/ * * /中的語法都是原始的undo方法。
糟糕,我有點糊塗.push()爲.add()哈哈。 但我怎麼實際上掃描它?就像掃描儀一樣? (while(scan.hasNext())? – 2012-02-13 14:13:19
Moron?No!它只是通過練習的經驗,繼續前進 – 2012-02-13 14:14:13
我如何掃描出來?我有這樣的想法,就像使用文件掃描儀,但我 – 2012-02-13 14:19:43