Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lesson 27: Introduction to the Java GUI The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; 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. class HelloButton{ While we can code this ourselves, this saves us a lot of work. 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.*; Our own class. The file it is stored in must therefore be called HelloButton.java. 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(); Our main section. This means that frame.show(); 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 // helloworldbutton.java import java.awt.*; import javax.swing.*; Here we create a java frame (JFrame), also known as a window. The JFrame is available since we imported the Swing library. class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); This is the title of the frame JButton hello = new JButton("HelloWorld"); pane.add(hello); frame.pack(); frame.show(); } } This is a constructor statement. We identify it through the keyword “new” The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; 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. 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(); } } 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.*; Here we create a Javax.swing.JButton object and give it a string which is used as a label. 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 object is called hello The Java Button // helloworldbutton.java import java.awt.*; import javax.swing.*; 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). 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(); } } 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.*; The pack method from JFrame is used to minimize the window to smallest size that can accommodate the content of the container 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(); } } 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.*; The show method from JFrame is used to display the frame. 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 result The source file Compiling and running the program The output The Java Button // helloworldbutton.java The Pack method caused the frame to cut off the label of the frame. 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 The Pack method caused the frame to cut off the label of the frame. 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 We can set the size of the frame by using the method setSize from JFrame. 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(); } } This method accepts 2 parameters (width, height), which passed in terms of number of pixels. The new result The source file The output Compiling and running the program 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). 1. Write the psudocode and the deployment diagram for what you are planning to do 2. 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. 3. 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.