Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the button and layout of the graphical user interface of Java program

2025-02-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to realize the buttons and layout of Java graphical user interface". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to realize the buttons and layout of the graphical user interface of Java programs.

Button component JButton

The JButton component represents a normal button

The common methods of the JButton class use public JButton () throws HeadlessException to create a Button object public JButton (String label) throws HeadlessException to create a Button object At the same time, specify its display content public JButton (Icon icon) create a button with picture public JButton (String text,Icon icon) create a button with picture and text public void setLabel (String label) set the display content of Button public String getLabel () get the size and display mode of the Button display content public void setBounds component and display mode public void setMnemonic (int mnemonic) set the shortcut key of the button

Demo

Import javax.swing.*;import java.awt.*;public class Hello {public static void main (String [] args) {JFrame frame = new JFrame ("one"); JButton but = new JButton ("click"); Font font = new Font ("Serief", Font.BOLD,25); but.setFont (font); frame.add (but); frame.setSize (2005.70); frame.setLocation (500300) Frame.setVisible (true);}}

The button displays the picture import javax.swing.*;public class Hello {public static void main (String [] args) {JFrame frame = new JFrame ("one"); String Path= "C:\\ Users\\ 30452\ Desktop\\ 123.jpg"; Icon icon = new ImageIcon (Path, "MLDN"); JButton but = new JButton (icon); frame.add (but); frame.setSize (500600) Frame.setLocation (300200); frame.setVisible (true);}}

Layout Manager

The following five common layout managers are mainly used in Swing: FlowLayout, BorderLayout, GridLayout, CardLayout, and absolute positioning.

FlowLayout

Streaming layout manager, which causes all components to be arranged in sequence like pipelining

Constant action public static final int CENTER center alignment public static final int LEADING in the same way as the beginning of the container public static final int LEFT left alignment public static final int RIGHT right alignment public static final int TRAILING in the same way as the end alignment of the container use public FlowLayout () to construct a new FlowLayout, center alignment public FlowLayout (int align) to construct a FlowLayout, and specify alignment public FlowLayout (int align,int hgap,int vgap) specify alignment, horizontal, vertical spacing

Demo:

Import javax.swing.*;import java.awt.*;public class Hello {public static void main (String [] args) {JFrame frame = new JFrame ("one"); frame.setLayout (new FlowLayout (FlowLayout.CENTER,4,4)); JButton but = null; for (int item0)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report