Program GUI Part 2
Coba GUI
GUI fakimport java.awt.*;import java.awt.event.*;import javax.swing.*;
class cobaGUI implements ActionListener{public JLabel label, label2;public JButton button1;public JTextField text1, text2;
public cobaGUI(){label = new JLabel ("Masukkan Nama Anda :");label.setLocation(10, 10);label.setSize(label.getPreferredSize());
label2 = new JLabel ("Hasil :");label2.setLocation(10, 90);label2.setSize(label2.getPreferredSize());
button1 = new JButton ("OK");button1.setLocation(245, 25);button1.setSize(button1.getPreferredSize());button1.setMnemonic('K');button1.addActionListener(this);text1 = new JTextField (20);text1.setLocation(10, 25);text1.setSize(text1.getPreferredSize());
text2 = new JTextField (20);text2.setLocation(10, 105);text2.setSize(text2.getPreferredSize());}
public void actionPerformed(ActionEvent event) {String nama = null;nama = text1.getText();
JButton btn = (JButton) event.getSource();if (btn == button1)text2.setText(new String().valueOf(nama));}
public void demoframe(){JFrame.setDefaultLookAndFeelDecorated(true);JFrame frame = new JFrame("Belajar Buat Frame");frame.setLayout(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setBounds(0, 0, 350, 200);frame.setLocationRelativeTo(null);frame.setVisible(true);frame.getContentPane().add(label);frame.getContentPane().add(label2);frame.getContentPane().add(button1);frame.getContentPane().add(text1);frame.getContentPane().add(text2);}
public static void main (String[] args) {cobaGUI app = new cobaGUI();app.demoframe();}}
import javax.swing.*;import java.awt.event.*;import java.awt.*;
class GUIfak extends JFrame implements ActionListener{double hasil;Container con = new Container();JButton hapus, ok;JTextField tex1, tex2;
double fakt(double angka){if(angka == 0){return 1;}else{return angka*fakt(angka-1);}}public GUIfak(){super("faktorial niy..!!");setSize (300,250);ok = new JButton ("faktorial");hapus = new JButton ("hapus");ok.addActionListener (this);hapus.addActionListener (this);
JPanel tombol = new JPanel ();hapus.setEnabled (false);tombol.setLayout (new GridLayout(1,2,10,10));tombol.add (hapus);tombol.add(ok);
tex1 = new JTextField("");tex2 = new JTextField("");tex2.setEditable (false);
JPanel tex = new JPanel();tex.setLayout(new GridLayout (2,1,10,10));tex.add(tex1);tex.add(tex2);con = getContentPane();con.setLayout(null);tex.setBounds(100,80,100,50);tombol.setBounds(50,150,200,30);con.add(tombol);con.add(tex);show();}
public void actionPerformed (ActionEvent e){try{if (e.getSource()==ok){hapus.setEnabled(true);String a;double angka = Double.parseDouble(tex1.getText());hasil = fakt(angka);}tex2.setText(Double.toString(hasil));if (e.getSource() == hapus){tex1.setText("");tex2.setText("");hapus.setEnabled(false);}}catch(Exception ex){hapus.setEnabled(false);JOptionPane.showMessageDialog(this, "Masukkan nilai yang benar..!!");}}
public static void main(String[] Zzzz){GUIfak q = new GUIfak();q.setResizable(false);q.setDefaultCloseOperation(EXIT_ON_CLOSE);}}
0 komentar:
Posting Komentar