|
javaprince (m)
|
I HAVE DONE AS YOU SAID, I HAVE POSTED THE MODIFICATION FROM THE PREVIOUS PAGE. But I didn't have time to compile them.
@sbucareer I wrote the program and made the neccessary corrections to make the code compile. I will like to paste it here to save others from having to debug it. Hope you don't mind.
CODE
Class - MobilePhoneApplication
package Phone;
import javax.swing.UIManager; import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; import javax.swing.text.*;
public class MobilePhoneApplication extends JPanel{
private JPanel mainPanel; private JPanel display; private JPanel keyPads; private JPanel keyControls;
private JTextArea textArea;
private JButton call; private JButton end;
private ActionListener listener; private DocumentListener docuListen; private Document jDocument;
public MobilePhoneApplication (ActionListener listener, DocumentListener docuListen){ try{ UIManager.setLookAndFeel ( UIManager.getSystemLookAndFeelClassName() ); }catch(Exception e ){ }//End try this.listener = listener; this.docuListen = docuListen; mainPanel = new JPanel(); display = new JPanel(); keyPads = keyPads(); keyControls = new JPanel(); keyControls.setLayout ( new FlowLayout( ) ); call = new JButton( "Call"); end = new JButton ( "End" ); keyControls.add ( call ); keyControls.add ( end ); display.setLayout ( new GridLayout (1, 1)); textArea = new JTextArea(); textArea.setEditable ( false ); textArea.getDocument().addDocumentListener (docuListen); textArea.setForeground ( new Color (255, 50 , 10 )); textArea.setFont ( new Font ("Dialog", Font.BOLD, 34 )); textArea.setLineWrap(true);
display.add ( textArea ); mainPanel.setLayout( new GridLayout(3, 1) ); mainPanel.add ( display ); mainPanel.add ( keyControls ); mainPanel.add ( keyPads ); call.setActionCommand ( "Call" ); end.setActionCommand ("End" ); call.addActionListener (listener); end.addActionListener ( listener );
this.add( mainPanel );
}//End MobilePhoneApplication Constructor
private JPanel keyPads (){ String values[] = {"1", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "PQRS7", "TUV8", "WXYZ9", "*", "0+", "#" }; JPanel toReturn = new JPanel(); toReturn.setLayout (new GridLayout(4, 3)); JButton keys[] = new JButton[values.length]; for ( int index1 = 0; index1 < keys.length; index1++ ){ keys[index1] = new JButton ( values[index1] ); }//End for
for (int index = 0; index < values.length; index++){ //keys[index].setText( values[index] ); keys[index].setActionCommand(values[index]); keys[index].addActionListener(this.listener);
toReturn.add(keys[index]); }//End for return toReturn; }//End keyPads method public JTextArea getTextArea (){ return textArea; }//End getTextArea public void show(String string ){ if ( textArea.getText().length() != 9 ){ textArea.setText( textArea.getText()+string ); } }//End show public void defaultMenu ( boolean set){
call.setEnabled ( set ); end.setEnabled ( set ); }//End defaultMenu //Test if the TextArea has any test public boolean hasChar(){ jDocument = textArea.getDocument(); return ( jDocument.getLength() != 0 ); }//End hasChar public void clear (){ String text = textArea.getText(); text = null; textArea.setText (text ); }//End clear }//End MobilePhoneApplication
Class - MobilePhoneApplication
[color=#000099] package Phone;
import javax.swing.UIManager; import java.awt.event.*; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.event.*;
public class MobilePhoneApplicationGUI extends JApplet implements ActionListener, DocumentListener{
private MobilePhoneApplication mobile; public MobilePhoneApplicationGUI(){ mobile = new MobilePhoneApplication(this, this); mobile.defaultMenu(false); this.init(); }//End MobilePhoneApplicationGUI
public void init(){
this.getContentPane().add(mobile); }//End init
public void actionPerformed (ActionEvent event ){ String source = event.getActionCommand(); if( source.equals ( "Call" )){ mobile.clear(); mobile.show("Calling, "); }else if (source.equals ( "End" )){ mobile.clear(); mobile.defaultMenu(false); }else if ( source.equals ( "1" )){ mobile.show("1"); }else if ( source.equals ( "ABC2" )){ mobile.show("A"); }else if ( source.equals ( "DEF3" )){ }else if ( source.equals ( "GHI4" ) ){ mobile.show("G"); }else if ( source.equals ( "JKL5" ) ){ mobile.show("J"); }else if ( source.equals ( "MNO6" )){ mobile.show("M"); }else if ( source.equals ( "PQRS7") ){ mobile.show("P"); }else if (source.equals ("TUV8")){ mobile.show("T"); }else if ( source.equals ( "WXYZ9")){ mobile.show("W"); }else if (source.equals ("*" )){ mobile.show("*"); }else if ( source.equals ("0+" )){ mobile.show("0"); }else if (source.equals ( "#")){ }else{ }//End if
}//End actionPerformed public void changedUpdate (DocumentEvent e ){ }//End changedUpdate public void insertUpdate (DocumentEvent e){ if ( mobile.hasChar() ) mobile.defaultMenu(true); }//End insertUpdate public void removeUpdate (DocumentEvent e ){ }//End removeUpdate
public static void main (String args[]){
JFrame frame = new JFrame("Mobile Phone"); MobilePhoneApplicationGUI demo = new MobilePhoneApplicationGUI(); frame.getContentPane().add (demo); frame.setSize(230, 600); frame.setLocation(550, 100); frame.setResizable(false); frame.setVisible(true); frame.pack(); frame.show(); /*frame.addActionListener(new ActionListener (){ public void actionPerformed (ActionEvent e ){ System.exit(0); }}); */ frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); }//End main
}//End MobilePhoneApplicationGUI
|
|
|
|
|
|
javaprince (m)
|
Now for Now, the code actually does not display anything. Be waiting for your update, But meanwhile Can't we use Visual IDEs to design our Interfaces? 
|
|
|
|
|
|
sayhi2ay (m)
|
is variable declaration not automatically private ? why do we need to put private in front ?
|
|
|
|
|
|
|
|
sbucareer (f)
|
sayhi2ay
They are by default public identifier, try it yourself. Write a class in a pacakge and another class in a different package and create a TestCase class and try to access the attributes of each identifier without private clause.
Another reason is to make sure that JVM during the runtime encapsulation associate an attribute with its appropriate identifier. Tell us your answer.
|
|
|
|
|
|
sayhi2ay (m)
|
a testcase class also in a different package ?
|
|
|
|
|
|
sbucareer (f)
|
Yes. TestCase in a different package.
|
|
|
|
|
|
sayhi2ay (m)
|
automatically private!, try it yourself,
|
|
|
|
|
|
sbucareer (f)
|
In the MobilePhoneApplicationGUI class we have a method called actionPerformed(ActionEvent event) This is because any class that implements an interface promises to implement it supplied method.
In our case, we implemented ActionListener class. Its supplied a method called actionPerformed (ActionEvent e), so we must implement it ourselfs. for help consult Java API
This message is for our method in the MobilePhoneApplicationGUI. So you can copy the method and paste it over the one above.
public void actionPerformed (ActionEvent e){
}//End actionPerformed
|
|
|
|
|
|
sbucareer (f)
|
sayhi2ay
Ok, you have learned that yeah, and certain that a variable declared without an identifier is automatically declared private by the JVM. Thank you sayhi2ay for that info.
But remember, it is recommended to physical write and identifier to a variable to guarantee that its behaviour will remain consistant to the written program.
|
|
|
|
|
|
sayhi2ay (m)
|
sayhi2ay
Ok, you have learned that yeah, and certain that a variable declared without an identifier is automatically declared private by the JVM. Thank you sayhi2ay for that info.
without an "access specifier", anyways, its just programming style, nothing to it , you can always put "private" so people would know you really want it private,
|
|
|
|
|
|
malone5923 (m)
|
Just want to share this piece of code with Java enthusiast. It is about Desktop programming. Mind you this code is not compiled or tested. It was typed directly onto the forum message editor. If it doesn't compile when you copy and paste it, you would need to debug it.
package Phone;
import javax.swing.UIManager; import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.event.*; import javax.swing.text.*;
public class MobilePhoneApplication extends JPanel{
private JPanel mainPanel; private JPanel display; private JPanel keyPads; private JPanel keyControls;
private JTextArea textArea;
private JButton call; private JButton end;
private ActionListener listener; private DocumentListener docuListen; private Document jDocument;
public MobilePhoneApplication (ActionListener listener, DocumentListener docuListen){ try{ UIManager.setLookAndFeel ( UIManager.getSystemLookAndFeelClassName() ); }catch(Exception e ){ }//End try this.listener = listener; this.docuListen = docuListen; mainPanel = new JPanel(); display = new JPanel(); keyPads = keyPads(); keyControls = new JPanel(); keyControls.setLayout ( new FlowLayout( ) ); call = new JButton( "Call"); end = new JButton ( "End" ); keyControls.add ( call ); keyControls.add ( end ); display.setLayout ( new GridLayout (1, 1)); textArea = new JTextArea(); textArea.setEditable ( false ); textArea.getDocument().addDocumentListener (docuListen); textArea.setForeground ( new Color (255, 50 , 10 )); textArea.setFont ( new Font ("Dialog", Font.BOLD, 24 )); display.add ( textArea ); mainPanel.setLayout( new GridLayout(3, 1) ); mainPanel.add ( display ); mainPanel.add ( keyControls ); mainPanel.add ( keyPads ); call.setActionCommand ( "Call" ); end.setActionCommand ("End" ); call.addActionListener (listener); end.addActionListener ( listener );
this.add( mainPanel );
}//End MobilePhoneApplication Constructor
private JPanel keyPads (){ String values[] = {"1", "ABC2", "DEF3", "GHI4", "JKL5", "MNO6", "PQRS7", "TUV8", "WXYZ9", "*", "0+", "#" }; JPanel toReturn = new JPanel(); toReturn.setLayout (new GridLayout(4, 3)); JButton keys[] = new JButton[values.length]; for ( int index1 = 0; index1 < keys.length; index1++ ){ keys[index1] = new JButton ( values[index1] ); }//End for
for (int index = 0; index < values.length; index++){ //keys[index].setText( values[index] ); keys[index].setActionCommand(values[index]); keys[index].addActionListener(this.listener);
toReturn.add(keys[index]); }//End for return toReturn; }//End keyPads method public JTextArea getTextArea (){ return textArea; }//End getTextArea public void show(String string ){ textArea.setText( string ); }//End show public void defaultMenu ( boolean set){
call.setEnabled ( set ); end.setEnabled ( set ); }//End defaultMenu //Test if the TextArea has any test public boolean hasChar(){ jDocument = textArea.getDocument(); return ( jDocument.getLength() != 0 ); }//End hasChar public void clear (){ String text = textArea.getText(); text = null; textArea.setText (text ); }//End clear }//End MobilePhoneApplication
Sbucareer or javaprince if is not going to be much of a problem, can you please type the errors you might have received for the first code I just want to know if I can debug it. I should have done it myself but the computer I am currently using doesnt have a compiler. Well if is going to be a problem I can always wait till I get home and use my own computer(which has a compiler).
|
|
|
|
|
|
sbucareer (f)
|
malone5923,
Asfar as I know all the current codes above are fully debugged and fully functional. Copy and paste it in a text editor like Notepad or WordPad and save them as .java respectively and run.
|
|
|
|
|
|
malone5923 (m)
|
malone5923,
Asfar as I know all the current codes above are fully debugged and fully functional. Copy and paste it in a text editor like Notepad or WordPad and save them as .java respectively and run.
As I said earlier I dont have a compiler right now but I will appreciate it if you post the executable file I only have a runtime. Thanks.
|
|
|
|
|
|
sbucareer (f)
|
Here is the bytecode and a HTML file to run with AppletViwer
|
|
|
|
|
|
|
|
malone5923 (m)
|
I cant run the applet I used IE and Mozzila, am I missing something though I got an exception stating that the runtime could not locate the class file Note: I have downloaded the class, this are the errors. Please be patient with me. Java Plug-in 1.6.0_05 Using JRE version 1.6.0_05 Java HotSpot(TM) Client VM User home directory = C:\Documents and Settings\Administrator
---------------------------------------------------- c: clear console window f: finalize objects on finalization queue g: garbage collect h: display this help message l: dump classloader list m: print memory usage o: trigger logging p: reload proxy configuration q: hide console r: reload policy configuration s: dump system and deployment properties t: dump thread list v: dump thread stack x: clear classloader cache 0-5: set trace level to <n> ----------------------------------------------------
load: class MobilePhoneApplicationGUI.class not found. java.lang.ClassNotFoundException: MobilePhoneApplicationGUI.class at sun.applet.AppletClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.applet.AppletClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.applet.AppletClassLoader.loadCode(Unknown Source) at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.plugin.AppletViewer.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
|
|
|
|
|
|
|
|
malone5923 (m)
|
I do you create a package without a compiler  . If I am getting it all wrong please explain in details.
|
|
|
|
|
|
sbucareer (f)
|
I have explained it above. How to create a pacakge.
|
|
|
|
|
|
malone5923 (m)
|
Sorry, I just saw the modifications you made to your previous post, It wasnt much help why because as I stated earlier I don't have a Compiler to make changes to the java file. But the link worked well(I guess thats Ok). Thanks
|
|
|
|
|
|
|
|
sbucareer (f)
|
This is the MobilePhoneApplicationGUI
package Phone;
import javax.swing.UIManager; import java.awt.event.*; import javax.swing.JApplet; import javax.swing.JFrame; import javax.swing.event.*;
public class MobilePhoneApplicationGUI extends JApplet implements ActionListener, DocumentListener{
private MobilePhoneApplication mobile; public MobilePhoneApplicationGUI(){ mobile = new MobilePhoneApplication(this, this); mobile.defaultMenu(MobilePhoneApplication.FALSE); this.init(); }//End MobilePhoneApplicationGUI
public void init(){
this.getContentPane().add(mobile); }//End init
public void actionPerformed (ActionEvent event ){ String source = event.getActionCommand(); if( source.equals ( "Call" )){ mobile.clear(); mobile.show("connecting, "); mobile.enableCall(MobilePhoneApplication.FALSE); mobile.setButtons(MobilePhoneApplication.FALSE); mobile.enableEnd(MobilePhoneApplication.TRUE); }else if (source.equals ( "End" )){ mobile.clear(); mobile.defaultMenu(MobilePhoneApplication.FALSE); mobile.setButtons(MobilePhoneApplication.TRUE); }else if ( source.equals ( "1" )){ mobile.show("1"); }else if ( source.equals ( "ABC2" )){ mobile.show("2"); }else if ( source.equals ( "DEF3" )){ mobile.show("3"); }else if ( source.equals ( "GHI4" ) ){ mobile.show("4"); }else if ( source.equals ( "JKL5" ) ){ mobile.show("5"); }else if ( source.equals ( "MNO6" )){ mobile.show("6"); }else if ( source.equals ( "PQRS7") ){ mobile.show("7"); }else if (source.equals ("TUV8")){ mobile.show("8"); }else if ( source.equals ( "WXYZ9")){ mobile.show("9"); }else if (source.equals ("*" )){ mobile.show("*"); }else if ( source.equals ("0+" )){ mobile.show("0"); }else if (source.equals ( "#")){ mobile.show("#"); }else{ mobile.show("You stole this phone!"); }//End if
}//End actionPerformed public void changedUpdate (DocumentEvent e ){ }//End changedUpdate public void insertUpdate (DocumentEvent e){ if ( mobile.hasChar() ) mobile.defaultMenu(MobilePhoneApplication.TRUE); mobile.enableEnd(MobilePhoneApplication.FALSE); }//End insertUpdate public void removeUpdate (DocumentEvent e ){ }//End removeUpdate
public static void main (String args[]){
JFrame frame = new JFrame("Mobile Phone"); MobilePhoneApplicationGUI demo = new MobilePhoneApplicationGUI(); frame.getContentPane().add (demo); frame.setSize(230, 600); frame.setLocation(550, 100); frame.setResizable(MobilePhoneApplication.FALSE); frame.setVisible(MobilePhoneApplication.TRUE); frame.pack(); frame.show(); /*frame.addActionListener(new ActionListener (){ public void actionPerformed (ActionEvent e ){ System.exit(0); }}); */ frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); }//End main
}//End MobilePhoneApplicationGUI
|
|
|
|
|
|
|
|
|
|
javaprince (m)
|
sbu Have done has u sent.
|
|
|
|
|
|
sayhi2ay (m)
|
java genius!
if you keep privatizing all your declarations, then we wont be able to extend your classes and methods,
|
|
|
|
|
|
sbucareer (f)
|
sayhi2ay
You can always use a callback methods. That is the whole point of it, to stop your code looking like spaghetti.
Remember software cohession. Sorry, I am used to software developing to programming.
|
|
|
|
|
|
sayhi2ay (m)
|
and if you want to extend classes or methods in the same code ?
|
|
|
|
|
|
|
|
sayhi2ay (m)
|
if you are extending the attributes of a class to another class in the same code ? you are just calling a method in the code you wrote,
|
|
|
|
|
|
sbucareer (f)
|
You do not extend attributes of any class in Java instead you inherit the public attributes and methods of a super class or protected methods and attributes of the same class package.
private attributes makes your variable immutable by any inquiring accessor, you only expose them by using callbacks.Java calls them setter and getter, i.e let take a variable int qty
//callbacks int getQty(){return qty;} void setQty (int qty) {this.qty = qty;}
their first name must start with set for mutable and get immutable
|
|
|
|
|
|