Pages

Subscribe:

Ads 468x60px

♪ ♥ Get Paid Daily - The Best Sites PTC ♥ ♪
DonkeyMails.com: No Minimum Payout
AyuWage Services - Get Paid to Visits Sites and Complete Surveys

Minggu, 15 Juli 2012

Dasar-dasar GUI Java


Contoh program: guijava.java
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.GridLayout;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class guijava implements ActionListener {
JLabel labelBilangan1 = new JLabel(“bilangan 1: “);
JTextField fieldBilangan1 = new JTextField(10);
JLabel labelBilangan2 = new JLabel(“bilangan 2: “);
JTextField fieldBilangan2 = new JTextField(10);
JLabel labelHasil = new JLabel(“hasil: “);
JTextField fieldHasil = new JTextField(10);
JButton buttonTambah = new JButton(“tambah”);
JButton buttonHapus = new JButton(“hapus”);
JPanel panelPenjumlahan = new JPanel();
guijava(){
GridLayout gl = new GridLayout(4,2);
panelPenjumlahan.setLayout(gl);
panelPenjumlahan.add(labelBilangan1);
panelPenjumlahan.add(fieldBilangan1);
panelPenjumlahan.add(labelBilangan2);
panelPenjumlahan.add(fieldBilangan2);
panelPenjumlahan.add(labelHasil);
panelPenjumlahan.add(fieldHasil);
panelPenjumlahan.add(buttonTambah);
panelPenjumlahan.add(buttonHapus);
buttonTambah.addActionListener(this);
buttonHapus.addActionListener(this);
JFrame jendela = new JFrame(“Contoh GUI Java”);
jendela.setContentPane(panelPenjumlahan);
jendela.setSize(400,150);
jendela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jendela.setVisible(true);
jendela.setResizable(false);
}
public static void main(String[] args) {
guijava contohGUI = new guijava();
}
public void actionPerformed(ActionEvent kejadian) {
try {
if(kejadian.getSource() == buttonTambah) {
int bilangan1 = Integer.parseInt(fieldBilangan1.getText());
int bilangan2 = Integer.parseInt(fieldBilangan2.getText());
int hasil = bilangan1 + bilangan2;
fieldHasil.setText(Integer.toString(hasil));
}
else {
fieldBilangan1.setText(“”);
fieldBilangan2.setText(“”);
fieldHasil.setText(“”);
fieldBilangan1.requestFocus();
}
}
catch(Exception a) {
JOptionPane.showMessageDialog(panelPenjumlahan, “anda salah memasukkan angka.”);
}
}
}
Output program:

0 komentar:

Posting Komentar

Please Give Your Feedback Or Message.
Thank You!!?

Unique 1:1 Traffic Exchange

♪ ♥ Ma liste de blogs ♥ ♪

♪ ♥ Please Votes! Thanks. ♪ ♥
×