In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail about Java graphical interface programming. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. Content Overview
first talk about personal understanding of graphical interface programming, graphical interface programming can directly see the effect of each step, compared to the traditional programming staring at the black box to learn is very interesting.
to talk about the final effect, the interface is made up of windows and components. The arrangement of components in the window is not disorganized, relying on the layout manager to make the components present in the right position and reasonable layout. The components arranged in the window can interact with the user through the event listener.
two。 Container Container
what is a container? Containers are special components. Containers are used to hold things, not only can be used to store components, but also can be used to store containers, and storage containers can store containers or components. It sounds like a lot of dolls, but it's easy to learn!
2.1Window
Window is a top-level window that can exist independently and uses the BorderLayout layout manager by default.
The frame.setLocation (500300) method is used to set the location of the window, and the computer's far point coordinates are usually in the upper-left corner.
The frame.setSize (500300) method is used to set the size of the window.
frame.setVisible (true) sets whether the window is visible.
Run effect (use Frame to create a window):
note that the window at this time can not close the window by clicking the'X'in the upper right corner, but can only end the program manually, because the event listening mechanism has not been added yet.
Code:
Import java.awt.*;public class WindowDemo {public static void main (String [] args) {/ / create a window object Frame frame = new Frame ("Test Window window"); / / specify the location and size of the window frame.setLocation (500300); frame.setSize (500300); / / set window visible frame.setVisible (true);}} 2.2Panel
Panel is an embedded container that must be embedded in other containers and cannot exist independently. It uses the FlowLayout layout manager by default.
Running effect:
For example, add panel to Frame, and the nature of the FlowLayout arrangement makes Panel easy to use.
A TextField component and a Button component are added to Panel through Panel's add method (p.add ("test text");). Finally, Panel is added to the Frame.
The setBounds (100100500300) method can set the coordinates and dimensions of the window at once.
Code:
Import java.awt.*;public class PanelDemo {public static void main (String [] args) {/ / 1. Create a Window object, because panel and other containers cannot exist independently and must be attached to Window Frame frame = new Frame ("demonstrate panel here"); / / 2. Create a panel object Panel p = new Panel (); / / 3. Create a text box and button and put them in Panel p.add (new TextField); p.add (new Button); / / 4. Put panel into Window frame.add (p); / / 5. Set the location and size of Window frame.setBounds (100100500300); / / 6. Set Window visible frame.setVisible (true);}} 2.3ScrollPane
Scrollpane is a container with scroll bars and cannot exist independently. By default, the cloth BorderLayout office manager is used. The parameter ScrollPane.SCROLLBARS_ALWAYS in the ScrollPane constructor in line 7 causes ScrollPane to display scroll bars by default, because ScrollPane does not display scroll bars by default when there is not much content.
Running effect:
Code:
Import java.awt.*;public class ScrollPaneDemo {public static void main (String [] args) {Frame frame = new Frame ("this is the test ScrollPane"); / / create a ScrollPane ScrollPane sp = new ScrollPane (ScrollPane.SCROLLBARS_ALWAYS); / / add content sp.add (new TextField ("test text")) to the ScrollPane; / / add ScrollPane to Frame frame.add (sp) Frame.setBounds (100100500300); frame.setVisible (true);}} 2.4Box
Box containers, which can be arranged horizontally or vertically to accommodate components or containers, are very useful for modularizing the construction of window frames.
The frame.pack () pack () method automatically sets the optimal size of the window based on the number and size of components in the window.
uses the Box.createHorizontalBox () method to create a horizontal Box container whose contents can only be arranged horizontally.
uses the Box.createVerticalBox () method to create a vertical Box container whose contents can only be arranged vertically.
uses the Box.createHorizontalGlue () or Box.createVerticalGlue () method to store content, and note that the size of such intervals changes as the window is dragged. Use Box.createHorizontalStrut (width) (Box.createVerticalStrut (height)) to create intervals that remain the same size horizontally (vertically).
Running effect:
Code:
Import javax.swing.*;import java.awt.*;public class BoxDemo {public static void main (String [] args) {Frame frame = new Frame (); / / create a horizontal Box Box hbox = Box.createHorizontalBox (); hbox.add (new Button ("horizontal button 1")); hbox.add (Box.createHorizontalGlue ()) / / size variable interval hbox.add (new Button ("horizontal button 2")); hbox.add (Box.createHorizontalStrut (30)); / / horizontal size invariant interval hbox.add (new Button ("horizontal button 3")); / / create a vertical Box Box vbox = Box.createVerticalBox (); vbox.add ("vertical button 1")) Vbox.add (Box.createVerticalGlue ()); / / size variable interval vbox.add (new Button ("vertical button 2")); vbox.add (Box.createVerticalStrut (30)); / / vertical size invariant interval vbox.add (new Button ("vertical button 3")); frame.add (hbox,BorderLayout.NORTH); frame.add (vbox); frame.pack () Frame.setVisible (true);} 3. Layout Manager 3.1FlowLayout
FlowLayout streaming layout Manager, adding content from left to right, top to bottom. You can customize the spacing and how it is arranged.
The setLayout (); method sets the layout manager for the specified container.
For example: frame.setLayout (new FlowLayout (FlowLayout.CENTER,40,20)); change the layout manager of frame (frame defaults to BorderLayout) to FlowLayout.
In the construction method, the first parameter of FlowLayout (FlowLayout.CENTER,40,20) is the specified arrangement, and the last two parameters are row spacing and column spacing. FlowLayout.CENTER indicates center alignment, FlowLayout.LEFT indicates left alignment, and FlowLayout.RIGHT indicates right alignment.
Running effect (using streaming layout Manager to add 9 buttons):
Code:
Import java.awt.*
Public class FlowLayoutDemo {public static void main (String [] args) {Frame frame = new Frame (); / / 1. Through setLayout frame.setLayout (new FlowLayout (FlowLayout.CENTER,40,20)); for (int iTunes 1)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.