Hasilnya seperti ini :
import javax.swing.*;
import java.awt.event.*;
public class test extends JFrame {
JTextField data1=new JTextField (6);
JTextField data2=new JTextField (6);
JButton operasitambah=new JButton ("+");
JButton operasikurang=new JButton ("-");
JButton operasikali=new JButton ("x");
JButton operasibagi=new JButton ("/");
JTextField hasil=new JTextField (6);
JLabel txtdata1=new JLabel ("Data 1 :");
JLabel txtdata2=new JLabel ("Data 2 :");
JLabel txthasil=new JLabel ("Hasil : ");
JLabel txthiasan=new JLabel (".=====----------Kalkulator----------=====.");
JButton exit=new JButton ("EXIT");
public test() {
setTitle ("SIMPLE EVENT");
setLocation(200,100);
setSize(270,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void komponenVisual ()
{
getContentPane() .setLayout(null);
getContentPane() .add(data1);
data1.setBounds(10,35,50,20);
getContentPane() .add(data2);
data2.setBounds(60,35,50,20);
getContentPane() .add(txthiasan);
txthiasan.setBounds(20,0,400,10);
getContentPane() .add (txtdata1);
txtdata1.setBounds(8,13,50,20);
getContentPane() .add (txtdata2);
txtdata2.setBounds(60,13,50,20);
getContentPane() .add (txthasil);
txthasil.setBounds(160,10,50,20);
getContentPane() .add (operasitambah);
operasitambah.setBounds(5,60,50,20);
getContentPane() .add (operasikurang);
operasikurang.setBounds(5,120,50,20);
getContentPane() .add (operasikali);
operasikali.setBounds(5,80,50,20);
getContentPane() .add (operasibagi);
operasibagi.setBounds(5,100,50,20);
getContentPane() .add(hasil);
hasil.setBounds(160,30,50,20);
getContentPane() .add(exit);
exit.setBounds(160,185,90,20);
setVisible(true);
}
void AksiReaksi ()
{
data1.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
data2.requestFocus();
}
}
});
data2.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
operasitambah.requestFocus();
}
}
});
operasitambah.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
operasikurang.requestFocus();
}
}
});
operasikurang.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
operasikali.requestFocus();
}
}
});
operasikali.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
operasibagi.requestFocus();
}
}
});
operasibagi.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
exit.requestFocus();
}
}
});
operasitambah.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
int
x=Integer.parseInt(data1.getText());
int
y=Integer.parseInt(data2.getText());
String z=String.valueOf(x+y);
hasil.setText(z);
}
});
operasitambah.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
int
x=Integer.parseInt(data1.getText());
int
y=Integer.parseInt(data2.getText());
String z=String.valueOf(x+y);
hasil.setText(z);
}
}
});
operasikurang.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
int
x=Integer.parseInt(data1.getText());
int
y=Integer.parseInt(data2.getText());
String z=String.valueOf(x-y);
hasil.setText(z);
}
});
operasikurang.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
int
x=Integer.parseInt(data1.getText());
int
y=Integer.parseInt(data2.getText());
String z=String.valueOf(x-y);
hasil.setText(z);
}
}
});
operasikali.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
int
x=Integer.parseInt(data1.getText());
int
y=Integer.parseInt(data2.getText());
String z=String.valueOf(x*y);
hasil.setText(z);
}
});
operasikali.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
int
x=Integer.parseInt(data1.getText());
int
y=Integer.parseInt(data2.getText());
String z=String.valueOf(x*y);
hasil.setText(z);
}
}
});
operasibagi.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
int
x=Integer.parseInt(data1.getText());
int
y=Integer.parseInt(data2.getText());
String z=String.valueOf(x/y);
hasil.setText(z);
}
});
operasibagi.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
int
x=Integer.parseInt(data1.getText());
int
y=Integer.parseInt(data2.getText());
String z=String.valueOf(x/y);
hasil.setText(z);
}
}
});
exit.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
System.exit(0);
}
});
exit.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==e.VK_ENTER)
{
System.exit(0);
}
}
});
}
public static void main (String args[])
{
test e1=new test ();
e1.komponenVisual();
e1.AksiReaksi();
}
}
0 komentar:
Posting Komentar