150 likes | 160 Views
Lesson 27: Introduction to the Java GUI. The Java Button. Invoking the graphics libraries called the abstract window toolkit and the swing. We will use the classes provided in these standard java libraries to create our Java GUI. While we can code this ourselves, this saves us a lot of work.
E N D
Lesson 27: Introduction to the Java GUI
The Java Button Invoking the graphics libraries called the abstract window toolkit and the swing. We will use the classes provided in these standard java libraries to create our Java GUI. While we can code this ourselves, this saves us a lot of work. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } }
The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } } Our own class. The file it is stored in must therefore be called HelloButton.java. Our main section. This means that this will be an executable file that we can call using the command “java HelloButton”. As all main sections must do, it accepts arguments
The Java Button Here we create a java frame (JFrame), also known as a window. The JFrame is available since we imported the Swing library. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } } This is the title of the frame This is a constructor statement. We identify it through the keyword “new”
The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } } A container is a component to which we can add other components. A JFrame is actually a container, but we should not add components directly to a JFrame. Instead, we should add all components to a special container in the JFrame. We use the method getContentPane() to get a reference to JFrame’s special container.
The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } } Here we create a Javax.swing.JButton object and give it a string which is used as a label. The object is called hello
The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } } The method add in the class Container is used to add a component to the JFrame’s content pane (later we will add many components to a frame). Note: For now, we do not tell the container how to arrange this component, nor where this component is to be displayed (we will do this later).
The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } } The pack method from JFrame is used to minimize the window to smallest size that can accommodate the content of the container Since we have only one component in the container, the window will be minimized to the size of the button, which is determined by the size of the label.
The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } } The show method from JFrame is used to display the frame.
The result The source file Compiling and running the program The output
The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } } The Pack method caused the frame to cut off the label of the frame.
The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } } The Pack method caused the frame to cut off the label of the frame.
The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.setSize(300,200); frame.show(); } } We can set the size of the frame by using the method setSize from JFrame. This method accepts 2 parameters (width, height), which passed in terms of number of pixels.
The new result The source file Compiling and running the program The output
Exercise # 5 Complete exercise 5 of the Java code handed out in-class. Upload your *.java file (not the class file) on BlackBoard by Friday March 21st 2003 (10 am). • Write the psudocode and the deployment diagram for what you are planning to do • Separate the Ball class into 2 classes: • Ball.java that known the location, its size and how to paint itself • MovableBall.java that extends the class Ball and adds all behaviors related to motion. Such as the data fields dx and dy, the functions setMotions, move and so on. • Rewrite the MultiBallWorld to use an instance of MovableBall. IMPORTANT: Don’t built on what we did in exercise #4 or earlier exercises. Instead use the original files for Ball and MultiBallWorld posted on BlackBoard.