Results 1 to 3 of 3
- 02-12-2010, 02:14 AM #1
Problem Reading in data from Text Field
So I couldn't get this exercise done even though I'm so close and I know it..
Basically need to create a GUI that has two text fields (input and output) and two buttons (double and triple)
You input a number into the input text field, press double or triple and voilą it prints out the answer in the output text field.
This is my first class which builds the GUI implements the action listeners etc.
And this is my second class that you pass the information obtained in the input box into, and it returns the answerJava Code:import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; /** * */ /** * @author Mark Walsh * */ public class MyCalculatorGui extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 1L; JTextField input = new JTextField(20); JTextField output = new JTextField(20); int result = 0; public MyCalculatorGui() { super("Gui Calculator"); Container c = getContentPane(); JPanel panel = new JPanel(); input.setText(""); input.setEditable(true); output.setEditable(false); JButton doubleButton = new JButton(); doubleButton.setText("Double"); doubleButton.addActionListener(this); JButton tripleButton = new JButton(); tripleButton.setText("Triple"); tripleButton.addActionListener(this); panel.add(input); panel.add(doubleButton); panel.add(tripleButton); panel.add(output); c.add(panel); setSize(450, 400); setVisible(true); setResizable(true); } public void actionPreformed(ActionEvent e){ if(e.getActionCommand().equals("Double")){ String s = input.getText(); int val = Integer.parseInt(s); int result = MathHelper.doubleInt(val); output.setText(""+result); } else if(e.getActionCommand().equals("triple")){ String s = input.getText(); int val = Integer.parseInt(s); int result = MathHelper.tripleInt(val); output.setText(""+result); } } /** * @param args */ public static void main(String[] args) { MyCalculatorGui MyGui = new MyCalculatorGui(); } }
Now the program runs but doesn't work to how I want it too. And its really getting to me now.Java Code:/** * @author Mark Walsh * */ public class MathHelper { /** * @param doubleInt .. info to be filled in */ public static int doubleInt(int inputNum) { return inputNum*2; } public static int tripleInt(int inputNum) { return inputNum*3; } }
I am rather new to the Swing and Awt packages
Can someone please even give me a good guidance to whats going wrong.
Cheers guys
-
- 02-12-2010, 02:29 AM #3
Omg..
I feel completely and utterly stupid. lol Major fail.
Let me show you my original copy and pasted code:
noticeJava Code:import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; /** * */ /** * @author Mark Walsh * */ public class MyCalculatorGui extends JFrame implements ActionListener{ /** * */ private static final long serialVersionUID = 1L; JTextField input = new JTextField(20); JTextField output = new JTextField(20); int result = 0; public MyCalculatorGui() { super("Gui Calculator"); Container c = getContentPane(); JPanel panel = new JPanel(); input.setText(""); input.setEditable(true); output.setEditable(false); JButton doubleButton = new JButton(); doubleButton.setText("Double"); doubleButton.addActionListener(this); JButton tripleButton = new JButton(); tripleButton.setText("Triple"); tripleButton.addActionListener(this); panel.add(input); panel.add(doubleButton); panel.add(tripleButton); panel.add(output); c.add(panel); setSize(450, 400); setVisible(true); setResizable(true); } public void actionPreformed(ActionEvent e){ if(e.getActionCommand().equals("Double")){ String s = input.getText(); int val = Integer.parseInt(s); int result = MathHelper.doubleInt(val); output.setText(""+result); } else if(e.getActionCommand().equals("triple")){ String s = input.getText(); int val = Integer.parseInt(s); int result = MathHelper.tripleInt(val); output.setText(""+result); } } /** * @param args */ public static void main(String[] args) { MyCalculatorGui MyGui = new MyCalculatorGui(); } @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }
Only started using Eclipse also.. Only Noticed that part after I pasted here. And me thinking I added it by accident deleted that part.Java Code:@Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub }
I removed that code and fixed the spelling error.. Works perfect.
:o
Talk about feeling like a complete newb :( :P
Similar Threads
-
Reading data from a text file
By Cheguvara in forum New To JavaReplies: 2Last Post: 02-02-2010, 02:33 PM -
Problem with text field patern
By Prashant.surwade in forum Advanced JavaReplies: 1Last Post: 09-05-2009, 02:45 AM -
problem with reading excel sheet data reading using poi libraries
By sandeepsai17 in forum New To JavaReplies: 5Last Post: 08-21-2009, 11:03 AM -
reading data from text file .. help plz
By Thug heart in forum New To JavaReplies: 7Last Post: 02-15-2009, 07:29 PM -
Problem in reading HTML input field while uploading file
By sudipanand in forum Java ServletReplies: 1Last Post: 11-27-2008, 09:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks