1 / 15

Creating a Simple Java GUI using Java Button

Learn how to create a basic Java GUI by invoking the abstract window toolkit and Swing libraries. Follow along with the provided classes to design your own Java GUI easily.

brownee
Download Presentation

Creating a Simple Java GUI using Java Button

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lesson 27: Introduction to the Java GUI

  2. 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(); } }

  3. 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

  4. 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”

  5. 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.

  6. 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

  7. 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).

  8. 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.

  9. 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.

  10. The result The source file Compiling and running the program The output

  11. 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.

  12. 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.

  13. 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.

  14. The new result The source file Compiling and running the program The output

  15. 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.

More Related