package tugas1; import javax.swing.*; import java.awt.event.*; public class Tugas1 extends JFrame{ JTextField fieldTinggi, fieldLebar; public Tugas1(){ super("Perhitungan Luas Persegi Panjang"); setSize(500,200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel=new JPanel(); JLabel labelTinggi=new JLabel("Tinggi :"); JLabel labelLebar=new JLabel("Lebar:"); fieldTinggi=new JTextField(10); fieldLebar=new JTextField(10); JButton tombol=new JButton("Hitung"); panel.add(labelTinggi); panel.add(fieldTinggi); panel.add(labelLebar); panel.add(fieldLebar); panel.add(tombol); add(panel); tombol.addActionListener(new ActionListener(){ double lebarPersegiPanjang = 0 , tinggiPersegiPanjang = 0; public void actionPerformed(ActionEvent e){ try{ tinggiPersegiPanjang = Double.parseDouble(fieldTinggi.getText()); } catch (NumberFormatException ex){ JOptionPane.showMessageDialog(null,"Masukkan bilangan real untuk tinggi"); fieldTinggi.requestFocus(); return; } try{ lebarPersegiPanjang = Double.parseDouble(fieldLebar.getText()); } catch (NumberFormatException ex){ JOptionPane.showMessageDialog(null,"Masukkan bilangan real untuk lebar"); fieldLebar.requestFocus(); return; } double luasPersegiPanjang = tinggiPersegiPanjang * lebarPersegiPanjang; JOptionPane.showMessageDialog(null,"Luas = "+ luasPersegiPanjang); } }); setVisible(true); setLocationRelativeTo(null); } public static void main (String[] args) { Tugas1 jendela = new Tugas1(); } }
package tugas2; import javax.swing.*; import java.awt.event.*; import java.awt.*; public class Tugas2 extends JFrame { public Tugas2 () { super("Form Login"); setSize(300,150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container panel = getContentPane(); JLabel labelid = new JLabel(" User ID : "); JLabel labelpassword = new JLabel(" Password : "); JTextField fieldid = new JTextField(20); JPasswordField password = new JPasswordField(15); JButton tombol = new JButton("LOGIN"); JButton exit = new JButton("EXIT"); labelid.setBounds(20,13,200,30); labelpassword.setBounds(20,45,200,30); fieldid.setBounds(120,20,150,20); password.setBounds(120,50,150,20); tombol.setBounds(20,80,90,30); exit.setBounds(180,80,90,30); panel.setLayout(null); panel.add(labelid); panel.add(fieldid); panel.add(labelpassword); panel.add(password); panel.add(tombol); panel.add(exit); tombol.addActionListener(new ActionListener(){ String user; public void actionPerformed (ActionEvent e){ if (e.getSource() == tombol){ user = fieldid.getText(); JOptionPane.showMessageDialog(null,"Selamat Datang Kembali "+user); } } }); exit.addActionListener(new ActionListener (){ public void actionPerformed (ActionEvent e){ if (e.getSource() == exit){ JOptionPane.showMessageDialog(null,"Terima Kasih"); System.exit(0); } } }); setVisible(true); setLocationRelativeTo(null); } public static void main (String[] args) { Tugas2 app = new Tugas2(); } }