You can lay out your forms by simply placing components where you want them. Creating a Project Because all Java development in the IDE takes place within projects, we first need to create a new ContactEditor project within which to store sources and other project files. An IDE project is a group of Java source files plus its associated meta data, including project-specific properties files, an Ant build script that controls the build and run settings, and a project. While Java applications often consist of several IDE projects, for the purposes of this tutorial, we will build a simple application which is stored entirely in a single project. Click Next.
|Published (Last):||19 October 2010|
|PDF File Size:||11.17 Mb|
|ePub File Size:||9.7 Mb|
|Price:||Free* [*Free Regsitration Required]|
You can lay out your forms by simply placing components where you want them. Creating a Project Because all Java development in the IDE takes place within projects, we first need to create a new ContactEditor project within which to store sources and other project files. An IDE project is a group of Java source files plus its associated meta data, including project-specific properties files, an Ant build script that controls the build and run settings, and a project.
While Java applications often consist of several IDE projects, for the purposes of this tutorial, we will build a simple application which is stored entirely in a single project. Click Next. Enter ContactEditor in the Project Name field and specify the project location. Click Finish. To proceed with building our interface, we need to create a Java container within which we will place the other required GUI components. Enter my. Notice that the my. When we added the JFrame container, the IDE opened the newly-created ContactEditorUI form in an Editor tab with a toolbar containing several buttons, as shown in the preceding illustration.
The additional toolbar buttons provide convenient access to common commands, such as choosing between Selection and Connection modes, aligning components, setting component auto-resizing behavior, and previewing forms. Provides a representation of all the components, both visual and non-visual, in your application as a tree hierarchy.
The Navigator also provides visual feedback about what component in the tree is currently being edited in the GUI Builder as well as allows you to organize components in the available panels. In addition, you can create, remove, and rearrange the categories displayed in the Palette using the customizer.
Properties Window. Guarded blocks are protected areas that are not editable in Source view. You can only edit code appearing in the white areas of the Editor when in Source view. Note: For advanced developers, the Palette Manager is available that enables you to add custom components from JARs, libraries, or other projects to the Palette. As you lay out your form, the GUI Builder provides visual guidelines suggesting optimal spacing and alignment of components.
In the background, the GUI Builder translates your design decisions into a functional UI that is implemented using the new GroupLayout layout manager and other Swing constructs. The GUI Builder figures out which layout attributes are required and then generates the code for you automatically. You need not concern yourself with insets, anchors, fills, and so forth. The GUI Builder provides helpful inline hints and other visual feedback regarding where components should be placed on your form, automatically snapping components into position along guidelines.
It makes these suggestions based on the positions of the components that have already been placed in the form, while allowing the padding to remain flexible such that different target look and feels render properly at runtime. Visual Feedback The GUI Builder also provides visual feedback regarding component anchoring and chaining relationships.
These indicators enable you to quickly identify the various positioning relationships and component pinning behavior that affect the way your GUI will both appear and behave at runtime.
This speeds the GUI design process, enabling you to quickly create professional-looking visual interfaces that work. All you need to do is drag and drop the components you need to your GUI form as shown in the illustrations that follow. Note: Refer to the Adding individual and multiple components. Many interface designers consider this a "best practice" technique, however, for the purposes of this tutorial you can simply peek at how our completed form should look by jumping ahead to the Previewing your GUI section.
To add a JPanel: In the Palette window, select the Panel component from the Swing Containers category by clicking and releasing the mouse button. Move the cursor to the upper left corner of the form in the GUI Builder. Click in the form to place the JPanel in this location. In order to do this we need to deselect the JPanel we just added.
Notice, however, that when you pass the cursor over the JPanel, its edges change to light gray so that its position can be clearly seen. You need only to click anywhere within the component to reselect it and cause the resize handles and anchoring indicators to reappear. Release the resize handle to resize the component.
Notice that the suggested vertical spacing between the two JPanels is much narrower than that at the edges. Because we want to visually distinguish the functions in the upper and lower sections of our GUI, we need to add a border and title to each JPanel. In the Properties window, click the ellipsis button In the Properties pane below, enter Name for the Title property. Click the ellipsis Click OK to exit the dialogs.
Select the bottom JPanel and repeat steps 2 through 5, but this time right-click the JPanel and access the Properties window using the pop-up menu. Enter E-mail for the Title property. Titled borders are added to both JPanel components. Adding Individual Components to the Form Now we need to start adding the components that will present the actual contact information in our contact list. Move the cursor over the Name JPanel we added earlier. When the guidelines appear indicating that the JLabel is positioned in the top left corner of the JPanel with a small margin at the top and left edges, click to place the label.
The JLabel is added to the form and a corresponding node representing the component is added to the Inspector window. Before going further, we need to edit the display text of the JLabel we just added. Though you can edit component display text at any point, the easiest way is to do this as you add them.
Type First Name: and press Enter. Move the cursor immediately to the right of the First Name: JLabel we just added. As usual, a node representing the component is added to the Navigator window. Before proceeding further, we need to add an additional JLabel and JTextField immediately to the right of those we just added, as shown in the following illustration.
When the vertical alignment guidelines appear suggesting the margin between the text field and right edge of the JPanel, release the mouse button to resize the JTextField. While accomplishing this, again notice that the GUI Builder displays horizontal and vertical guidelines suggesting the preferred component spacing. To add multiple JLabels to the form: In the Palette window, select the Label component from the Swing Controls category by clicking and releasing the mouse button.
Move the cursor over the form directly below the First Name: JLabel we added earlier. While still pressing the Shift key, place another JLabel immediately to the right of the first. Make certain to release the Shift key prior to positioning the second JLabel.
If you forget to release the Shift key prior to positioning the last JLabel, simply press the Escape key. The JLabels are added to the form creating a second row, as shown in the following illustration. Nodes representing each component are added to the Navigator window.
Type Title: and press Enter. Inserting Components Note: Refer to the Inserting components. Often it is necessary to add a component between components that are already placed in a form. Whenever you add a component between two existing components, the GUI Builder automatically shifts them to make room for the new component. Move the cursor over the Title: and Nickname: JLabels on the second row such that the JTextField overlaps both and is aligned to their baselines.
If you encounter difficulty positioning the new text field, you can snap it to the left guideline of the Nickname JLabel as shown in the first image below. The rightmost JLabel shifts toward the right of the JTextField to accommodate the suggested horizontal offset. Move the cursor to the right of the Nickname label and click to place the text field.
When the vertical alignment guidelines appear suggesting the margin between the text field and JPanel edges, release the mouse button to resize the JTextField. Press Ctrl-S to save the file. Component Alignment Note: Refer to the Aligning and anchoring components.
Every time you add a component to a form, the GUI Builder effectively aligns them, as evidenced by the alignment guidelines that appear.
It is sometimes necessary, however, to specify different relationships between groups of components as well. Click the Align Right in Column button in the toolbar.
The anchoring relationships are updated, indicating that the components have been grouped. The JTextFields are set to resize horizontally at runtime. The alignment guidelines and anchoring indicators are updated, providing visual feedback of the component relationships. To set components to be the same size: Control-click all four of the JTextFields in the form to select them.
The JTextFields are all set to the same width and indicators are added to the top edge of each, providing visual feedback of the component relationships. Now we need to add another JLabel describing the JComboBox that will enable users to select the format of the information our ContactEditor application will display. The JLabel snaps into a right-aligned position with the column of JLabels above, as shown in the following illustration.
As in the previous examples, double-click the JLabel to select its display text and then enter Display Format: for the display name. Notice that when the JLabel snaps into position, the other components shift to accommodate the longer display text. Baseline Alignment Whenever you add or move components that include text JLabels, JTextFields, and so forth , the IDE suggests alignments which are based on the baselines of the text in the components.
When we inserted the JTextField earlier, for example, its baseline was automatically aligned to the adjacent JLabels. Notice once again the baseline alignment guidelines that appear to assist us with the positioning.
To align the baselines of components: In the Palette window, select the Combo Box component from the Swing Controls category. Move the cursor immediately to the right of the JLabel we just added. The component snaps into a position aligned with the baseline of the text in the JLabel to its left, as shown in the following illustration. It is important to understand, however, that another integral part of component placement is anchoring. Once placed, new components are also anchored to the nearest container edge or component to ensure that component relationships are maintained at runtime.
Adding, Aligning, and Anchoring The GUI Builder enables you to lay out your forms quickly and easily by streamlining typical workflow gestures.
Whenever you add a component to a form, the GUI Builder automatically snaps them into the preferred positions and sets the necessary chaining relationships so you can concentrate on designing your forms rather than struggling with complicated implementation details.
Creare interfacce grafiche Java con Google WindowBuilder Pro
Una Graphical User Interface si compone di tanti elementi. Ci saranno poi dei bottoni e le relative azioni generate dalla loro pressione. Il tutto va a comporre una struttura che ci fa comunicare con il programma installato sul nostro PC. Diciamo che potremmo parlare di finestra nella finestra, come stessimo creando una matrioska. In seconda battuta ci occuperemo di tutte le funzioni associate alla pressione di ciascun bottone. Questi quattro vengono impiegati con una certa frequenza, ma ne esistono tanti altri.
INTERFACCE GRAFICHE IN JAVA - I