Log in

View Full Version : [ASK] java converter


Linuxmania
20th November 2011, 11:12 AM
Code:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package testcurrency;

/**
*
* @author christian
*/
import java.awt.*;
import java.text.*;
import java.applet.*;;
import java.util.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.applet.Applet;
import java.awt.Button;
import java.awt.Label;
import java.awt.Panel;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Choice;
import java.text.DecimalFormat;
import javax.swing.text.NumberFormatter;
import javax.swing.JFormattedTextField;
import java.awt.Font;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Color;
import java.text.DateFormat;
import java.awt.event.ActionEvent;

public class Main extends Applet implements ActionListener
{
Button b1 = new Button("C O N V E R T");
Button b2 = new Button("C L E A R");

Label ld = new Label();
Label l1 = new Label();
Label l2 = new Label("");
Label l3 = new Label("Amount: ");
Label l4 = new Label("From: ");
Label l5 = new Label("To : ");
Label l6 = new Label("");
Label l7 = new Label("Result : ");
Label l8 = new Label("Rate : ");
Label l9 = new Label();
Label lrst = new Label("");
Label lrate = new Label("");
Label lsign = new Label("");


Panel p1 = new Panel(new GridLayout(7,1,2,2) );
Panel p2 = new Panel(new GridLayout(7,1,2,2) );
Panel p3 = new Panel(new FlowLayout());
Panel p4 = new Panel(new FlowLayout(FlowLayout.LEFT));
Panel p5 = new Panel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

Choice ch1 = new Choice();
Choice ch2 = new Choice();




TextField tf = new TextField();



Font myFont = new Font("Courier", Font.BOLD,12);


Font f1 = new Font("Ariel", Font.BOLD, 15);

Double [] rate = {1.43973,0.702280,7.74979,11019.58,47.1476,89.8416 ,3.47406,1.43499,0.664559,1.00};
String [] strg = {"AUD","EUR","HKD","IDR","INR","JPY","MYR","SGD","GBP","USD"};

public void init()
{
setLayout(new BorderLayout() );
setSize(300, 50);
p1.setPreferredSize(new Dimension(100, 70));
tf.setPreferredSize(new Dimension(100, 10));
tf.setFont(myFont);
tf.setColumns(20);
//textFormatter.setAllowsInvalid(false);
b1.setFont(myFont);
b2.setFont(myFont);

ld.setFont(f1);
ld.setAlignment(Label.CENTER);
ld.setForeground(Color.yellow);
ld.setBackground(Color.blue);
add(ld, "North");

l2.setFont(f1);
l2.setAlignment(Label.CENTER);
l2.setForeground(Color.red);
l2.setBackground(Color.black);
add(l2, "South");

Date now = new Date();
DateFormat df = DateFormat.getInstance();
ld.setText("Currency Converter " + df.format(now));
l2.setText("Last Update " + df.format(now));

l3.setFont(f1);
l3.setAlignment(Label.LEFT);
add(l3, "West");

l4.setFont(f1);
l4.setAlignment(Label.LEFT);
add(l4, "West");

l5.setFont(f1);
l5.setAlignment(Label.LEFT);
add(l5, "West");


l7.setFont(f1);
l7.setAlignment(Label.LEFT);
add(l7, "West");

l8.setFont(f1);
l8.setAlignment(Label.LEFT);
add(l8, "East");

p2.add(l3);
p2.add(l4);
p2.add(l5);
p2.add(l6);
p2.add(l7);
p2.add(l8);
add(p2, "West");

ch1.add("Australia Dollar");
ch1.add("Euro");
ch1.add("Hong Kong Dollar");
ch1.add("Indonesia Rupiah");
ch1.add("India Rupee");
ch1.add("Japan Yen");
ch1.add("Malaysia Ringgit");
ch1.add("Singapore Dollar");
ch1.add("United Kingdom Pounds");
ch1.add("United States Dollar");

ch2.add("Australia Dollar");
ch2.add("Euro");
ch2.add("Hong Kong Dollar");
ch2.add("Indonesia Rupiah");
ch2.add("India Rupee");
ch2.add("Japan Yen");
ch2.add("Malaysia Ringgit");
ch2.add("Singapore Dollar");
ch2.add("United Kingdom Pounds");
ch2.add("United States Dollar");

c.gridx = 0;
c.gridy = 0;
c.weightx = 0.5;
c.weighty = 0.1;
c.fill = GridBagConstraints.BOTH;
p5.add(tf,c);

p4.add(lrate);

p1.add(p5);
p1.add(ch1);
p1.add(ch2);
p1.add(p3);
p1.add(lrst);
p1.add(lrate);
p1.add(l1);
p1.setBackground(Color.blue);
add(p1, "Center");


p3.add(b1);
p3.add(b2);


b1.addActionListener(this);
b2.addActionListener(this);

}


public void actionPerformed(ActionEvent e)
{


if (e.getSource() == b1)

{


int indexFrom = ch1.getSelectedIndex();
int indexTo = ch2.getSelectedIndex();
double amount = Double.parseDouble(tf.getText());
double myRate = rate[indexTo]/rate[indexFrom];
double result = amount * myRate;
String rslt = Double.toString(result);
String rt = Double.toString(myRate);
String srt = strg[indexTo];
String txt = "error";
DecimalFormat decf = new DecimalFormat("0.000");
lrst.setText(decf.format(result)+" "+srt);
lrate.setText(decf.format(myRate));

}

if (e.getSource() == b2)
{
//tf.setValue("");
lrst.setText("");
lrate.setText("");
}
}
}



misal saya mau bikin ratenya ga manual tapi update internet gitu gimana yah?

lalu kalau mau setting gede tabllenya sebrapa gitu gimana yah?

maap masih newbie tentang pemrograman

</div>