20 Oktober 2011

Main SimpleClick



package View;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class SimpleClick extends JFrame implements ActionListener {

    private JButton clickButton1, clickButton2;
    private JLabel numberOfClicksLabel1, numberOfClicksLabel2;
    private int numberOfClicks = 0;

    public SimpleClick() {
        this.setLayout(new FlowLayout()); // atur layout

        //tambah button
        clickButton1 = new JButton("I’mSwing Button");
        this.add(clickButton1);

        // tambah label
        numberOfClicksLabel1 = new JLabel("Number Of ButtonClick : ");
        this.add(numberOfClicksLabel1);

        clickButton2 = new JButton("I'm not Swing Button");
        this.add(clickButton2);


        //tambah event handler
        clickButton1.addActionListener(this);
        clickButton2.addActionListener(this);
        this.setTitle("swing Application");//atur judul aplikasi
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//atur kendali exit
        this.setSize(300, 200); // atur ukuran frame
        this.setVisible(true); // atur status tampilan dapat dilihat
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == clickButton1) {
            numberOfClicks += 1;
            numberOfClicksLabel1.setText("Number of Button Click"
                    + numberOfClicks);
        } else {
            numberOfClicks -= 1;
            numberOfClicksLabel1.setText("Number of Button Click"
                    + numberOfClicks);
        }
    }
}

Tidak ada komentar:

Posting Komentar