I'm a newbie to java, i just want to make a simple JFrame which takes two numbers as input through JtextFields, sum them and return the answer through JLabel... but it simply doesn't do it what it suppose to do, here is my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.util.*;
public class gUi extends JFrame {
private JLabel lbl;
private JLabel lbl2;
private JLabel lbl3;
private JTextField tb;
private JTextField tb1;
private int num1;
private int num2;
public gUi(){
super("Sum Dialog");
setLayout(new FlowLayout());
lbl = new JLabel("1st Number:");
add(lbl);
tb = new JTextField(15);
add(tb);
tb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event){
num1 = Integer.parseInt(tb.getText());
}
});
lbl2 = new JLabel("2nd Number");
add(lbl2);
tb1= new JTextField(15);
tb1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event ){
num2= Integer.parseInt(tb1.getText());
}
});
add(tb1);
int sum= num1+num2;
String ssum = Integer.toString(sum);
lbl3 = new JLabel();
lbl3.setText(ssum);
add(lbl3);
}
}
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
gUi g =new gUi();
g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
g.setVisible(true);
g.setSize(180, 160);
}
}
It Compiles Alright but not showing sum through JLabel when I put some intergers through Textfields... I am having this problem for a week and tried different things that I know so far but problem persists...
Have a look at the code below(includes comments):
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
/**
* Shows a Stage that has two text Fields
*
*/
@SuppressWarnings("serial")
public class UserInterface extends JFrame {
private JLabel label1;
private JLabel label2;
private JLabel label3;
private JTextField textField1;
private JTextField textField2;
/**
* Constructor
*/
public UserInterface() {
super("Sum Dialog");
// Layout
setLayout(new FlowLayout());
// 1st JLabel
label1 = new JLabel("1st Number:");
add(label1);
textField1 = new JTextField(15);
add(textField1);
// 2 second JLabel
label2 = new JLabel("2nd Number");
add(label2);
textField2 = new JTextField(15);
add(textField2);
// 3 third label
label3 = new JLabel();
label3.setSize(200, 30);
add(label3);
JButton button = new JButton("Calculate");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// calculate the sum when the Button is pressed
int sum = Integer.parseInt(textField2.getText()) + Integer.parseInt(textField2.getText());
label3.setText(Integer.toString(sum));
}
});
add(button);
}
/**
* Main method of the application
*
* @param args
*/
public static void main(String[] args) {
UserInterface main = new UserInterface();
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setVisible(true);
main.setSize(180, 160);
}
}