Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

How do get a Java program containing an Input Box and a Button to take a number from the Input Box and do something with it (eg. store it in a variable) when the Button is clicked?

OpenStudy (anonymous):

import javax.swing.JOptionPane; class abc { public static void main(String arg[]) { // input box to take input numbers from user String fn=JOptionPane.showInputDialog("Enter first number"); String sn=JOptionPane.showInputDialog("Enter fsecond number"); int num1=Integer.parseInt(fn); // convert into int int num2=Integer.parseInt(sn); // int num3=num1+num2; JOptionPane.showMessageDialog(null,"the result is :"+num3,"title of message box",joptionPane.PLAIN_MESSAGE); // displays result in message box } } NOTE : INPUT BOX TAKES EVERY INPUT IN STRING FORMAT , SO YOU NEED TO CONVERT IT INTO YOUR DESIRED DATATYPE AS I HAVE DONE ABOVE TO CONVERT IN INTEGER.

OpenStudy (anonymous):

Thanks tuhinrawat90 that's great, just what I needed. Now how can I get multiple input boxes and buttons all into the same window? So you can see all input boxes and buttons at once? Or is that too complicated to explain at the moment?

OpenStudy (anonymous):

for this you can use IDE like netbeans and eclipse which give you a drag and drop interface but if you want all this stuff by coding give me little bit of time for writing the code :)

OpenStudy (anonymous):

Oh, I didn't know Eclipse had a drag-and-drop function.. How do you get that up and running? And if I were to do it by coding, could you give me a quick summary of how it would be done.. Like do you have to manually set the locations in X and Y or something..? Thanks :)

OpenStudy (anonymous):

And that wouldn't be using JOptionPane.showInputDialog would it?

OpenStudy (anonymous):

Java Swing uses layout managers for alignment in GUI. You can read up about it here. http://download.oracle.com/javase/tutorial/uiswing/layout/using.html It's far too extensive for me to describe here.

OpenStudy (anonymous):

m realy sorry eclipse does not have a drag and drop menu sorry i forgot for doing manually class abd extends JFrame //declare your fields private JTextfield t1; private JTextfield t2; private JTextfield t3; private JButton add; private JButton multiply; public abd() // initialize default constructor { super("title of your window"); setLayout(new FlowLayout()); add=new JButton("Addition",BorderLayout.NORTH); // this gives a caption to you //button and also the layout positon where it should be located it can be CENTER,WEST,SOUTH t1=new JTextField(10); // gives a size limit to your textbox add(add); // add function adds your items on JFrame add(t1); } // create a main class to call the constructor of the window class class main1 { public static void main(String arg[]) { abd ab=new abd(); // calls the constructor of class abd which will create your frame ab.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// it decides what will happen when you click cross button of your window. ab.setSize(200,200); // it sets the size of your window ab.setVisible(true; } } IT IS JUST A VERY SMALL PROGRAM TO SHOW YOU HOW TO CREATE A GUI AND NO EVENT HANDLING IS DONE

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!