import java.awt.event.*;
public class TesTextField extends Frame {
Label tInfo;
TextField f1, f2;
Button b;
public static void main(String[] args) {
TesTextField apl = new TesTextField();
}
public TesTextField() {
super("Tes TextField");
setSize(300, 120);
Label t1 = new Label("Nama Pemakai:");
f1 = new TextField("", 10);
Label t2 = new Label("Password:");
f2 = new TextField("", 10);
f2.setEchoChar('*');
b = new Button("Login");
tInfo = new Label("Masukkan nama pemakai " +
"dan password");
Panel p1 = new Panel();
p1.setLayout(new GridLayout(2,2));
p1.add(t1);
p1.add(f1);
p1.add(t2);
p1.add(f2);
GridBagLayout gb = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
Panel p2 = new Panel();
p2.setLayout(gb);
gbc.gridx = 0;
gb.setConstraints(b,gbc);
gb.setConstraints(tInfo,gbc);
p2.add(b);
p2.add(tInfo);
add("North", p1);
add("South", p2);
b.addActionListener(
new TesTextField.PenanganTombol());
// --- Tampilkan frame
show();
}
class PenanganTombol implements ActionListener {
public void actionPerformed(ActionEvent e ) {
String s = e.getActionCommand();
if (s.equals("Login")) {
String namaPemakai = f1.getText();
String password = f2.getText();
if (namaPemakai.compareTo("dwi") == 0 && //set untuk username
password.compareTo("dwipasword") == 0) //set untuk password
tInfo.setText("Login OK");
else
tInfo.setText("Ulangi Login");
}
}
}
}
:: output programnya ::
:: jika kondisi benar ::
jika username /password salah:

syntax error..
BalasHapushehehe