📄 tillapplet.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class TillApplet extends Applet implements ActionListener {
/* The Grocery Till applet by J M Bishop Oct 1996
* Java 1.1 version by T Abbott and J M Bishop Oct 1997
*
* Simulates the operation of a grocery till for
* up to 12 products, together with the costs per
* kilogram.
* Runs as an applet via its corresponding html file.
*/
private TextField weighField, totalField;
private Button[] itemButtons;
private Button weighButton, printButton, closeButton;
public void init () {
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new FlowLayout());
totalField = new TextField(6);
totalField.setEditable(false);
p.add(new Label("COST"));
p.add(totalField);
add("North", p);
p = new Panel();
p.setLayout(new GridLayout(5, 3));
itemButtons = new Button[items.length];
for (int i = 0; i < items.length; i++) {
itemButtons[i] = new Button(items[i]);
itemButtons[i].addActionListener(this);
p.add(itemButtons[i]);
}
weighButton = new Button("WEIGH");
weighButton.addActionListener(this);
p.add(weighButton);
printButton = new Button("PRINT");
printButton.addActionListener(this);
p.add(printButton);
add("Center", p);
p = new Panel();
p.setLayout(new FlowLayout());
weighField = new TextField(4);
weighField.setEditable(false);
weighField.addActionListener(this);
p.add(weighField);
p.add(new Label("MASS"));
p.add(new Label("Type return after the amount"));
add("South", p);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == printButton) {
printItems();
} else
if (source == weighButton) {
resetWeighField();
} else
if (source == weighField) {
readWeighField();
} else
selectItem(e.getActionCommand());
}
/* Here follows the main control of the program.
* The event and action handlers above call these
* methods, which ensure that there is only a reaction
* if certain conditions (e.g. other previous events)
* have already been met.
*/
public void resetWeighField() {
weighField.setText("");
weighField.setEditable(true);
}
public void readWeighField() {
if (weighField.isEditable()) {
weighField.setEditable(false);
weighField.selectAll();
kg = (double)Integer.valueOf(
weighField.getText().trim()).intValue();
weighed = true;
}
}
public void printItems() {
if (weighed && chosen) {
total = kg*unitCosts[select];
totalField.setText("G "+total);
System.out.println(kg+"kg "+items[select]+" @ G"+
unitCosts[select]+" = G"+total);
kg = 0;
weighed = false;
chosen = false;
total = 0;
weighField.select(0,0);
}
}
public void selectItem(String item) {
select = 0;
while (!item.equals(items[select]))
select++;
chosen = true;
}
private String[] items = { "Apples", "Pears", "Oranges",
"Potatoes", "Lemons", "Squash",
"Onions", "Garlic", "Avocados",
"", "", "" };
private double[] unitCosts = { 6.00, 5.00, 7.00,
3.00, 10.00, 4.00,
4.00, 12.00, 15.00,
0, 0, 0 };
private double total;
private double kg;
private boolean chosen = false;
private boolean weighed = false;
private int select = 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -