-
Buttons
hi , i am currently writing a program called mobileShop. it has five buttons. the idea is that when the applet starts, it displays 5 phone models and there prices.
the problem that i am having is that when i click one of the buttons the information doesnt change. for example, one of the buttons is called R2D2 so when this is clicked it is supposed to display, the model name, price, standby time, talktime and bluetooth.
it should be displayed like this:
ModelName :
Price:
Standby Time (days):
Talk Time (hours)
Bluetooth :
the bluetooth variable should be boolean .
would i need to make an array for each button or could i simply use the g.drawString command ?
here is the code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class MobileShop extends Applet
implements ActionListener {
public Button PriceList, R2D2, C3PO, UB40, WD40, RU18;
public void init() {
PriceList = new Button ("Price List");
add (PriceList);
PriceList.addActionListener(this);
R2D2 = new Button ("R2D2");
add (R2D2);
R2D2.addActionListener(this);
C3PO = new Button ("C3PO");
add (C3PO);
C3PO.addActionListener(this);
UB40 = new Button ("UB40");
add (UB40);
UB40.addActionListener(this);
WD40 = new Button ("WD40");
add (WD40);
WD40.addActionListener(this);
RU18 = new Button ("RU18");
add (RU18);
RU18.addActionListener(this);
}
public void paint (Graphics g) {
g.drawString("R2D2 £79.95", 200,100);
g.drawString("C3PO £99.95", 200,120);
g.drawString("UB40 £129.95", 200,140);
g.drawString("WD40 £189.95", 200,160);
g.drawString("RU18 £219.95", 200,180);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == PriceList)
if (event.getSource() == R2D2)
if (event.getSource() == C3PO)
if (event.getSource() == UB40)
if (event.getSource() == WD40)
if (event.getSource() == RU18)
repaint();
}
-
Just make a few variables that contain the data and update the variables. Or make a PhoneModel class.
Code:
"ModelName : "+model
"Price: "+price
"Standby Time (days): "+sbTime
"Talk Time (hours) "+tTime
"Bluetooth : "+hasBluetooth
if (event.getSource() == R2D2)
{
model = "R2D2";
price = "$200";
sbTime = "36";
tTime = "6";
hasBluetooth = "yes";
}
-
hi, thank you for your help. i have tried putting the code in that you posted but the program wont compile. when you said make a phonemodel class, what did u mean??
I am sorry if these questions sound stupid but i am very new to java.
-
Here is a suggestion for code. There is one big drawback however; If you override the Applets paint method and at the same time wants to add components to this applet (panel) it can get messy (im really not sure how this applet would look like at runtime).
The easiest is to set the applet to a borderlayout, add a small button panel in south and and make your own Panel subclass for adding in the applets center. In this subclass you can then override the paint method w.out getting in the way of the applets button drawing.
Code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/**
* A button and mobilephone hybrid.....
*/
class MButton extends Button {
private int price=0;
private int talkTime=0;
private int standbyTime=0;
private boolean blooeTooth=false;
public MButton(String name, int price,
int standbytime, int talkTime, boolean blueTooth) {
super(name);
this.price=price;
this.standbyTime=standbyTime;
this.talkTime=talkTime;
this.blooeTooth=blueTooth;
}
public void drawNameAndPrice(Graphics g, Point p) {
g.drawString(this.getLabel()+" "+price,p.x,p.y);
}
public void drawPhoneSpecs(Graphics g, Point p) {
g.drawString("Name: "+this.getLabel(),p.x,p.y); p.y+=20;
/* etc....*/
}
}
public class MobileShop extends Applet implements ActionListener {
public Button lastPressedBtn=null;
public Button priceListBtn=null;
public static MButton [] mBtns= {
new MButton("R2D2", 123, 20, 40, true),
new MButton("C3PO", 223, 22, 14, false),
new MButton("UB40", 323, 23, 24, true),
new MButton("WD40", 423, 24, 34, false),
new MButton("RU18", 523, 25, 44, false)
};
public void init() {
priceListBtn = new Button("Price List");
add(priceListBtn); // <---- uuups
priceListBtn.addActionListener(this);
for (int i=0;i<mBtns.length;i++) {
mBtns[i].addActionListener(this);
this.add(mBtns[i]); // <---- uuups
}
}
public void update(Graphics g) {
Color c=g.getColor();
g.setColor(Color.white);
g.fillRect(0,0,this.getSize().width, this.getSize().height);
g.setColor(Color.black);
Point p=new Point(10,10);
if (lastPressedBtn == priceListBtn) {
for (int i=0;i<mBtns.length;i++) {
mBtns[i].drawNameAndPrice(g,p);
p.y+=20;
}
} else {
((MButton)lastPressedBtn).drawPhoneSpecs(g,p);
}
g.setColor(c);
}
public void paint (Graphics g) {
update(g);
}
public void actionPerformed(ActionEvent event) {
lastPressedBtn=(Button)event.getSource();
repaint();
}
}
eschew obfuscation
-
ok, here is the program specification:
The aim of the assignment is to develop an applet that displays the details of a range of mobile phones.
Model: R2D2 C3PO
Talk Time: 3 4
Standby Time: 5 6
Weight: 102 85
Bluetooth: No Yes
Price: £79.95 £99.95
There are 5 models in total.
When the applet starts running, a price list is displayed showing the different mobiles available and their prices. It is possible to return to this list at any time by pressing the Price List button. When one of the other buttons is pressed, a description of the chosen model is displayed. This consists of the model name, talk time, standby time, weight and the mention of Bluetooth if the mobile uses it.
The program should consist of the classes Mobile and MobileShop (both in one file called MobileShop.java). The Mobile class should consist of appropriate attributes, a constructor, a method to display the description of the model, a method to display the name and price (i.e. the corresponding line in the price list) and a method to return the name of the model so that it can be used as a caption when the corresponding button is created. The Mobile objects should be created and stored in an array in the MobileShop class.
---------------------------------------------------------------
this is my code, but it doesnt work the way it should. so any help and advice would be much appreciated!!! :
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class Mobile extends Applet implements ActionListener {
public Button PriceList, R2D2, C3PO, UB40, WD40, RU18;
private int sbTime, tTime;
private int model;
private int weight;
private boolean hasBluetooth;
private double price;
public void init() {
PriceList = new Button ("Price List");
add (PriceList);
PriceList.addActionListener(this);
R2D2 = new Button ("R2D2");
add (R2D2);
R2D2.addActionListener(this);
C3PO = new Button ("C3PO");
add (C3PO);
C3PO.addActionListener(this);
UB40 = new Button ("UB40");
add (UB40);
UB40.addActionListener(this);
WD40 = new Button ("WD40");
add (WD40);
WD40.addActionListener(this);
RU18 = new Button ("RU18");
add (RU18);
RU18.addActionListener(this);
}
public void paint (Graphics g) {
g.drawString("R2D2 £79.95", 200, 200);
g.drawString("C3PO £99.95", 200, 230);
g.drawString("UB40 £129.95", 200, 260);
g.drawString("WD40 £189.95", 200, 290);
g.drawString("RU18 £219.95", 200, 320);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == PriceList)
if (event.getSource() == R2D2)
//model = R2D2;
price = 200;
sbTime = 36;
tTime = 6;
hasBluetooth = true;
weight = 10;
if (event.getSource() == C3PO)
if (event.getSource() == UB40)
if (event.getSource() == WD40)
if (event.getSource() == RU18)
repaint();
}
}
class Model{
private int sbTime, tTime;
private int model;
private int weight;
private boolean hasBluetooth;
private double price;
public void paint (Graphics g) {
g.drawString("ModelName : "+model, 200, 200);
g.drawString("Price: "+price, 200, 230);
g.drawString("Standby Time (days): "+sbTime, 200, 260);
g.drawString("Talk Time (hours) "+tTime, 200, 290);
g.drawString("Bluetooth : "+hasBluetooth, 200, 310);
g.drawString("Weight : "+weight, 200, 330);
}
}
-
Change the name of MButton to Mobile in my code, do the double panel implementation, and you're there.
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks