import java.awt.*;
public class d2x extends Frame
{
int decimalValue= 0;
String baseXValue = new String("0");
TextField dDisplay,xDisplay;
//d2x constructor
d2x()
{
super("Decimal Converter");//set the title of the frame
MenuBar mb = new MenuBar();
Button d2Binary = new Button("Binary");
Button d2Octal = new Button("Octal");
Button d2Hex = new Button("Hex");
Button d2Base36 = new Button("Base36");
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
//add a simple menu
Menu m = new Menu("Application");
m.add(new CheckboxMenuItem("Base 36 Active"));
m.add(new MenuItem("Exit"));
//add menu to menubar
mb.add(m);
setMenuBar(mb);//install this menu bar in the frame
//Add buttons to their own panel
p3.setLayout(new FlowLayout());
p3.add(d2Binary);
p3.add(d2Octal);
p3.add(d2Hex);
p3.add(d2Base36);
//Add text fields
Label dLabel = new Label("Enter Deecimal: ");
Label xLabel = new Label("Converted Value: ");
dDisplay = new TextField(Integer.toString(decimalValue),7);
xDisplay = new TextField(baseXValue,32);
xDisplay.setEditable(false);
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(dLabel);
p1.add(dDisplay);
p2.add(xLabel);
p2.add(xDisplay);
//Add the panels
add("North",p1);
add("Center",p2);
add("South",p3);
}//end d2x constructor
public void start()
{
resize(400,150);
show();
}
public void updateXDisplay()
{
xDisplay.setText(baseXValue);
}
public boolean handleEvent(Event evt)
{
if (evt.target instanceof MenuItem)
{
if ("Exit".equals(((MenuItem)evt.target).getLabel()))
{
hide();
dispose();
System.exit(0);
return false;
}
return true;
}
else if(evt.target instanceof Button)
{
String whick = ((Button)evt.target).getLabel();
if (whick.equals("Binary"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,2);
}
if (whick.equals("Octal"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,8);
}
if (whick.equals("Hex"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,16);
}
if (whick.equals("36"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,36);
}
updateXDisplay();
return true;
}
return false;
}
public static void main(String args[])
{
d2x m = new d2x();
m.start();
}
}