Thursday, April 3, 2014

JAVA GUI

Java GUI programming involves two packages: 1) the original abstract windows kit                      (AWT)
                                                                                   2)and the Swing toolkit.









Architechture:



Simple Program
The code in Listing 1 creates an empty frame. The title of the frame ("Example 1") is set in the call to the constructor. A frame is initially invisible and must be made visible by invoking its show() method.
import java.awt.*;
public class Example1
{
   public static void main(String [] args)
   {
      Frame f = new Frame("Example 1");
      f.show();
   }
}
·        Because the Java programming language is platform-independent, the AWT must also be platform-independent.

·        The AWT was designed to provide a common set of tools for graphical user interface design that work on a variety of platforms.


·        The user interface elements provided by the AWT are implemented using each platform's native GUI toolkit, thereby preserving the look and feel of each platform. This is one of the AWT's strongest points.

·         The disadvantage of such an approach is the fact that a graphical user interface designed on one platform may look different when displayed on another platform.


Swing:

·        Swing components have the prefix J to distinguish them from the original AWT ones (e.g. JFrame instead of Frame).

·        must import the java.awt.*, java.awt.event.*, andjavax.swing.* packages.


·        Displayable frames are top-level containers such as JFrame, JWindows, JDialog, andJApplet, which interface with the operating system's window manager.

·        Non-displaying content panes are intermediate containers such as JPanel, JOptionsPane, JScrollPane, and JSplitPane.

·         Containers are therefore widgets or GUI controls that are used to hold and group other widgets such as text boxes, check boxes, radio buttons, et al.

·        In Swing, there are three types of windows: the Applet, the Dialog, and the Frame. These interface with the windows manager. In swing, a frame object is called aJFrame.

·        A JFrame is considered the top most container.

Simple Program
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
 
public class SimpleExample extends JFrame {
 
    public SimpleExample() {
        
       setTitle("Simple example");
       setSize(300, 200);
       setLocationRelativeTo(null);
       setDefaultCloseOperation(EXIT_ON_CLOSE);        
    }
    
 
    public static void main(String[] args) {
        
                SimpleExample ex = new SimpleExample();
                ex.setVisible(true);
            }
        });
    }
}
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
Here we import Swing classes that will be used in the code example.
public class Example extends JFrame {
The Example class inherits from the JFrame widget. JFrame is a toplevel container. In the container, we put other widgets.
setTitle("Simple example");
Here we set the title of the window using the setTitle() method.
setSize(300, 200);
This code will resize the window to be 300px wide and 200px tall.
setLocationRelativeTo(null);
This line will center the window on the screen.
setDefaultCloseOperation(EXIT_ON_CLOSE);
This method will close the window, if we click on the close button of the titlebar. By default nothing happens.
{
        SimpleExample ex = new SimpleExample();
        ex.setVisible(true);
    }
 
 
 
 
output

Steps:


Method 1of 2.Making the Overall Frame


Create a class that extends the JFrame class. This class will hold all of your GUI components, such as buttons and textfields.


Plan the overall layout of your first application. A good start could be a centralpanel with BorderLayout with another panel at the bottom (BorderLayout.South). This second panel may have the FlowLayout and contain several buttons, check boxes and other similar controls. Finally, place the big JTextArea into the center of the central component. You will be able to use its getText() and setText() methods to do some text-based interaction with the user.

Write constructor to your class. 
This constructor must create all panels and components you plan, place them properly into each other and add the final panel the "holds all" to you frame (myFrame.getContentPane().add(myLargePanel, BorderLayout.Center).
Image:Create a Swing GUI in Java Step 4.jpg
Write the main method which will be the program's entry point. In this method, create an instance of your frame, set the initial size and location (use .setSize(x,y)and .setLocation(width, height) ) and make it to appear on the screen by calling.setVisible(true).
Method 2 of 2: Programming responses to the user actions

Make your frame implement the ActionListener interface.
 This will allow your class to listen to components' activities
Image:Create a Swing GUI in Java Step 6.jpg

For every button, check box or other control that you have created, invoke its method .addActionListener, passing your frame (this) as parameter.
Override ActionListener's abstract method, actionPerformed(ActionEvent event). 
In this method, you should put if statements checking where does the action event come from. This if statement should have a condition that says something like "if (event.getSource() == button1)". This checks where the event came from and if it came from your button. Inside the if statement, do whatever needs to be done when your button is pressed.
Image:Create a Swing GUI in Java Step 7.jpg

JTextArea has a method .setText("myText") which seems good as the way to program some visible response on your action.

No comments:

Post a Comment