i need to arrange my Knowledge
I want to create a calculator in applet. But to do that I want to create 4 classes.
- that i will calculate the numbers
- that i will made there the methods for calculate that i will use in the calculate class
- I want to make class that i will design the calculator
- that will be the calculator main.
for now I made the design class.
Please, take a look:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Caldesinger extends BorderLayout
{
public Panel p1=new Panel();
public Button[] arr=new Button[20];
public String[] name = {"9","8","7","6","5","4","3","2","1","0","+","-","*","/",".","cos","sin","=","pow"};
public Caldesinger()
{
for (int i = 0; i < arr.开发者_如何学Golength; i++)
{
this.arr[i]=new Button(""+name[i]);
}
}
public Panel caldesinge()
{
for (int i = 0; i < arr.length; i++)
{
this.arr[i].setBackground(Color.LIGHT_GRAY);
this.arr[i].setForeground(Color.orange);
this.p1.add(arr[i]);
}
this.p1.add(this.p1,BorderLayout.NORTH);
return this.p1;
}
}
I must know how the classes are conected that i know the main is extends for the applet and i must have at the main class an init method
i need your expertise that the 4 classes will work together needs help..
I am sorry, but your question is unclear. It seems this is your first program in Java and probably the first program at all.
Generally your main class should extend Applet, override its init() method and implement it. You can read the applet parameters there. The start() method of your applet should create instances of view (UI) and model. Obviously UI has to be able to communicate with model and vise versa.
This problem is typically solved using pattern named MVC (model-view-controller). View is UI, model is the logic that calculates numbers, controller is a collection of action listeners that you attach to your buttons.
I hope it helps. If not search for simple tutorial that explains how to write GUI in java.
Classes 1 and 2. should be combined. Also, when designing new programs give the classes names to that you can ask your self should design questions like "Should the Engine class really have a openCarDoor() method?"
精彩评论