/** * @(#)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就又要被降级了。。。。
----------------解决方案--------------------------------------------------------
第一个问题就如你说的,需要改动clear。因为要把原来画的地方清楚,第二个的思路也一样,要清空那里画的东西
----------------解决方案--------------------------------------------------------
// MailOrder.java
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};
private float total = 0f;
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 = newLabel(" Welcome to Mail Order");
L4 = newLabel(" Select product number (1 to 4) to view and");
L5 = newLabel(" 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(null);
T2.setText(null);
historyMap.clear();
total = 0f;
}
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;
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);
}
}
}
//MailOrder
<html>
<head>
</head>
<body>
<applet
code = "MailOrder.class"
width = "500"
height = "700"
>
</applet>
</body>
</html>
----------------解决方案--------------------------------------------------------
很谢谢楼上啊,但是我想问
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); // 这里的 [i-1] ,是什么意思呢???
g.drawString(" " + i +" " + value +" " + "$" + price[i-1] * Integer.parseInt(value), 50, cnt * 20 + 310); / / 这里的 price[i-1] * Integer.parseInt(value); 是什么意思呢????
total = total + price[i-1] * Integer.parseInt(value); / / 这里的 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); / /这里的(290,420,390,440) 都代表什么呢???这些数值与下面那行有什么关系吗????
g.drawString("Total cost:" + "$ :" + String.valueOf(total), 290, 440);
}
}
}
[此贴子已经被作者于2005-8-15 5:51:59编辑过]
----------------解决方案--------------------------------------------------------
谁能帮我解释一下那些代码的意思
Thanks a lot !
----------------解决方案--------------------------------------------------------
在这个For-Loop中i 是从1开始的,再来看你在private 中的定义 private Image I[] = newImage[4]; 也就是说你定义了一个Image 类型的数组,并为其开设了4个空间,这样这个数组就可以接受4个Image。
在init() 函数中 你给这4个Image 存储空间赋予了值,
I[0] = getImage(getDocumentBase(), "I1.jpg");
I[1] = getImage(getDocumentBase(), "I2.jpg");
I[2] = getImage(getDocumentBase(), "I3.jpg");
I[3] = getImage(getDocumentBase(), "I4.jpg");
这样你就可以使用这些Image 了.
现在我们从这个For循环来看, i 从1 开始, 而那个Image数组的起始Index 是0 所以 要i-1 啊 g.drawImage(I[i-1], 30, 440, this);
value = (String) historyMap.get(String.valueOf(i)); // 这条语句的意思是从 historyMap 中取值, 并将其强制转换为String类型,
if(value != null) // 对刚才所得到的那个value作判断,如果在那个HashMap中确实已存储了那个变量,那么应该同过那个get() 函数提取出来,也就是说value 不等于 null, 否则的话 value 等于 null
如果value 不等于 null, 那么进入那个 if 语句
否则的话,什么也不做。
price[i-1] * Integer.parseInt(value); 为什么i-1上面已经解释过了,再给你解释一遍,当i =1, i-1 = 0, 数组的起始index 是0, value 这个变量是String 类型,所以我们要将他转换为 integer 类型,这样才可以进行计算啊。
比如 value = "1";
那么 Integer.parseInt(value) 就将 "1" 转换为整数 1 而 price[0] = 2.98f 所以 2.98*1 = 2.98
g.clearRect(290,420,390,440); 意味着将某一Rectangle 区域内的内容清除掉,那么你必须清楚Rectangle 是怎么定义的, Rectangle 的定义为 (x, y, width,height) 这里x, y 为起始左上方的那个点。
还有什么不明白的再问
----------------解决方案--------------------------------------------------------
ha ...
I understand already
U are really good at JAVA
thank you so much .
----------------解决方案--------------------------------------------------------