Monday, March 2, 2009

Sample java program using JFrame, JButton and JRadioButton.

This simple program using JFrame in java allows user to multiply, Add, Subtract and Divide numbers just enter your number in the first and second text and select your operation and just simply click the button 'Show Answer' and clearing the value inside the Textbox just simply click 'Clear' button.














import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class JSample extends JFrame implements ActionListener {
JButton b1;
JButton b2;
JRadioButton r1;
JRadioButton r2;
JRadioButton r3;
JRadioButton r4;
JCheckBox c1;
JTextField txt1;
JTextField txt2;
public JSample() {
Container p = getContentPane();
JPanel jp = new JPanel();
ButtonGroup bg = new ButtonGroup();
p.add(jp);

bg.add(r1=new JRadioButton("Mutiplication"));
bg.add(r2=new JRadioButton("Addtion"));
bg.add(r3=new JRadioButton("Subtraction"));
bg.add(r4=new JRadioButton("Division"));
jp.add(r1);
jp.add(r2);
jp.add(r3);
jp.add(r4);
jp.add(txt1=new JTextField(5));
jp.add(txt2=new JTextField(5));
jp.add(b1=new JButton("Show Answer"));
jp.add(b2=new JButton("Clear"));
b1.addActionListener(this);
b2.addActionListener(this);
}
public static void main(String[] args) {
JSample js = new JSample();
js.setVisible(true);
js.setSize(400,100);
js.setTitle("Basic Aritmetic Operations - *, +, -, /");
js.setResizable(false);
}
public void actionPerformed(ActionEvent e) {
double ans=0.0;
JOptionPane j = new JOptionPane();
if(e.getSource() == b1) {
if(r1.isSelected()) {
ans=Double.parseDouble(txt2.getText()) * Double.parseDouble(txt1.getText());
j.showMessageDialog(null,"Answer: "+ ans);
}else if(r2.isSelected()) {
ans=Double.parseDouble(txt2.getText()) + Double.parseDouble(txt1.getText());
j.showMessageDialog(null,"Answer: "+ ans);
}else if(r3.isSelected()) {
ans=Double.parseDouble(txt2.getText()) - Double.parseDouble(txt1.getText());
j.showMessageDialog(null,"Answer: "+ ans);
}else if(r4.isSelected()) {
ans=Double.parseDouble(txt2.getText()) / Double.parseDouble(txt1.getText());
j.showMessageDialog(null,"Answer: "+ ans);
}else {
j.showMessageDialog(null,"Select first Operation","Error",JOptionPane.ERROR_MESSAGE);
txt1.setText("");
txt2.setText("");
}
}else if(e.getSource() == b2) {
txt1.setText("");
txt2.setText("");
}
}
}



You can download my example below.


Download File


18 comments:

  1. hmm let me try ..^^

    ReplyDelete
  2. program using all the Jcomponent

    ReplyDelete
  3. cool... can u give me the code for adding textbox or textfield only?

    please email me at pskillua@gmail.com


    ASAP!!

    ReplyDelete
  4. Programming is not easy need more study and time.
    Yes i'll send code

    ReplyDelete
    Replies
    1. are u the administrator of this blog?
      Can you please write a code for my project Voting System!!!
      TNX and more POWER!

      Delete
  5. program using JFrame & JMenu?

    ReplyDelete
  6. can u say me how to write action performed for displaying questions and its options which is retrieved from mysql.i did but i could display for one question only.If got any.then jus mail to ranjani.vtu@gmail.com

    {
    Class.forName(driverName).newInstance();
    con = DriverManager.getConnection(url+dbName, userName, password);
    Statement st = con.createStatement();
    ResultSet rs1 = st.executeQuery(query2);
    while(rs1.next())
    {

    if(evt.getSource()==jButton1)
    {
    removeAll();
    //c++;
    //jButton1.setText("clicked " + c + " times!");
    String d1=rs1.getString(1);
    String d2=rs1.getString(2);
    String d3=rs1.getString(3);
    String d4=rs1.getString(4);
    String d5=rs1.getString(5);
    //String d5=rs1.getString(5);
    jLabel1.setText(d1);
    jLabel2.setText(d2);
    //jTextArea1.revalidate();
    jRadioButton1.setText(d3);
    jRadioButton2.setText(d4);
    jRadioButton3.setText(d5);


    }
    }

    rs1.close();
    st.close();
    con.close();
    }

    ReplyDelete
  7. hi, i have a problem with jradiobutton. how can i get the image portrayed by the selected jradiobutton? for example i have 3 jradiobutton and each has a string name and picture. like dog, cat and bird. now what i whant to do is get the selected jradiobutton and send the image to another window. is it possible?

    ReplyDelete
    Replies
    1. Hi, I think I already have a program which is similar to this problem. . Check this out:

      http://java-program-sample.blogspot.com/2011/08/display-image-to-another-window-using.html

      Delete
  8. Nice blog, I like how you did the codes because it looks more simple and easy to understand. I would like to share also some of my java works. .

    http://java-program-sample.blogspot.com

    ReplyDelete
  9. There is a very simple way on how to do your program. I'll post it in here this week. I will also solve the problem of "Anonymous" who posted on January 11, 2011 3:13 AM about JRadioButton.

    ReplyDelete
  10. Can anybody tell me how can i design a grid on an image ? (that is i want to divide my picture into cells)

    ReplyDelete
  11. Can someone send me more examples like this one. I don't rely need the code, i need some ideas what can i practice for my exams (they are similar to these tasks +/-)

    ReplyDelete
    Replies
    1. http://java-program-sample.blogspot.com/2011/08/basic-arithmetic-operation-using.html

      Delete
  12. Excellent Example

    ReplyDelete
  13. What a great presentation! I agree with you that each could be its own lesson for students and they would be engaging and fun! Actually, I start my next semester of courses in April and this would be a great introduction lesson to get my adult students speaking!
    presentations

    ReplyDelete

 

blogger templates | Make Money Online