جافا : GUI برنامج لحساب الجمع والطرح والضرب من اعداد الطالبة المبدعة داليا قلالوة
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
public class MyGUI {
public static class GUI extends JFrame implements ActionListener{
JPanel myPanel;
JTextField number1 ;
JTextField number2 ;
JLabel n1 ;
JLabel n2 ;
JLabel result ;
JLabel r ;
JButton add ;
JButton sub ;
JButton Mul;
public GUI(){
myPanel = new JPanel(new GridLayout(5 ,2, 20 ,20 ));
number1 = new JTextField(10);
number2 = new JTextField(10);
sub = new JButton("-");
add = new JButton("+");
Mul=new JButton("*");
result = new JLabel();
n1 = new JLabel("First number : ");
n2 = new JLabel("Second Number : ");
r = new JLabel("Result = ");
add.addActionListener(this);
sub.addActionListener(this);
Mul.addActionListener(this);
myPanel.add(n1);
myPanel.add(number1);
myPanel.add(n2);
myPanel.add(number2);
myPanel.add(r);
myPanel.add(result);
myPanel.add(add);
myPanel.add(sub);
myPanel.add(Mul);
this.add(myPanel);
}
public static void main(String[] args) {
GUI frame=new GUI();
frame.setTitle("Calculate");
frame.setSize(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent event) {
if(event.getSource()==add){
int num1=Integer.parseInt(number1.getText());
int num2=Integer.parseInt(number2.getText());
int sum=num1+num2;
result.setText(""+sum);
}
if(event.getSource()==sub){
int num1=Integer.parseInt(number1.getText());
int num2=Integer.parseInt(number2.getText());
int sub=num1-num2;
result.setText(""+sub);
}
if(event.getSource()==Mul){
int num1=Integer.parseInt(number1.getText());
int num2=Integer.parseInt(number2.getText());
int Mul=num1*num2;
result.setText(""+Mul);
}
}
}
}
0 التعليقات: