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

What are the common layout methods in Java graphical interface

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points about the layout commonly used in the Java graphical interface. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

Streaming layout

Using a streaming layout will arrange the elements in the order from left to right. If an element cannot fit in a row, the element will be arranged in a separate line and still in the order from left to right.

Code

Public class Test {public static void main (String [] args) {/ / create window JFrame jFrame = new JFrame (); / / set window name jFrame.setTitle ("hello"); / / create streaming layout manager alignment to left align LayoutManager layout = new FlowLayout (FlowLayout.LEFT) / / close the window Terminator jFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); / / create the content panel Container contentpage = jFrame.getContentPane (); / / set the content panel layout to stream layout contentpage.setLayout (layout) / / create buttons JButton button1 = new JButton ("1"); JButton button2 = new JButton ("2"); JButton button3 = new JButton ("3"); JButton button4 = new JButton ("4"); JButton button5 = new JButton ("5") / / set button size button1.setPreferredSize (new Dimension (100100)); button2.setPreferredSize (new Dimension (100100)); button3.setPreferredSize (new Dimension (100100)); button4.setPreferredSize (new Dimension (100100)); button5.setPreferredSize (new Dimension (100100)) / / set button background color button1.setBackground (Color.red); button2.setBackground (Color.blue); button3.setBackground (Color.pink); button4.setBackground (Color.orange); button5.setBackground (Color.yellow) / / add buttons to the content panel contentpage.add (button1); contentpage.add (button2); contentpage.add (button3); contentpage.add (button4); contentpage.add (button5); / / set window size jFrame.setSize (500,300) / / set window visible jFrame.setVisible (true);}} border layout

Using the boundary layout, the elements will be divided into five directions: East, West, Central, South and North, respectively, marked by EAST,WEST,CENTER,SOUTH,NORTH, and only one element can be placed in each direction.

Code

Public class Test {public static void main (String [] args) {/ / create window JFrame jFrame = new JFrame (); / / set window name jFrame.setTitle ("hello"); / / create border layout manager BorderLayout layout = new BorderLayout () / / close the window termination program jFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); / / create the content panel Container contentpage = jFrame.getContentPane (); / / set the content panel layout to stream layout contentpage.setLayout (layout) / / create buttons JButton button1 = new JButton ("1"); JButton button2 = new JButton ("2"); JButton button3 = new JButton ("3"); JButton button4 = new JButton ("4"); JButton button5 = new JButton ("5") / / set button background color button1.setBackground (Color.red); button2.setBackground (Color.blue); button3.setBackground (Color.pink); button4.setBackground (Color.orange); button5.setBackground (Color.yellow) / / add buttons to the content panel / / place buttons in the north contentpage.add (button1,BorderLayout.NORTH); / / place buttons in the south contentpage.add (button2,BorderLayout.SOUTH); / / place buttons in the west contentpage.add (button3,BorderLayout.WEST) / / place the button on the east contentpage.add (button4,BorderLayout.EAST); / / place the button on the center contentpage.add (button5,BorderLayout.CENTER); / / set the window size jFrame.setSize (500,300) / / set window visible jFrame.setVisible (true);}} Card layout

As the name implies, if a container uses a card layout, all the components in it overlap like a deck of cards. The container can only display one component, and the first component is displayed by default. You can change the displayed component through the show method in CardLayout.

Code

Public class Test {public static void main (String [] args) {/ / create window JFrame jFrame = new JFrame (); / / set window name jFrame.setTitle ("hello"); / / create card layout manager CardLayout layout = new CardLayout () / / close the window Terminator jFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); / / create the panel JPanel jPanel = new JPanel (); / / set the panel layout to card layout jPanel.setLayout (layout) / / add button to set background color JButton jButton1 = new JButton (); jButton1.setBackground (Color.pink); JButton jButton2 = new JButton (); jButton2.setBackground (Color.yellow) / / add the button to the panel and name the button jPanel.add (jButton1, "bt1"); jPanel.add (jButton2, "bt2"); / / specify the button layout.show (jPanel, "bt2") to be displayed on the panel / / add panels to the window jFrame.add (jPanel); / / set window size jFrame.setSize (500300); / / set window visible jFrame.setVisible (true);}} Custom layout

The so-called custom layout means that we do not use any layout manager, but we specify the location of the component by specifying the X coordinate, Y coordinate, width and height of the component.

The component uses the vertex in the upper left corner as the origin to locate the coordinates, uses a custom layout, and sets the layout manager used by the container to null

Then some friends will ask, since the layout manager is set to null, is it possible not to set it directly? of course not. If it is not set, the components will not be displayed.

Code

Public class Test {public static void main (String [] args) {/ / create window JFrame jFrame = new JFrame (); / / set window name jFrame.setTitle ("hello") / / close the window and end the program jFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); / / create a panel JPanel jPanel = new JPanel (); / / use a custom layout to set the layout manager used by the container to null jPanel.setLayout (null) / / add button to set background color JButton jButton1 = new JButton (); jButton1.setBackground (Color.pink); JButton jButton2 = new JButton (); jButton2.setBackground (Color.yellow) / / set the coordinates of the button to (100100), width 100, height 100 jButton1.setBounds (new Rectangle (100100100100)); / / set the coordinates of the button to (220), width 100, height 100 jButton2.setBounds (new Rectangle (220) 70100100) / / add buttons to the panel jPanel.add (jButton1); jPanel.add (jButton2); / / add the panel to the window jFrame.add (jPanel) / / set the window size jFrame.setSize (500300); / / set the window visible jFrame.setVisible (true);}} these are all the contents of the article "what are the common layout methods in the Java graphical interface?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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