Tuesday, November 24, 2015


Java Programming Guide [WORK-IN-PROGRESS]

Table of Contents: {You can use CTRL+F to search sections!}
  • Section I: Compilers
  • Section II: The Frame
  • Section III: Buttons
  • Section IV: Dialogs
  • Section V: Variables
  • Section VI: Text Boxes
  • Section VII: Opening More Frames Within a Frame
  • Section VIII: Applying Images
  • Section IX: Audio
  • Section X: Calculator Example
  • Section XI: Chat Room Example [NETWORKING]

  • Section I: Compilers
    Compilers are 'tools' used to convert syntax, which is like a "language" that a programming language uses to convert specific words into binary in order for the program to function. For these tutorials, we're going to use Compile Java.net Of course you can use other compilers of your choice or even complete IDEs like Eclipse, but this online compiler/IDE is perfect for just starting out.

    Section II: The Frame
    Now we're going to make our first frame in Java! First, before anything, when we start our java programming, we MUST import our libraries which allow us to use certain syntax (like mentioned earlier). The libraries we're using right now are:

  • import java.awt.*;
  • and:
  • import javax.swing.*;
  • Now that we imported our libraries, we're now going to make a class. We're going to name this class "ExampleClass":
    import java.awt.*;
    import javax.swing.*;

    public class ExampleClass extends JFrame {

    }



    The 'extends' syntax allows us to 'open' a "port" for us to begin coding our frame in a public function we're going to create. But BEFORE we create a public function, we need a MAIN function. A main function is REQUIRED for our program to run!
    import java.awt.*;
    import javax.swing.*;

    public class ExampleClass extends JFrame {

    public static void main(String args[]) {

    }

    }



    Inside our main function, were going to create our public function by entering "new 'CLASS_NAME'();" of course, replacing "CLASS_NAME" with what ever your class name is. We used "ExampleClass"

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

    public class ExampleClass extends JFrame {

    public static void main(String args[]) {

    new ExampleClass();

    }

    }



    Now we go above our main method and build our public function now that we "activated" it in our main function.

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

    public class ExampleClass extends JFrame {

    public ExampleClass() {

    }

    public static void main(String args[]) {

    new ExampleClass();

    }

    }



    Now finally, in our public function we built, we're going to put the most common frame settings. Be aware that I'm going to NOTE what each syntax does using "//". Using "//" is making a comment in your code, the compiler/IDE will IGNORE this, so it's VERY useful to leave notes in your code :D
    import java.awt.*;
    import javax.swing.*;

    public class ExampleClass extends JFrame {

    public ExampleClass() {

    setSize(600, 400); //Set frame size [setSize(x, y);]
    setVisible(true); //Make the frame visible
    setResizable(false); //Allow the frame to be re-sized
    getContentPane().setBackground(Color.black); //Change background colour, you can see different colours online (:
    setTitle("My First Frame"); //Set title of the frame
    setDefaultCloseOperation(EXIT_ON_CLOSE); //Make the frame close when you press the 'X' (Kills the task in the task manager)

    }

    public static void main(String args[]) {

    new ExampleClass();

    }

    }



    And there we have it...our FIRST frame coded in pure Java :D Now to run this code, if you're using "compilejava.net", hit "compile and execute", make sure there's no errors, then "download zip", open the zip file, and you'll see a ".jar" file which is your executable!
    Section III: Buttons

    In order for buttons to work, we need to implement a 'class extension' (I don't know what to call them) under the name of "action listener". An action listener, as you can probably tell, listens for 'actions', or, button presses that take place within the application. To implement an action listener, we use the following syntax:
  • public class ... implements ActionListener
  • We also need to import a new library:
  • import java.awt.event.*;
  • Now all of your code should look like this [I am using the previous code we made in section II]:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class ExampleClass extends JFrame implements ActionListener {

    public ExampleClass() {

    setSize(600, 400);
    setVisible(true);
    setResizable(false);
    getContentPane().setBackground(Color.black);
    setTitle("My First Frame");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public static void main(String args[]) {

    new ExampleClass();

    }

    }



    We are not done creating the action listener though. Action listeners require a whole function to record the events taking place. We code the following function anywhere in your code: (just not in an already existing function of course)


    public void actionPerformed(ActionEvent e) {

    }

    I am going to put this function under our public function like so:

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

    public class ExampleClass extends JFrame implements ActionListener {

    public ExampleClass() {

    setSize(600, 400);
    setVisible(true);
    setResizable(false);
    getContentPane().setBackground(Color.black);
    setTitle("My First Frame");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent e) {

    }

    public static void main(String args[]) {

    new ExampleClass();

    }

    }



    Before we start creating buttons, we're going to set a layout. I'm going to go over an automatic layout and a custom layout, which you can place the buttons anywhere you want, and resize them however you want.

    We're going to go over the automatic layout first, which is called "Flow Layout". In our public function, we set up the flow layout like this:

    public ExampleClass() {

    [other code]

    setLayout(new FlowLayout());

    }



    Now that we've created a layout, we can create our button object. Do this ABOVE your public function.

    JButton exampleButton = new JButton("Button Name");

    public ExampleClass() { ...



    After creating the button object, we must add it to the screen, attach an action listener so the button can correspond with the action listener function we created previously, and set a "command" to it so we can identify which button was pressed. We do this IN our public function.

    public ExampleClass() {

    [other code]

    add(exampleButton);
    exampleButton.addActionListener(this);
    exampleButton.setActionCommand("exampleCommand");

    }



    In our action listener function, we can check if the button is pressed by checking what command was pressed:

    public void actionPerformed(ActionEvent e) {

    if("exampleCommand".equals(e.getActionCommand())) {

    //Do stuff

    }



    Where it says "//Do stuff", you can do anything you want. For example, we can change the background color of the frame like this:
  • getContentPane().setBackground(Color.green);

  • HERE IS THE FULL CODE FOR THIS EXAMPLE:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;


    public class ExampleClass extends JFrame implements ActionListener {

    JButton exampleButton = new JButton("Button Name");

    public ExampleClass() {

    setSize(600, 400);
    setVisible(true);
    setResizable(false); getContentPane().setBackground(Color.black);
    setTitle("My First Frame");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(new FlowLayout());

    add(exampleButton);
    exampleButton.addActionListener(this);
    exampleButton.setActionCommand("exampleCommand");

    }

    public void actionPerformed(ActionEvent e) {

    if("exampleCommand".equals(e.getActionCommand())) {

    getContentPane().setBackground(Color.green);

    }

    }

    public static void main(String args[]) {

    new ExampleClass();

    }

    }



    Remember talking about placing the buttons wherever we want? We do that by CREATING OUR OWN layout. Go to where we put "setLayout(new FlowLayout());" and change it to "setLayout(null);"

    This declares that we have a blank layout. Now it's time to set the buttons wherever we want.

    Now, we just need to add one more line. In our public function, where we have the button's/buttons' settings, we add one more setting defining the x position, y position, width of the button, and height of the button:
  • exampleButton.setBounds(x, y, width, height);
  • EXAMPLE WITH CUSTOM LAYOUT:

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

    public class ExampleClass extends JFrame implements ActionListener {

    JButton exampleButton = new JButton("Button Name");

    public ExampleClass() {

    setSize(600, 400);
    setVisible(true);
    setResizable(false); getContentPane().setBackground(Color.black);
    setTitle("My First Frame");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(null);

    add(exampleButton);
    exampleButton.addActionListener(this);
    exampleButton.setActionCommand("exampleCommand");
    exampleButton.setBounds(200, 200, 25, 25);

    }

    public void actionPerformed(ActionEvent e) {

    if("exampleCommand".equals(e.getActionCommand())) {

    getContentPane().setBackground(Color.green);

    }

    }

    public static void main(String args[]) {

    new ExampleClass();

    }

    }

    No comments:

    Post a Comment