belajar java part 3 pertemuan 4

Panggil.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class panggil implements ActionListener{
public JLabel label;
public JButton button1;
public JTextField text1,text2,text3;
int hasil,angka1,angka2;
public panggil(){
label = new JLabel (“mencoba kalkulator”);
label.setLocation(10, 100);
label.setSize(label.getPreferredSize());
button1 = new JButton (“panggil”);
button1.setLocation(245, 10);
button1.setSize(button1.getPreferredSize());
button1.addActionListener (this);
}
public void demoframe(){
JFrame frame = new JFrame(“Belajar Berhitung”);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().add(button1);
frame.setBounds(0, 0, 350, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource()==button1)
{
cobabutton app = new cobabutton();
app.demoframe();
}
}
public static void main (String[] args) {
panggil app = new panggil();
app.demoframe();
}
}
Cobabutton.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class cobabutton implements ActionListener{
public JLabel label;
public JButton button1,button2,button3,button4;
public JTextField text1,text2,text3;
int hasil,angka1,angka2;
public cobabutton(){
label = new JLabel (“mencoba kalkulator”);
label.setLocation(10, 100);
label.setSize(label.getPreferredSize());
button1 = new JButton (“X”);
button1.setLocation(245, 10);
button1.setSize(button1.getPreferredSize());
button1.addActionListener (this);
button2 = new JButton (“/”);
button2.setLocation(245, 50);
button2.setSize(button2.getPreferredSize());
button2.addActionListener (this);
button3 = new JButton (“+”);
button3.setLocation(245, 90);
button3.setSize(button3.getPreferredSize());
button3.addActionListener (this);
button4 = new JButton (“-“);
button4.setLocation(245, 130);
button4.setSize(button4.getPreferredSize());
button4.addActionListener (this);
text1 = new JTextField (20);
text1.setLocation(10, 10);
text1.setSize(text1.getPreferredSize());
text2 = new JTextField (20);
text2.setLocation(10, 40);
text2.setSize(text2.getPreferredSize());
text3 = new JTextField (20);
text3.setLocation(10, 70);
text3.setSize(text3.getPreferredSize());
}
public void demoframe(){
JFrame frame = new JFrame(“Belajar Berhitung”);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().add(button1);
frame.getContentPane().add(button2);
frame.getContentPane().add(button3);
frame.getContentPane().add(button4);
frame.getContentPane().add(text1);
frame.getContentPane().add(text2);
frame.getContentPane().add(text3);
frame.setBounds(0, 0, 350, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource()==button1)
{
int angka1 = Integer.parseInt(text1.getText());
int angka2 = Integer.parseInt(text2.getText());
hasil = angka1*angka2;
}
text3.setText(Integer.toString(hasil));
if (e.getSource()==button2)
{
int angka1 = Integer.parseInt(text1.getText());
int angka2 = Integer.parseInt(text2.getText());
hasil = angka1/angka2;
}
text3.setText(Integer.toString(hasil));
if (e.getSource()==button3)
{
int angka1 = Integer.parseInt(text1.getText());
int angka2 = Integer.parseInt(text2.getText());
hasil = angka1+angka2;
}
text3.setText(Integer.toString(hasil));
if (e.getSource()==button4)
{
int angka1 = Integer.parseInt(text1.getText());
int angka2 = Integer.parseInt(text2.getText());
hasil = angka1-angka2;
}
text3.setText(Integer.toString(hasil));
}
}

Iklan

One response to “belajar java part 3 pertemuan 4

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s