Pages

Kamis, 17 November 2011

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;

/**
 *
 * @author admin
 */
public class SimpleClick extends JFrame implements ActionListener {

    private JButton clickButton, clickButton2;
    private JLabel numberOfClicksLabel;
    private int numberOfClicks = 0;

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

        //tambah button
        clickButton = new JButton("I'm Swing Button ");
        this.add(clickButton);

        // tambah label
        numberOfClicksLabel = new JLabel("Number Of ButtonClick:");
        this.add(numberOfClicksLabel);
        clickButton.addActionListener(this);
        this.setTitle("swing Application");//atur judul aplikasi
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//atur kendali exit
        this.setSize(500, 200); // atur ukuran frame
        this.setVisible(true); // atur status tampilan dapat dilihat

        // tambah button
        clickButton2 = new JButton("I'm not swing button");
        this.add(clickButton2);
        //tambah event handler
        clickButton2.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == clickButton) {
            numberOfClicks += 1;
            numberOfClicksLabel.setText("number of button click: " + numberOfClicks);

        }
        if (e.getSource() == clickButton2) {
            numberOfClicks -= 1;
            numberOfClicksLabel.setText("number of button click: " + numberOfClicks);

        }
    }

}



Kelas Testing
package view;

public class tesSimpleClick {

    public static void main(String[] args) {

        SimpleClick simple = new SimpleClick();
//        simple.setVisible(true);
    }

}

0 komentar:

Posting Komentar