Tips for heathy life

Tips for heathy life

Senin, 25 Agustus 2014

How to make GUI Form Login dan Input data in Java SE?

I want to share how to make GUI Form Login dan Input data in Java SE. You can use this code but if you copy this code and paste in your blog/website, please insert this link of article.

----------
package gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class mwi {

public mwi() {

JFrame frame = new JFrame();

JButton tombol1 = new JButton("LOGIN");
JButton tombol2 = new JButton("LOGOUT");
JLabel lblPengguna = new JLabel ("Pengguna");
JLabel lblPasword = new JLabel ("Password");
JLabel lblCreated = new JLabel ("Created by :Dani:.");
final JTextField txtPengguna = new JTextField (10);
final JPasswordField txtPasword = new JPasswordField (10);

tombol1.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent event) {
txtPengguna.setText("dani");
txtPasword.setText("do u want it?");
}
});

tombol2.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(7);
}
});

Container contentPane = frame.getContentPane();
//mengeset jenis layout tanpa menggunakan jenis layout apapun
contentPane.setLayout(null);
contentPane.add(tombol1);
contentPane.add(txtPengguna);
contentPane.add(tombol2);
contentPane.add(txtPasword);
contentPane.add(lblPengguna);
contentPane.add(lblPasword);
contentPane.add(lblCreated);


//meletakkan komponen pada posisi tertentu
Insets insets = contentPane.getInsets();
//mengatur batas untuk msg2 komponen
//namaKomponen.setBounds(, ,,)
lblPengguna.setBounds(insets.left + 5, insets.top +10, 150, 20);
lblPasword.setBounds(insets.left + 5, insets.top + 40, 150, 20);
txtPengguna.setBounds(insets.left + 165, insets.top + 10, 150, 20);
txtPasword.setBounds(insets.left + 165, insets.top + 40, 150, 20);
tombol1.setBounds(insets.left + 5, insets.top + 100, 150, 20);
tombol2.setBounds(insets.left +170, insets.top +100, 150, 20);
lblCreated.setBounds(insets.left + 340, insets.top + 100, 150, 20);

frame.setTitle("Dani");
frame.pack();
frame.setSize(new Dimension(500, 160));
frame.setVisible(true);
}
public static void main(String args[]) {
new mwi();
}
}
=============================
package gui2;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.acl.Group;
import javax.swing.*;

public class Dani {

public Dani() {

JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();

JButton tombol1 = new JButton("TAMPIL");
JButton tombol2 = new JButton("BERSIH");
JButton tombol3 = new JButton("KELUAR");
JLabel lblNim = new JLabel ("NIM");
JLabel lblNama = new JLabel ("NAMA");
JLabel lblJenkel = new JLabel ("JENKEL");
JLabel lblAgama = new JLabel ("AGAMA");
final JTextField txtNim = new JTextField (10);
final JTextField txtNama = new JTextField (10);

JComboBox cmb1 = new JComboBox ();
JList lst1 = new JList();

JRadioButton opt1 = new JRadioButton("Laki-Laki");
opt1.setSelected(true);
JRadioButton opt2 = new JRadioButton("Perempuan");
ButtonGroup group1= new ButtonGroup();
group1.add (opt1);
group1.add(opt2);

cmb1.addItem("Islam");
cmb1.addItem("Kristen");
cmb1.addItem("Khatolik");
cmb1.addItem("Bindu");
cmb1.addItem("Budha");

//user interface
tombol1.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent event) {
txtNim.setText("06.12.2021");
txtNama.setText("Vieta");
}
});

tombol2.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent event) {
txtNim.setText("");
txtNama.setText("");
}
});

tombol3.addActionListener (new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
Container contentPane = frame.getContentPane();
//mengeset jenis layout tanpa menggunakan jenis layout apapun
contentPane.setLayout(null);
contentPane.add(tombol1);
contentPane.add(txtNim);
contentPane.add(tombol2);
contentPane.add(txtNama);
contentPane.add(tombol3);
contentPane.add(lblNim);
contentPane.add(lblNama);
contentPane.add(lblAgama);
contentPane.add(lblJenkel);

contentPane.add(cmb1);
contentPane.add(lst1);
contentPane.add(opt1);
contentPane.add(opt2);



//meletakkan komponen pada posisi tertentu
Insets insets = contentPane.getInsets();
//mengatur batas untuk msg2 komponen
//namaKomponen.setBounds(, ,,)
lblNim.setBounds(insets.left + 5, insets.top +10, 150, 20);
lblNama.setBounds(insets.left + 5, insets.top + 40, 150, 20);
lblJenkel.setBounds(insets.left + 5, insets.top + 70, 150, 20);
opt1.setBounds(insets.left + 150, insets.top + 70, 100, 20);
opt2.setBounds(insets.left + 250, insets.top + 70, 150, 20);
lblAgama.setBounds(insets.left + 5, insets.top + 100, 150, 20);
cmb1.setBounds(insets.left +150, insets.top +100 , 150, 20);
txtNim.setBounds(insets.left + 165, insets.top + 10, 150, 20);
txtNama.setBounds(insets.left + 165, insets.top + 40, 150, 20);
tombol1.setBounds(insets.left + 5, insets.top + 200, 150, 20);
tombol2.setBounds(insets.left +150, insets.top +200, 150, 20);
tombol3.setBounds(insets.left +300, insets.top +200, 150, 20);

lst1.setBounds(insets.left + 5, insets.top + 300 ,300, 20);

frame.setTitle("Dani");
frame.pack();
frame.setSize(new Dimension(500, 300));
frame.setVisible(true);
}
public static void main(String args[]) {
new Dani();
}
}

0 komentar:

Posting Komentar