silaken dikopi untuk kepentingan pendidikan... :) (english version)
tampilannya kayak gini :
kosongan... |
misal diisi angka 234 |
hasilnya... |
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package SpiralenNumber;
//import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.Event;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
/**
*
* @author bapake-aisyah
*/
public class KonversiAngka extends JFrame implements ActionListener {
//private static ActionListener eventclick;
private String hasilKonv;
public String convert1Digit(int x) {
String array[] = {"zero", "one", "two", "three", "four", "five", "six",
"Seven", "eight", "nine"};
return array[x];
/*switch (x) {
case 0:
return "zero";
case 1:
return "one";
case 2:
return "two";
case 3:
return "three";
case 4:
return "four";
case 5:
return "five";
case 6:
return "six";
case 7:
return "seven";
case 8:
return "eight";
case 9:
return "nine";
default:
return "";
} */
}
public String convert2Digit(int x) {
if (x < 20) {
int y = x % 10;
String hasil = this.convert1Digit(y) + "teen";
String strreplace;
if (y == 0) {
strreplace = "ten";
hasil = hasil.replaceAll(hasil, strreplace);
} else if (y == 1) {
strreplace = "eleven";
hasil = hasil.replaceAll(hasil, strreplace);
} else if (y == 2) {
strreplace = "twelve";
hasil = hasil.replaceAll(hasil, strreplace);
} else if (y == 3) {
strreplace = "thir";
hasil = hasil.replaceAll("three", strreplace);
} else if (y == 5) {
strreplace = "fif";
hasil = hasil.replaceAll("five", strreplace);
}
return hasil;
} else {
int y = x % 10;
int z = x / 10;
String hasil = this.convert1Digit(z) + "ty " + this.convert1Digit(y);
String strreplace = "";
if (y == 0) {
hasil = hasil = hasil.replaceAll("zero", strreplace);
}
if (z == 2) {
strreplace = "twenty";
hasil = hasil.replaceAll("twoty", strreplace);
} else if (z == 3) {
strreplace = "thirty";
hasil = hasil.replaceAll("threety", strreplace);
} else if (z == 5) {
strreplace = "fifty";
hasil = hasil.replaceAll("fivety", strreplace);
}
return hasil;
}
}
public String convert3Digit(int x) {
int y = x % 100;
int z = x / 100;
String hasil;
if (z == 1) {
hasil = this.convert1Digit(z) + " hundred " + this.convert2Digit(y);
} else {
hasil = this.convert1Digit(z) + " hundreds " + this.convert2Digit(y);
}
String strreplace;
if (y == 0) {
strreplace = "";
hasil = hasil.replaceAll("ten", strreplace);
}
return hasil;
}
public String konvert(int x) {
String hasil = "";
if (x < 10) {
hasil = this.convert1Digit(x);
} else if (x >= 10 && x <= 99) {
hasil = this.convert2Digit(x);
} else if (x >= 100 && x <= 999) {
hasil = this.convert3Digit(x);
} else {
//hasil = "nyuwun ngapunten nggih, ongkonipun ampun langkung kaliyan 999";
JOptionPane.showMessageDialog(null, "nyuwun ngapunten nggih, ongkonipun ampun langkung kaliyan 999"
, "PEPENGET", JOptionPane.WARNING_MESSAGE);
}
return hasil;
}
public boolean konversiClick(Event evt, int x) {
if (x < 10) {
this.convert1Digit(x);
} else if (x < 100) {
this.convert2Digit(x);
} else if (x < 1000) {
this.convert3Digit(x);
}
return true;
}
public static void main(String[] args) {
final KonversiAngka konversi = new KonversiAngka();
JFrame frame = new JFrame("Ngganti Ongko nang Huruf");
frame.setSize(300, 100);
frame.setLocation(500, 250);
frame.setVisible(true);
final JTextField textF = new JTextField();
textF.setSize(500, 50);
textF.setLocation(500, 250);
textF.setVisible(true);
JButton ubah = new JButton();
ubah.setText("ngganti");
ubah.setSize(50, 50);
JButton exit = new JButton();
exit.setText("metu");
exit.setSize(50, 50);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(textF, BorderLayout.NORTH);
frame.getContentPane().add(ubah, BorderLayout.WEST);
frame.getContentPane().add(exit, BorderLayout.EAST);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ubah.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
try {
String input = textF.getText();
int bil = Integer.parseInt(input);
if (bil < 0) {
JOptionPane.showMessageDialog(null, "kolom isian dereng diisi", "PEPENGET",
JOptionPane.WARNING_MESSAGE);
} else {
textF.setText(konversi.konvert(bil));
}
} catch (NumberFormatException f) {
JOptionPane.showMessageDialog(null, "isian sanes ongko!", "ONGKO ERROR",
JOptionPane.ERROR_MESSAGE);
}
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JOptionPane.showMessageDialog(null, "sampeyan bade medal", "MEDAL",
JOptionPane.YES_NO_CANCEL_OPTION);
System.exit(100);
}
});
}
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
0 komentar:
Posting Komentar