Code:
import javax.swing.*;import java.awt.*;
import java.awt.event.*;
public class GUISwingDemo extends JFrame implements ActionListener{
JPanel panel[] = new JPanel[3];
JLabel label[] = new JLabel[3];
JTextField tf[] = new JTextField[3];
JButton btnAdd = new JButton("ADD");
String lblCap[] = {"First Number","Second Number","Sum"};
int x;
public GUISwingDemo(){
setTitle("SwingDemo");
for(x=0;x<panel.length;x++)
panel[x] = new JPanel();
panel[0].setLayout(new GridLayout(3,2));
for(x=0;x<label.length;x++){
panel[0].add(label[x] = new JLabel(lblCap[x]));
panel[0].add(tf[x] = new JTextField(10));
label[x].setForeground(Color.RED);
label[x].setFont(new Font("Arial",3,20));
tf[x].setFont(new Font("Arial",3,20));
tf[x].setForeground(Color.BLUE);
}
btnAdd.addActionListener(this);
panel[1].add(btnAdd);
panel[2].setLayout(new BorderLayout());
panel[2].add(panel[0],BorderLayout.NORTH);
panel[2].add(panel[1],BorderLayout.SOUTH);
getContentPane().add(panel[2]);
setVisible(true);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource().equals(btnAdd)){
int n1 = Integer.parseInt(tf[0].getText());
int n2 = Integer.parseInt(tf[1].getText());
int sum = n1+n2;
tf[2].setText(String.valueOf(sum));
}
}
public static void main(String[] args){
new GUISwingDemo();
}
}
This code worked at school man. But it turned out to have 100errors after compiling.
Pwede pa check if mu run sa inyu compiler?
PS: oooooooops. edit edit edit. found the errors na >.< sorrrrrry