Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Beginner Requesting Others To Critique My Code

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner Requesting Others To Critique My Code

    Good morning, noon, and night to everyone,

    This is my first post. I apologize if this post is in the wrong place. I didn't see a section for a perfect fit.

    Anyway, I'm a new programmer. I'm still learning. Java has been a bit confusing for me to say the least. I get the syntax. That's easy. But Java has tons of APIs and three million ways of doing everything. Also I'm not finding any good documentation that lays out best practices. I've found guides that recommend various ways to do the exact same thing. So, what I would like, if anyone would be willing, is to take a look at my code and critique it. Don't be afraid to be harsh. I would really like to learn but it's hard to find my own faults when I don't know what I'm looking at.

    I really appreciate any help and I thank everyone in advanced for even reading this post. Thank you!

    [CODE]package KeyBoardEvent;
     
    public class KeyBoardEvent{
         public static void main(String[] args){
              KBEWindow window = new KBEWindow("Example Keyboard Event");
              while(true){
                   window.changeTextField();
              }
         }
    }[/CODE]


    package KeyBoardEvent;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class KBEWindow implements KeyListener{
     
         //Define class variables and objects
         JFrame frame;
         JPanel topPanel;
         JPanel bottomPanel;
         JLabel keyboardInput;
         JLabel quitMessage;
         JLabel spacer;
         JButton quit;
         String userInput = "Nothing Pressed yet.";
         int keyPressed = 0;
     
         public KBEWindow(String name){
     
              //Setup the Java frame
              frame = new JFrame(name);
              frame.setVisible(true);
              frame.setSize(300,100);
              frame.setFocusable(true);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.addKeyListener(this);
     
     
              //JTextField setup
              //This object displays the kayboard input on the screen
              keyboardInput = new JLabel(userInput);
              keyboardInput.setHorizontalAlignment(JLabel.CENTER);
              quitMessage = new JLabel("Press Esc to quit.");
              quitMessage.setHorizontalAlignment(JLabel.CENTER);
              spacer = new JLabel(" ");
     
              //Create quit button
              quit = new JButton(new AbstractAction("Quit"){
                   @Override
                   public void actionPerformed(ActionEvent e){
                        System.exit(0);
                   }
              });
     
              //Setup the Java panel
              //This panel will contain the text field that will display what the keyboard input is
              topPanel = new JPanel(new GridLayout(2,1));
              topPanel.add(keyboardInput);
              topPanel.add(quitMessage);
     
              bottomPanel = new JPanel(new GridLayout(1,3));
              bottomPanel.add(new JLabel(" "));
              bottomPanel.add(quit);
              bottomPanel.add(new JLabel(" "));
     
              //Add the panel (that includes the user input text) and the quit button to the window
              frame.add(topPanel, BorderLayout.NORTH);
              frame.add(bottomPanel, BorderLayout.SOUTH);
              //This was found to be necessary after a little bit of research. When the program was 
              //first launches the window would not paint the labels or button until the window
              //was resized. I had to validate the frame after adding the components to it
              frame.validate();
         }
     
         //This adjusts the JLabel to print out what is currently being pressed on the keyboard
         public void changeTextField(){
              keyboardInput.setText(userInput);
              if (keyPressed == 27)
                   System.exit(0);
     
         }
     
         //Override methods for KeyListener implementation
         public void keyPressed(KeyEvent e){
              userInput = "User Input: " + e.getKeyCode();
              keyPressed = e.getKeyCode();
         }
     
         //These two methods need to be written because of the implimentation
         //They are not needed though so I left them blank
         public void keyReleased(KeyEvent e){}
         public void keyTyped(KeyEvent e){}
    }


  2. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Requesting Others To Critique My Code

    Good morning everyone,

    This is my first post and I'm not sure this is in the right section. If it is not, I apologize and please let me know where to put it. I'm a new developer. I've been studying Java for the past couple of months and find some aspects of the language confusing. The Syntax and usage I understand. But There are tons of APIs and three million ways of accomplishing any task. A lot of the information I'm finding has also been spread out over the last ten years. I'm having trouble understanding if what I'm doing is proper. I'm also not finding any material that outlines best practices. If anyone is willing, I'm asking that people take a look at my code and critique it. Let me know anything that could be changed or done in a better way. I'd even like critiques on my comments and ways to make them more precise and concise.

    I thank everyone in advance for even looking at my post.

    package KeyBoardEvent;
     
    public class KeyBoardEvent{
         public static void main(String[] args){
              KBEWindow window = new KBEWindow("Example Keyboard Event");
              while(true){
                   window.changeTextField();
              }
         }
    }

    [highlight=java]
    package KeyBoardEvent;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class KBEWindow implements KeyListener{

    //Define class variables and objects
    JFrame frame;
    JPanel topPanel;
    JPanel bottomPanel;
    JLabel keyboardInput;
    JLabel quitMessage;
    JLabel spacer;
    JButton quit;
    String userInput = "Nothing Pressed yet.";
    int keyPressed = 0;

    public KBEWindow(String name){

    //Setup the Java frame
    frame = new JFrame(name);
    frame.setVisible(true);
    frame.setSize(300,100);
    frame.setFocusable(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.addKeyListener(this);


    //JTextField setup
    //This object displays the kayboard input on the screen
    keyboardInput = new JLabel(userInput);
    keyboardInput.setHorizontalAlignment(JLabel.CENTER );
    quitMessage = new JLabel("Press Esc to quit.");
    quitMessage.setHorizontalAlignment(JLabel.CENTER);
    spacer = new JLabel(" ");

    //Create quit button
    quit = new JButton(new AbstractAction("Quit"){
    @Override
    public void actionPerformed(ActionEvent e){
    System.exit(0);
    }
    });

    //Setup the Java panel
    //This panel will contain the text field that will display what the keyboard input is
    topPanel = new JPanel(new GridLayout(2,1));
    topPanel.add(keyboardInput);
    topPanel.add(quitMessage);

    bottomPanel = new JPanel(new GridLayout(1,3));
    bottomPanel.add(new JLabel(" "));
    bottomPanel.add(quit);
    bottomPanel.add(new JLabel(" "));

    //Add the panel (that includes the user input text) and the quit button to the window
    frame.add(topPanel, BorderLayout.NORTH);
    frame.add(bottomPanel, BorderLayout.SOUTH);
    //This was found to be necessary after a little bit of research. When the program was
    //first launches the window would not paint the labels or button until the window
    //was resized. I had to validate the frame after adding the components to it
    frame.validate();
    }

    //This adjusts the JLabel to print out what is currently being pressed on the keyboard
    public void changeTextField(){
    keyboardInput.setText(userInput);
    if (keyPressed == 27)
    System.exit(0);

    }

    //Override methods for KeyListener implementation
    public void keyPressed(KeyEvent e){
    userInput = "User Input: " + e.getKeyCode();
    keyPressed = e.getKeyCode();
    }

    //These two methods need to be written because of the implimentation
    //They are not needed though so I left them blank
    public void keyReleased(KeyEvent e){}
    public void keyTyped(KeyEvent e){}
    }
    [highlight]

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Requesting Others To Critique My Code

    Good morning everyone,

    I'm new here and a new developer. This is also my third time writing this post because I keep getting stuck in an edit loop for some reason in my browser. I'm not getting the code formatting to work properly. Also I didn't see a clear place for this post. If it should belong in a different section than please let me know. :-)

    At any rate I'm having a bit of trouble with the Java language. I understand the syntax and use well enough. But I'm having problems with the APIs and best practices. There seems to be 3 million different ways to do anything. I'm not finding answers to what is current and what is not. I'm also having trouble finding a clear outline for best practices. What I'm asking, if anyone is willing, is to critique my code and let me know what changes I can make, how things could best be done, and what I'm doing wrong. I would really appreciate any help.

    I thank everyone for even taking the time to read this post.

    package KeyBoardEvent;
     
    public class KeyBoardEvent{
         public static void main(String[] args){
              KBEWindow window = new KBEWindow("Example Keyboard Event");
              while(true){
                   window.changeTextField();
              }
         }
    }
     
    package KeyBoardEvent;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class KBEWindow implements KeyListener{
     
         //Define class variables and objects
         JFrame frame;
         JPanel topPanel;
         JPanel bottomPanel;
         JLabel keyboardInput;
         JLabel quitMessage;
         JLabel spacer;
         JButton quit;
         String userInput = "Nothing Pressed yet.";
         int keyPressed = 0;
     
         public KBEWindow(String name){
     
              //Setup the Java frame
              frame = new JFrame(name);
              frame.setVisible(true);
              frame.setSize(300,100);
              frame.setFocusable(true);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.addKeyListener(this);
     
     
              //JTextField setup
              //This object displays the kayboard input on the screen
              keyboardInput = new JLabel(userInput);
              keyboardInput.setHorizontalAlignment(JLabel.CENTER);
              quitMessage = new JLabel("Press Esc to quit.");
              quitMessage.setHorizontalAlignment(JLabel.CENTER);
              spacer = new JLabel(" ");
     
              //Create quit button
              quit = new JButton(new AbstractAction("Quit"){
                   @Override
                   public void actionPerformed(ActionEvent e){
                        System.exit(0);
                   }
              });
     
              //Setup the Java panel
              //This panel will contain the text field that will display what the keyboard input is
              topPanel = new JPanel(new GridLayout(2,1));
              topPanel.add(keyboardInput);
              topPanel.add(quitMessage);
     
              bottomPanel = new JPanel(new GridLayout(1,3));
              bottomPanel.add(new JLabel(" "));
              bottomPanel.add(quit);
              bottomPanel.add(new JLabel(" "));
     
              //Add the panel (that includes the user input text) and the quit button to the window
              frame.add(topPanel, BorderLayout.NORTH);
              frame.add(bottomPanel, BorderLayout.SOUTH);
              //This was found to be necessary after a little bit of research. When the program was 
              //first launches the window would not paint the labels or button until the window
              //was resized. I had to validate the frame after adding the components to it
              frame.validate();
         }
     
         //This adjusts the JLabel to print out what is currently being pressed on the keyboard
         public void changeTextField(){
              keyboardInput.setText(userInput);
              if (keyPressed == 27)
                   System.exit(0);
     
         }
     
         //Override methods for KeyListener implementation
         public void keyPressed(KeyEvent e){
              userInput = "User Input: " + e.getKeyCode();
              keyPressed = e.getKeyCode();
         }
     
         //These two methods need to be written because of the implimentation
         //They are not needed though so I left them blank
         public void keyReleased(KeyEvent e){}
         public void keyTyped(KeyEvent e){}
    }

  4. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Requesting Others To Critique My Code

    Good morning everyone,

    I'm new here and a new developer. This is also my third time writing this post because I keep getting stuck in an edit loop for some reason in my browser. I'm not getting the code formatting to work properly. Also I didn't see a clear place for this post. If it should belong in a different section than please let me know. :-)

    At any rate I'm having a bit of trouble with the Java language. I understand the syntax and use well enough. But I'm having problems with the APIs and best practices. There seems to be 3 million different ways to do anything. I'm not finding answers to what is current and what is not. I'm also having trouble finding a clear outline for best practices. What I'm asking, if anyone is willing, is to critique my code and let me know what changes I can make, how things could best be done, and what I'm doing wrong. I would really appreciate any help.

    I thank everyone for even taking the time to read this post.

    package KeyBoardEvent;
     
    public class KeyBoardEvent{
         public static void main(String[] args){
              KBEWindow window = new KBEWindow("Example Keyboard Event");
              while(true){
                   window.changeTextField();
              }
         }
    }
     
    package KeyBoardEvent;
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class KBEWindow implements KeyListener{
     
         //Define class variables and objects
         JFrame frame;
         JPanel topPanel;
         JPanel bottomPanel;
         JLabel keyboardInput;
         JLabel quitMessage;
         JLabel spacer;
         JButton quit;
         String userInput = "Nothing Pressed yet.";
         int keyPressed = 0;
     
         public KBEWindow(String name){
     
              //Setup the Java frame
              frame = new JFrame(name);
              frame.setVisible(true);
              frame.setSize(300,100);
              frame.setFocusable(true);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.addKeyListener(this);
     
     
              //JTextField setup
              //This object displays the kayboard input on the screen
              keyboardInput = new JLabel(userInput);
              keyboardInput.setHorizontalAlignment(JLabel.CENTER);
              quitMessage = new JLabel("Press Esc to quit.");
              quitMessage.setHorizontalAlignment(JLabel.CENTER);
              spacer = new JLabel(" ");
     
              //Create quit button
              quit = new JButton(new AbstractAction("Quit"){
                   @Override
                   public void actionPerformed(ActionEvent e){
                        System.exit(0);
                   }
              });
     
              //Setup the Java panel
              //This panel will contain the text field that will display what the keyboard input is
              topPanel = new JPanel(new GridLayout(2,1));
              topPanel.add(keyboardInput);
              topPanel.add(quitMessage);
     
              bottomPanel = new JPanel(new GridLayout(1,3));
              bottomPanel.add(new JLabel(" "));
              bottomPanel.add(quit);
              bottomPanel.add(new JLabel(" "));
     
              //Add the panel (that includes the user input text) and the quit button to the window
              frame.add(topPanel, BorderLayout.NORTH);
              frame.add(bottomPanel, BorderLayout.SOUTH);
              //This was found to be necessary after a little bit of research. When the program was 
              //first launches the window would not paint the labels or button until the window
              //was resized. I had to validate the frame after adding the components to it
              frame.validate();
         }
     
         //This adjusts the JLabel to print out what is currently being pressed on the keyboard
         public void changeTextField(){
              keyboardInput.setText(userInput);
              if (keyPressed == 27)
                   System.exit(0);
     
         }
     
         //Override methods for KeyListener implementation
         public void keyPressed(KeyEvent e){
              userInput = "User Input: " + e.getKeyCode();
              keyPressed = e.getKeyCode();
         }
     
         //These two methods need to be written because of the implimentation
         //They are not needed though so I left them blank
         public void keyReleased(KeyEvent e){}
         public void keyTyped(KeyEvent e){}
    }

Similar Threads

  1. [SOLVED] [Homework] Requesting User Input
    By nKulo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 24th, 2013, 12:42
  2. Requesting help.
    By narf0708 in forum Object Oriented Programming
    Replies: 1
    Last Post: July 6th, 2012, 17:04
  3. Replies: 4
    Last Post: May 12th, 2012, 09:53
  4. Game requesting user input
    By Neo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 3rd, 2011, 21:00
  5. Critique Java Game: Help Me Improve
    By gretty in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 21st, 2010, 19:49