Kali ini kita akan membuat fungsi terbilang dengan menggunakan program java. Fungsi terbilang itu sendiri adalah misalkan kita menuliskan "999" tanpa tanda kutip, setelah di Convert maka akan tercetak "999=Sembilan ratus sembilan puluh sembilan".
Berikut ini adalah listing code-nya:
//TerbilangJava
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
class TerbilangJava extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JLabel jLabel = null;
private JTextField txtBilangan = null;
private JButton btnTerbilang = null;
private JTextArea jTextArea = null;
private StringBuffer sb = new StringBuffer();
String bilangan[] = new String[] { " ", "satu ", "dua ", "tiga ", "empat ",
"lima ", "enam", "tujuh ", "delapan ", "sembilan ", "sepuluh ",
"sebelas " };
private JButton jButton = null;
//tempat mengetikkan angka
private JTextField getTxtBilangan() {
if (txtBilangan == null) {
txtBilangan = new JTextField();
txtBilangan.setBounds(new Rectangle(135, 15, 166, 31));
}
return txtBilangan;
}
// Tombol Converter
private JButton getBtnTerbilang() {
if (btnTerbilang == null) {
btnTerbilang = new JButton();
btnTerbilang.setBounds(new Rectangle(315, 15, 152, 31));
btnTerbilang.setText("Terbilang");
btnTerbilang.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextArea.setText(" ");
String hasil = generate((double)Integer.parseInt(txtBilangan.getText()));
jTextArea.append(Integer.parseInt(txtBilangan.getText())+" = "+hasil);
txtBilangan.setText(" ");
sb.delete(0, 1000);
}
});
}
return btnTerbilang;
}
//Text Area
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setBounds(new Rectangle(15, 60, 616, 91));
}
return jTextArea;
}
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.out.println("actionPerformed()");
}
});
}
return jButton;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TerbilangJava thisClass = new TerbilangJava();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public TerbilangJava() {
super();
initialize();
}
//Luas Frame
private void initialize() {
this.setSize(650, 207);
this.setContentPane(getJContentPane());
this.setTitle("Fungsi Terbilang");
}
//Label Bilangan
private JPanel getJContentPane() {
if (jContentPane == null) {
jLabel = new JLabel();
jLabel.setBounds(new Rectangle(15, 15, 106, 31));
jLabel.setText("Bilangan");
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(jLabel, null);
jContentPane.add(getTxtBilangan(), null);
jContentPane.add(getBtnTerbilang(), null);
jContentPane.add(getJTextArea(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
public String generate(double number) {
if (number <>= 12 && number <>= 20 && number <>= 100 && number <>= 200 && number <>
»» Selanjutnya...