import java awt *; // for layout managers import javax swing *; // for GUI components The default type of layout manager for a JFrame is called a BorderLayout,
Previous PDF | Next PDF |
[PDF] Unit 1 Java Features - Annamalai University
Java is one of the most popular and widely used programming language • Java has been one of StringBuffer s=new StringBuffer("GeeksforGeeks"); Methods Some of AWT and SWT's concept of Heavyweight components The difference
[PDF] Graphical User Interfaces - Building Java Programs
import java awt *; // for layout managers import javax swing *; // for GUI components The default type of layout manager for a JFrame is called a BorderLayout,
[PDF] Limitations of AWT, MVC Architecture, Components & Containers
This means that the look and feel of a component is defined by the platform, not by java Because the AWT components use native code resources, they are
[PDF] Java Swing
15 jui 2019 · understand JavaBean GUI components that can be dragged and dropped as GUI builders in visual programming environment Java Swing JPanel with examples GeeksforGeeks Jurnal Java AWT dan Swing Catatan Kecil
Programmer Avec Java Swing Et Sql - UNIJALES
Creating Frames using Swings in Java - GeeksforGeeks (interfaces graphiques ) avec Swing et AWT, les outils les plus célèbres du monde Java widgets and packages to make sophisticated GUI components for Java applications Swing is
[PDF] awt components in java ppt
[PDF] awt components in java program
[PDF] awt components in java tutorial point
[PDF] awt components in javatpoint
[PDF] awt controls in java
[PDF] ay tax airline
[PDF] azure devops command line
[PDF] azure fortigate pricing
[PDF] a^nb^n is not regular
[PDF] baby bar essays
[PDF] baby boom 1950
[PDF] baby boom chart?
[PDF] baby boom france 1945
[PDF] baby boom france 2000
Introduction
In this chapter we will explore the creation of graphical user interfaces (GUIs).Although console programs like the ones we have written in the preceding chapters are still very important,the majority of modern desk- top applications have graphical user interfaces.Supplement 3G introduced a DrawingPanelclass that allowed you to draw two-dimensional graph- ics on the screen.This class is useful for certain applications,but writing a GUI is not the same as drawing shapes and lines onto a canvas.A real graphical user interface includes window frames which you create that contain buttons,text input fields,and other onscreen components. A major part of creating a graphical user interface in Java is figuring out how to position and lay out the components of the user interface to match the appearance you desire. Once you have chosen and laid out these components,you must make the events interactive by making them respond to various user events such as button clicks or mouse move- ments.There are many predefined components,but you can also define components that draw custom two-dimensional graphics,including anima- tions.At the end of this chapter,we will reimplement a basic version of the DrawingPanelclass from Supplement 3G.14.1GUI Basics
▪Graphical Input and Output with Option Panes ▪Working with Frames▪Buttons, Text Fields, and Labels ▪Changing a Frame's Layout ▪Handling an Event14.2Laying Out Components
▪Layout Managers ▪Composite Layouts14.3Interaction between
Components
▪Example 1: BMI GUI ▪Object-Oriented GUIs ▪Example 2: Credit Card GUI14.4Additional Components
and Events ▪Text Areas, Scrollbars, and Fonts ▪Icons ▪Mouse Events14.5Two-Dimensional
Graphics
▪Drawing onto Panels ▪Animation with Timers14.6Case Study:Implementing
DrawingPanel
▪Initial Version without Events ▪Second Version with EventsChapter14
Graphical User Interfaces
822M14_REGE1813_02_SE_C14.qxd 2/10/10 3:43 PM Page 822
14.1GUI Basics823
14.1GUI Basics
GUIs are potentially very complex entities because they involve a large number of interacting objects and classes. Each onscreen component and window is represented by an object, so a programmer starting out with GUIs must learn many new class, method, and package names. In addition, if the GUI is to perform sophisticated tasks, the objects must interact with each other and call each other's methods, which raises tricky communication and scoping issues. Another factor that makes writing GUIs challenging is that the path of code execu- tion becomes nondeterministic. When a GUI program is running, the user can click any of the buttons and interact with any of the other onscreen components in any order. Because the program's execution is driven by the series of events that occur, we say that programs with GUIs are event-driven.In this chapter you'll learn how to handle user events so that your event-driven graphical programs will respond appro- priately to user interaction.Graphical Input and Output with Option Panes
The simplest way to create a graphical window in Java is to have an option panepop up. An option pane is a simple message box that appears on the screen and presents a message or a request for input to the user. The Java class used to show option panes is called JOptionPane. JOptionPane belongs to the javax.swingpackage, so you'll need to import this package to use it. ("Swing" is the name of one of Java's GUI libraries.) Note that the package name starts with javaxthis time, not java. The xis because, in Java's early days, Swing was an extension to Java's feature set. import javax.swing.*; // for GUI components JOptionPanecan be thought of as a rough graphical equivalent of System.out.printlnoutput and Scannerconsole input. The following program creates a "Hello, world!" message on the screen with the use of JOptionPane:1 // A graphical equivalent of the classic "Hello world" program.
23 importjavax.swing.*; // for GUI components
45 public classHelloWorld {
6 public static voidmain(String[] args) {
7 JOptionPane.showMessageDialog(null, "Hello, world!");
8 }
9 } The program produces the following graphical "output" (we'll show screenshots for the output of the programs in this chapter): M14_REGE1813_02_SE_C14.qxd 2/10/10 3:43 PM Page 823824Chapter 14Graphical User Interfaces
The window may look slightly different in different operating systems, but the message will be the same. The preceding program uses a static method in the JOptionPaneclass called showMessageDialog. This method accepts two parameters: a parent window and a mes- sage string to display. We don't have a parent window in this case, so we passed null. JOptionPanecan be used in three major ways: to display a message (as just demonstrated), to present a list of choices to the user, and to ask the user to type input. The three methods that implement these three behaviors are called showMessageDialog,showConfirmDialog,and showInputDialog, respectively.These methods are detailed in Table 14.1.
Table 14.1Useful Methods of the JOptionPaneClass
Method Description
showConfirmDialog(parent,Shows a Yes/No/Cancel message box containing the given message)message on the screen and returns the choice as an intwith one of the following constant values: • JOptionPane.YES_OPTION(user clicked "Yes") • JOptionPane.NO_OPTION(user clicked "No") • JOptionPane.CANCEL_OPTION(user clicked "Cancel") showInputDialog(parent,Shows an input box containing the given message on the message)screen and returns the user's input value as a String showMessageDialog(parent,Shows the given message string in a message box on the message)screen The following program briefly demonstrates all three types of option panes:1 // Shows several JOptionPane windows on the screen.
23 importjavax.swing.*; // for GUI components
45 public classUseOptionPanes {
6 public static voidmain(String[] args) {
7 // read the user's name graphically
8 String name = JOptionPane.showInputDialog(null,
9 "What is your name?");
1011 // ask the user a yes/no question
12 intchoice = JOptionPane.showConfirmDialog(null,
M14_REGE1813_02_SE_C14.qxd 2/10/10 3:43 PM Page 82414.1GUI Basics825
13 "Do you like cake, " + name + "?");
1415 // show different response depending on answer
16 if(choice == JOptionPane.YES_OPTION) {
17 JOptionPane.showMessageDialog(null,
18 "Of course! Who doesn't?");
19 } else{ // choice == NO_OPTION or CANCEL_OPTION
20 JOptionPane.showMessageDialog(null,
21 "We'll have to agree to disagree.");
22 }
23 }
24 }The graphical input and output of this program is a series of windows, which pop up one at a time: One limitation of JOptionPaneis that its showConfirmDialogmethod always returns the user's input as a String. If you'd like to graphically request user input that is a number instead, your program must convert the Stringusing the Integer.parseIntor Double.parseDoublemethod. These static methods accept a Stringas a parameter and return an intor doublevalue, respectively. The following program demonstrates the use of JOptionPaneto read numbers from the user: