Results 1 to 5 of 5
- 07-16-2011, 08:41 AM #1
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
how to enhance code from extension class file using constructor
hello guys is there another way to improve this code from extension class using constructor?
the code above is working but i want to access usrnm,pwd and btnJava Code:new Main_extension(usrnm,pwd,btn);
using "new Main_extension();" not "new Main_extension(usrnm,pwd,btn);"
is there another way to do that? thanks...
Full Code Main.java and Main_extension.java
Main.java
Main_extension.javaJava Code:import javax.swing.*; public class Main extends JFrame{ private JTextField usrnm; private JPasswordField pwd; private JButton btn; public Main(){ usrnm = new JTextField(10); pwd = new JPasswordField("mypassword",10); btn = new JButton("Login"); add(usrnm); add(pwd); add(btn); new Main_extension(usrnm,pwd,btn); //<-- this work find setLayout(new java.awt.FlowLayout()); setBounds(10,10,400,300); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[]args){ new Main(); } }
Java Code:import javax.swing.*; import java.awt.event.*; public class Main_extension{ public Main_extension(final JTextField usrnm,final JPasswordField pwd,JButton btn){ btn.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ System.out.println("UserName: "+usrnm.getText()); System.out.println("Password: "+pwd.getText()); } }); } }
- 07-16-2011, 08:50 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Write a default constructor.
Some things to remember;
The compiler will provide a default constructor if you don't supply one which sets all objects to null, booleans to false, and numbers to 0 or 0.0
If one constructor is provided the compiler will no longer provide the above default constructor and you will be responsible for writing it if you want it.
Also, getting a reference to the object may be helpful.
- 07-17-2011, 12:57 AM #3
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
- 07-17-2011, 01:03 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
No, static variables should be avoided except for special cases. Rather than doing
DoJava Code:new ClassName();
Java Code:ClassName varName = new ClassName();
- 07-19-2011, 05:33 PM #5
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
Similar Threads
-
How do you make a file save with a default file extension?
By Dark in forum New To JavaReplies: 3Last Post: 06-21-2011, 04:51 PM -
Help with creating an instance of a class extension
By Inferno719 in forum New To JavaReplies: 19Last Post: 05-04-2011, 03:53 AM -
can someone help me extend my code so that it can enhance grey scale images
By jude87 in forum Advanced JavaReplies: 3Last Post: 04-09-2010, 04:06 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks