Java Programming Guide [WORK-IN-PROGRESS]
Table of Contents: {You can use CTRL+F to search sections!}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 javax.swing.*;
public class ExampleClass extends JFrame {
}
import javax.swing.*;
public class ExampleClass extends JFrame {
public static void main(String args[]) {
}
}
import javax.swing.*;
public class ExampleClass extends JFrame {
public static void main(String args[]) {
new ExampleClass();
}
}
import javax.swing.*;
public class ExampleClass extends JFrame {
public ExampleClass() {
}
public static void main(String args[]) {
new ExampleClass();
}
}
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();
}
}
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:
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();
}
}
public void actionPerformed(ActionEvent e) {
}
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();
}
}
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:
[other code]
setLayout(new FlowLayout());
}
public ExampleClass() { ...
[other code]
add(exampleButton);
exampleButton.addActionListener(this);
exampleButton.setActionCommand("exampleCommand");
}
if("exampleCommand".equals(e.getActionCommand())) {
//Do stuff
}
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();
}
}
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:
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