Results 1 to 5 of 5
- 03-27-2008, 07:59 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 19
- Rep Power
- 0
[SOLVED] Confused by the textField
I'm just having a look at ways to input and retrieve a string from a text field.
I've got confused somewhere along the way now I get an exception thrown and my components are missing from the JFrame (Plus It won't setBounds)
Any help would be appreciated, Das code:
Cheers :{pJava Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ActionExample extends JFrame { Button goButton; TextField tField; public void ActionExample() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(0,0,200,200); Container c = getContentPane(); c.setLayout(new FlowLayout()); tField = new TextField("*Enter your text here*",35); goButton = new Button("Go"); c.add(tField); c.add(goButton); goButton.addActionListener ( new ActionListener() { public void actionPerformed( ActionEvent evt ) { repaint(); } } ); } public static void main(String args[]) { ActionExample ae = new ActionExample(); ae.setVisible(true); } public void paint(Graphics g) { g.drawString(tField.getText(),20,100); } }
- 03-27-2008, 11:51 PM #2
Java Code:Change public void ActionExample() to public ActionExample()
- 03-28-2008, 12:37 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 19
- Rep Power
- 0
Cheers, :{p
Nice one! I can't believe I missed that. I'm assuming it can't be void because it returns a string from the textField to the public void paint(Graphics g)
- 03-28-2008, 04:28 AM #4
it can't be void because it returns a string from the textField to the public void paint(Graphics g)
No. It doesn't return anything. To return something to the paint method the paint method would have to call it first, eg,
Actually, it's a constructor which is the only kind of method that does not have a return type in its method declaration. Java calls the constructor for class initialization. With the return type included, viz, void the method was never called.Java Code:public String getString { ... } public void paint(Graphics g) { ... String s = getString(); ... }
- 03-28-2008, 05:30 PM #5
Member
- Join Date
- Mar 2008
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
set focus to the textfield
By paty in forum New To JavaReplies: 2Last Post: 07-09-2010, 08:28 AM -
Allowing only numeric values in a TextField
By Java Tip in forum Java TipReplies: 0Last Post: 03-01-2008, 10:08 PM -
what does it mean:confused:
By sivasayanth in forum New To JavaReplies: 2Last Post: 01-12-2008, 04:52 AM -
JSP - getting value from a textfield
By Java Tip in forum Java TipReplies: 0Last Post: 12-01-2007, 08:58 PM -
alignment of textfield in awt
By nitinborge5 in forum New To JavaReplies: 2Last Post: 07-30-2007, 11:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks