An Introduction to Java GUI Programming. Introduction. This article is meant for the individual who has little or no experience in Java GUI programming. As such, this paper will focus on the hierarchal tree structure that roots at the frame and goes into the content pane panel. The primary focus will then be on the button widget (control in . NET) and the corresponding method used to handle that event listener. Any . NET programmer will find these concepts extremely similar, except that the coding style requires more text is some cases and the terms used are different. Eclipse is probably best known as a Java IDE, but it is more: it is an IDE framework, a tools framework, an open source project, a community, an eco. Key Eclipse Platform improvements include HiDPI support and autosave.A quick and easy way to compile this code on the command line after installing Sun's Java Runtime at www. Sun's J2. EE SDK 5.
Sun\SDK\JDK\bin> type con > somecode. Ctrl- Z and then compile. To set your path: set PATH=%PATH%.; C: \Sun\SDK\JDK\bin. Graphical User Interface (GUI) vs Command Line Interface (CLI) - Duration. The Hello World Program 8,290 views. 2:28 Create Action Listeners with JButton - Java GUI/SWING Tutorial+HELP: - Duration: 8:13. Creating a Graphic User Interface (GUI) for your program. Java provides a hierarchy of classes useful in creating GUI’s. It’s useful to have an idea of how this hierarchy fits together, since classes inherit objects. Java GUI programming involves two packages: the original abstract windows kit (AWT) and the newer Swing toolkit. Swing components have the prefix J to distinguish them from the original AWT ones (e. JFrame instead of Frame). To include Swing components and methods in your project, you must import the java. Displayable frames are top- level containers such as JFrame, JWindows, JDialog, and JApplet, which interface with the operating system's window manager. Non- displaying content panes are intermediate containers such as JPanel, JOptions. Pane, JScroll. Pane, and JSplit. Pane. 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 . NET the main UI, called the Windows Form, holds the controls that are dragged and dropped onto the control surface. Every GUI starts with a window meant to display things. 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 a JFrame. A JFrame is considered the top most container. These are also called displayable frames. Non- displaying content panes are intermediate containers such as JPanel, JScroll. Pane, JLayered. Pane, JSplit. Pane and JTabbed. Pane which organize the layout structure when multiple controls are being used. Stated simply, the content pane is where we place out text fields are other widgets, so to add and display GUI controls, we need to specify that it is the content pane that we are adding to. The content pane is then at the top of a containment hierarchy, in which this tree- like hierarchy has a top- level container (in our case JFrame). Working down the tree, we would find other top level containers like JPanel to hold the components. Here is the code that produces a simple frame upon to build on: import java. Frame. 1 extends JFrame. As . NET compilers emit IL code and metadata, where the metadata tables are read by the CLR to verify type safety (that is, that the correct data types are passed to the correct methods), the JIT compiler converts the IL code into native code for execution. There is no interpretation as there is with the Java Virtual Machine. The Java platform is defined by the APIs (collections of compiled libraries for use programs and the JVM (which is similar to the CLR). A Java source code file is compiled into byte code wherein a class file is generated that functions as a blueprint for the runtime execution. Here is an example: import java. Sys . This is a string passed to the print line method of the System class. Here is code that show a GUI with a button. The button, however, does nothing when pressed: import java. Frame. 2 extends JFrame. Here is the output of the button press: import java. Frame. 3 extends JFrame implements Action. Listener. . Next add the phrase implements Action. Listener to the class header to use the interface. Register event listeners for each button widget using the add. Action. Listener(this) method. The reserved word this indicates that the required (by implements Action. Listener) handler method called action. Performed() will be included in the current class. For example, consider this more colorful example: import javax. Color. publicclass Button. GUI Programming - Java Programming Tutorial. Introduction. So far, we have covered most of the basic constructs of Java and introduced the important concept of Object- Oriented Programming (OOP). Writing your own graphics classes (re- inventing the wheels) will take you many years! These graphics classes, developed by expert programmers, are highly complex and involve many advanced Java concepts. I will describe another important concept called nested class (or inner class) in this article. There are two sets of Java APIs for graphics programming: AWT (Abstract Windowing Toolkit) and Swing. AWT API was introduced in JDK 1. JFC consists of Swing, Java. D, Accessibility, Internationalization, and Pluggable Look- and- Feel Support APIs. JFC was an add- on to JDK 1. Java since JDK 1. Other than AWT/Swing Graphics APIs provided in JDK, others have also provided Graphics APIs that work with Java, such as Eclipse's Standard Widget Toolkit (SWT) (used in Eclipse), Google Web Toolkit (GWT) (used in Android), 3. D Graphics API such as Java bindings for Open. GL (JOGL) and Java. D. You need to check the JDK API specification (http: //docs. AWT and Swing APIs while reading this chapter. The best online reference for Graphics programming is the . For advanced 2. D graphics programming, read . It consists of 1. Swing is even bigger, with 1. JDK 1. 8). Fortunately, only 2 packages - java. The java. awt package contains the core AWT graphics classes. GUI Component classes (such as Button, Text. Field, and Label),GUI Container classes (such as Frame, Panel, Dialog and Scroll. Pane),Layout managers (such as Flow. Layout, Border. Layout and Grid. Layout),Custom graphics classes (such as Graphics, Color and Font). The java. awt. event package supports event handling. Event classes (such as Action. Event, Mouse. Event, Key. Event and Window. Event),Event Listener Interfaces (such as Action. Listener, Mouse. Listener, Key. Listener and Window. Listener),Event Listener Adapter classes (such as Mouse. Adapter, Key. Adapter, and Window. Adapter). AWT provides a platform- independent and device- independent interface to develop graphic programs that runs on all platforms, such as Windows, Mac, and Linux. Containers and Components. There are two types of GUI elements: Component: Components are elementary GUI entities (such as Button, Label, and Text. Field.)Container: Containers (such as Frame and Panel) are used to hold components in a specific layout (such as flow or grid). A container can also hold sub- containers. GUI components are also called controls (Microsoft Active. X Control), widgets (Eclipse's Standard Widget Toolkit, Google Web Toolkit), which allow users to interact with (or control) the application through these components (such as button- click and text- entry). In the above figure, there are three containers: a Frame and two Panels. A Frame is the top- level container of an AWT program. A Frame has a title bar (containing an icon, a title, and the minimize/maximize/close buttons), an optional menu bar and the content display area. A Panel is a rectangular area used to group related GUI components in a certain layout. In the above figure, the top- level Frame contains two Panels. There are five components: a Label (providing description), a Text. Field (for users to enter text), and three Buttons (for user to trigger certain programmed actions). In a GUI program, a component must be kept in a container. Every container has a method called add(Component c). A container (say a. Container) can invoke a. Container. add(a. Component) to add a. Component into itself. For example. Panel panel = new Panel(). Button btn = new Button(. The commonly- used top- level containers in AWT are Frame, Dialog and Applet. A Frame provides the . A Dialog has a title- bar (containing an icon, a title and a close button) and a content display area, as illustrated. Applet will be discussed in the later chapter. Secondary Containers: Panel and Scroll. Pane. Secondary containers are placed inside a top- level container or another secondary container. AWT also provide these secondary containers: Panel: a rectangular box under a higher- level container, used to layout a set of related GUI components in pattern such as grid or flow. Scroll. Pane: provides automatic horizontal and/or vertical scrolling for a single child component. Hierarchy of the AWT Container Classes. The hierarchy of the AWT Container classes is as follows: As illustrated, each Container has a layout. AWT Component Classes. AWT provides many ready- made and reusable GUI components. Take note that System. You could use a Label to label another component (such as text field) or provide a text description. Check the JDK API specification for java. Label. Constructors. Label(String str. Label, int alignment). Label(String str. Label). public Label(); The Label class has three constructors: The first constructor constructs a Label object with the given text string in the given alignment. You could set the label text via the set. Text() method later. Constants. public static final LEFT. RIGHT. public static final CENTER; These three constants are defined for specifying the alignment of the Label's text. Public Methods. public String get. Text(). public void set. Text(String str. Label). Alignment(). public void set. Alignment(int alignment); The get. Text() and set. Text() methods can be used to read and modify the Label's text. The container can then add this component onto itself via a. Container. add(a. Component) method. Every container has a add(Component) method. Take note that it is the container that actively and explicitly adds a component onto itself, instead of the other way. Example. Label lbl. Input. lbl. Input = new Label(. We will describe Swing later. Event. Clicking a button fires a so- called Action. Event and triggers a certain programmed action. You can convert a String to a primitive, such as int or double via static method Integer. Int() or Double. parse. Double(). To convert a primitive to a String, simply concatenate the primitive with an empty String. Example 1: AWTCounter. Let's assemble some components together into a simple GUI counter program, as illustrated. In other words, this class AWTCounteris a. Frame, and inherits all the attributes and behaviors of a Frame, such as the title bar and content pane. Lines 1. 2 to 4. 6 define a constructor, which is used to setup and initialize the GUI components. In Line 1. 3, the set. Layout() (inherited from the superclass Frame) is used to set the layout of the container. Flow. Layout is used which arranges the components in left- to- right and flows into next row in a top- to- bottom manner. A Label, Text. Field (non- editable), and Button are constructed. We invoke the add() method (inherited from the superclass Frame) to add these components into container. In Line 3. 3- 3. 4, we invoke the set. Size() and the set. Title() (inherited from the superclass Frame) to set the initial size and the title of the Frame. The set. Visible(true) method (Line 4. The statement btn. Count. add. Action. Listener(this) (Line 2. In brief, whenever the button is clicked, the action. Performed() will be called. In the action. Performed() (Lines 5. Text. Field. In the entry main() method (Lines 5. AWTCounter is constructed. The constructor is executed to initialize the GUI components and setup the event- handling mechanism. The GUI program then waits for the user input. Inspecting Container/Components via to. String()It is interesting to inspect the GUI objects via the to. String(), to gain an insight to these classes. You could have an insight of the variables defined in the class. The components are arranged in Flow. Layout. The program shall accumulate the number entered into the input. Text. Field and display the sum in the output. Text. Field. The Frame adds the components, in Flow. Layout. tf. Input (Text. Field) is the source object, which fires an Action. Event upon hitting the Enter key. The listener class (this or AWTAccumulator) needs to implement Action. Listener interface and provides implementation to method action. Performed(). Whenever an user hits Enter on the tf. Input (Text. Field), the action. Performed() will be invoked. Inspecting Container/Components via to. String()Printing the to. String() after set. Visible() produces. AWTAccumulator. This is unlike the procedural model, where codes are executed in a sequential manner. The AWT's event- handling classes are kept in package java. Three objects are involved in the event- handling: a source, listener(s) and an event object. The source object (such as Button and Textfield) interacts with the user. Upon triggered, it creates an event object. In other words, triggering a source fires an event to all its listener(s), and invoke an appropriate handler of the listener(s). To express interest for a certain source's event, the listener(s) must be registered with the source. In other words, the listener(s) . This is known as subscribe- publish or observable- observer design pattern. The sequence of steps is illustrated above: The source object registers its listener(s) for a certain type of event. For example, clicking an Button fires an Action. Event, mouse- click fires Mouse. Event, key- type fires Key. Event, etc. How the source and listener understand each other? For example, if a source is capable of firing an event called Xxx. Event (e. g., Mouse. Event) involving various operational modes (e. Firstly, we need to declare an interface called Xxx. Listener (e. g., Mouse. Listener) containing the names of the handler methods. Recall that an interface contains only abstract methods without implementation. For example. public interface Mouse. Listener . That is, the listeners must provide their own implementations (i. Xxx. Listener interface. In this way, the listenser(s) can response to these events appropriately. For example. class My. Mouse. Listener implement Mouse. Listener . The signature of the methods are. Xxx. Listener(Xxx. Listener l). public void remove. Xxx. Listener(Xxx.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2017
Categories |