/** * @(#)MailOrder.java * * Sample Applet application * * @author * @version 1.00 05/08/14 */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap;
public class MailOrder extends Applet implements ActionListener { Label L1, L2, L3, L4, L5; TextField T1, T2; Button B1, B2; Panel P1, P2, P3; int P, Q, M1, M2, M3, M4; private Image I[] = new Image[4]; private HashMap historyMap = null; private static final float[] price = {2.98f,0.98f,1.98f,9.98f};
public void init() { setSize(400, 540);
I[0] = getImage(getDocumentBase(), "I1.jpg"); I[1] = getImage(getDocumentBase(), "I2.jpg"); I[2] = getImage(getDocumentBase(), "I3.jpg"); I[3] = getImage(getDocumentBase(), "I4.jpg");
L1 = new Label("Product number "); L2 = new Label("Quantities "); L3 = new Label(" Welcome to Mail Order"); L4 = new Label(" Select product number (1 to 4) to view and"); L5 = new Label(" enter quantities to purchase");
T1 = new TextField(20); T1.setText(""); T2 = new TextField(20); T2.setText("");
P1 = new Panel(); P2 = new Panel(); P3 = new Panel();
B1 = new Button("Add"); B1.addActionListener(this); B2 = new Button("Cancel"); B2.addActionListener(this);
P1.setLayout(new GridLayout(5, 5, 3, 3)); P1.add(L1); P1.add(T1); P1.add(L2); P1.add(T2); P1.add(B1); P1.add(B2); P3.setLayout(new BorderLayout(5, 5)); P3.add("North",L3); P3.add("Center",L4); P3.add("South",L5); P2.setLayout(new BorderLayout(10, 10)); P2.add("North",P3); P2.add("South",P1); add(P2);
historyMap = new HashMap();
}
public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Cancel")) { T1.setText(""); T2.setText(""); } else { if (T1.getText().matches("^[0-9]+.[0-9]+$|^[0-9]$")) { P = Integer.parseInt(T1.getText()); } else { P = 0; } if (T2.getText().matches("^[0-9]+.[0-9]+$|^[0-9]$")) { Q = Integer.parseInt(T2.getText()); } else { Q = 0; } if(P != 0 && Q != 0){ historyMap.put(String.valueOf(P),String.valueOf(Q)); } } repaint(); }
public void paint(Graphics g1) {
int i; String value = null; int cnt = 0; float total = 0f; Graphics2D g = (Graphics2D) g1; g.drawString(" List if items to purchase", 60, 270); g.drawString("Product# Quantities Price", 50, 290); for (i = 1; i <= 4; i++) { value = (String) historyMap.get(String.valueOf(i)); if(value != null){ cnt++; g.drawImage(I[i-1], 30, 440, this); g.drawString(" " + i + " " + value + " " + "$" + price[i-1] * Integer.parseInt(value), 50, cnt * 20 + 310); total = total + price[i-1] * Integer.parseInt(value); //g.drawString("product# 1- Straebreey", 300, 450); //g.drawString("price $2.98", 300, 470);
} g.clearRect(290,420,390,440); g.drawString("Total cost:" + "$ :" + String.valueOf(total), 290, 440);
} } }
这是我的程序,有2个问题,第一个是 g.clearRect(290,420,390,440); g.drawString2("Total cost:" + "$ :" + String.valueOf(total), 290, 440); 这2行,我想吧("Total cost:" + "$ :" + String.valueOf(total), 的位置改了,但是只改动290, 440,后,每按一次ADD后,现在的输出就会覆盖住原来的总钱数的输出,我想问是不是还要改g.clearRect(290,420,390,440); 这里的什么东西呢?
还有1个就是,CANCEL的按扭不起作用,只能清空TEXTFIELD,我想按了CANCEL后,能把除了 图片 和 TOTAL COST : 以外的输出都清空,然后在原来的输出位置,只输出“CANCELED”。 请大家多帮帮我啊,如果这个再做不好,我的JAVA就又要被降级了。。。。