Di sini saya membuat program menentukan luas persegi panjang, dan hasil outputnya adalah :
Untuk source code nya:
import javax.swing.*;
import java.awt.event.*;
public class jframe extends JFrame{
public jframe(){
initComponents();
}
private void initComponents(){
setTitle("LUAS PERSEGI PANJANG");
setSize(550, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new JPanel();
namaLabel = new JLabel("Nama");
nimLabel = new JLabel("Nim");
aLabel = new JLabel("Menghitung Luas Persegi Panjang:");
panjangLabel = new JLabel("Panjang");
lebarLabel = new JLabel("Lebar");
luasLabel = new JLabel("Luas ");
namaTextField= new JTextField();
nimTextField= new JTextField();
aTextField= new JTextField();
panjangTextField = new JTextField();
lebarTextField = new JTextField();
luasTextField = new JTextField();
hitungButton = new JButton("Hitung");
tutupButton = new JButton("Tutup");
panel.setLayout(null);
getContentPane().add(panel);
panel.add(namaLabel);
panel.add(nimLabel);
panel.add(aLabel);
panel.add(panjangLabel);
panel.add(lebarLabel);
panel.add(luasLabel);
panel.add(namaTextField);
panel.add(nimTextField);
panel.add(panjangTextField);
panel.add(lebarTextField);
panel.add(luasTextField);
panel.add(hitungButton);
panel.add(tutupButton);
namaLabel.setBounds(30,30,90,25);
nimLabel.setBounds(30,60,90,25);
aLabel.setBounds(30,90,900,25);
panjangLabel.setBounds(30,120,90,25);
lebarLabel.setBounds(30,150,90,25);
luasLabel.setBounds(30,180,90,25);
namaTextField.setBounds(100,30,400,25);
nimTextField.setBounds(100,60,400,25);
panjangTextField.setBounds(100,120,400,25);
lebarTextField.setBounds(100,150,400,25);
luasTextField.setBounds(100,180,400,25);
hitungButton.setBounds(200,230,90,30);
tutupButton.setBounds(350,230,90,30);
luasTextField.setEditable(false);
hitungButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
hitungButtonActionPerformed(evt);
}
});
tutupButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
tutupButtonActionPerformed(evt);
}
});
}
private void hitungButtonActionPerformed(ActionEvent evt){
double panjang=0, lebar=0, luas;
try{
panjang=Double.parseDouble(panjangTextField.getText());
}catch (NumberFormatException ex){
JOptionPane.showMessageDialog(this, "Yang anda masukan salah !!");
}
try{
lebar= Double.parseDouble(lebarTextField.getText());
} catch (NumberFormatException ex){
JOptionPane.showMessageDialog(this, "Yang anda masukan salah !!");
}
luas = (panjang) * (lebar);
luasTextField.setText(Double.toString(luas));
}
private void tutupButtonActionPerformed(ActionEvent evt){
System.exit(0);
}
public static void main(String[] args){
new jframe().setVisible(true);
}
private JPanel panel;
private JLabel namaLabel;
private JLabel nimLabel;
private JLabel aLabel;
private JLabel panjangLabel;
private JLabel lebarLabel;
private JLabel luasLabel;
private JTextField namaTextField;
private JTextField nimTextField;
private JTextField aTextField;
private JTextField panjangTextField;
private JTextField lebarTextField;
private JTextField luasTextField;
private JButton hitungButton;
private JButton tutupButton;
}