Contoh GUI

Posted by rieb | Posted in | Posted on

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

class GUI {
    String nim = "";
    String nama = "";


    GUI() {
         // 1 - container
         JFrame frame1 = new JFrame();
         Container c = frame1.getContentPane();

         // 2 - layout
         c.setLayout(new FlowLayout());

         // 3 - komponen
         JLabel lblSatu = new JLabel("NIM");
         JLabel lblDua = new JLabel("Nama");
         final JTextField txtSatu = new JTextField(10);
         final JTextField txtDua = new JTextField(10);
         JButton btnSatu = new JButton("Tampilkan");
         JButton btnDua = new JButton("Kosongkan");
         JButton btnTiga = new JButton("Simpan");

         c.add(lblSatu);
         c.add(txtSatu);
         c.add(lblDua);
         c.add(txtDua);
         c.add(btnSatu);
         c.add(btnDua);
         c.add(btnTiga);

         frame1.setVisible(true);
         //frame1.setSize(800, 100);
         frame1.pack();

         // 4 - action
         btnSatu.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                 txtSatu.setText(nim);
                 txtDua.setText(nama);
                 JOptionPane.showMessageDialog(null, txtSatu.getText());
                 JOptionPane.showMessageDialog(null, txtDua.getText());
             }
         });

         btnDua.addActionListener(new ActionListener() {
                      public void actionPerformed(ActionEvent e) {
                          txtSatu.setText("");
                          txtDua.setText("");
                      }
         });

           btnTiga.addActionListener(new ActionListener() {
                      public void actionPerformed(ActionEvent e) {
                          JOptionPane.showMessageDialog(null,"btn Simpan diklik");
                          nim = txtSatu.getText();
                          nama = txtDua.getText();

                      }
         });
    }

    public static void main (String [] args) {
        new GUI();
    }
}

Comments (0)

Posting Komentar

Isilah komentar dibawah ini sesuka anda asalkan jangan spam dan yang berbau SARA. Trims.,,