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

Example Analysis of layout Controller in javaGUI programming

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

javaGUI programming layout controller example analysis, I believe that many inexperienced people are helpless, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Layout controller is used to automatically assign how each component is arranged inside the window; the default is FlowLayout, i.e. sorting one by one. FlowLayout is Panel

instance 1:FlowLayout

import java.awt.*; public class TestLayout { public static void main(String[] args) { Frame f = new Frame(); Button b1 = new Button("ok"); Button b2 = new Button("open"); Button b3 = new Button("change"); f.setBackground(Color.BLUE); f.setBounds(100, 100, 300, 300); f.setLayout(new FlowLayout()); f.setResizable(true); f.add(b1); f.add(b2); f.add(b3); f.setVisible(true); }}

instance 2:BorderLayout

import java.awt.*; public class TestBorderLayout { public static void main(String[] args) { Frame f = new Frame("Border Layout"); Button bn = new Button("Bn"); Button bs = new Button("Bs"); Button be = new Button("Be"); Button bw = new Button("Bw"); Button bc = new Button("Bc"); // f.add(bw,"West");// f.add(be,"East");// f.add(bn,"North");// f.add(bs,"South");// f.add(bc,"Center");// f.setLayout(new BordorLayout()); f.add(bn,BorderLayout.NORTH); f.add(bs,BorderLayout.SOUTH); f.add(bw,BorderLayout.WEST); f.add(be,BorderLayout.EAST); f.add(bc,BorderLayout.CENTER); f.setSize(200, 200); f.setVisible(true); }}

BorderLayout divides the entire interface into five regional blocks in the east, west, north and south; BorderLayout.NORTH is int type data and is a constant.

instance 3:GridLayout

import java.awt.*; public class TestGridLayout { public static void main(String[] args) { Frame f = new Frame(); Button b1 = new Button("b1"); Button b2 = new Button("b2"); Button b3 = new Button("b3"); Button b4 = new Button("b4"); Button b5 = new Button("b5"); Button b6 = new Button("b6"); f.setLayout(new GridLayout(3,2)); f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5); f.add(b6); f.pack(); f.setSize(200, 200); f.setVisible(true); }}

Note: When Layout is called, the size and position of its internal components are assigned by the system (Layout Manager), and cannot be considered as a design. The pack() method is used to display components.

instance 4: Integrated instance

import java.awt.*; public class TestedLayout { public static void main(String[] args) { Frame f = new Frame("GUI"); f.setLayout(new GridLayout(2,1)); Panel p1 = new Panel(new BorderLayout()); Panel p2 = new Panel(new BorderLayout()); Panel p11 = new Panel(new GridLayout(2,1)); Panel p22 = new Panel(new GridLayout(2,2)); Button b1 = new Button("button"); Button b2 = new Button("button"); Button b3 = new Button("button"); Button b4 = new Button("button"); Button b5 = new Button("button"); Button b6 = new Button("button"); Button b7 = new Button("button"); Button b8 = new Button("button"); Button b9 = new Button("button"); Button b10 = new Button("button"); p1.add(b1,BorderLayout.WEST); p1.add(b2,BorderLayout.EAST); p2.add(b3,BorderLayout.WEST); p2.add(b4,BorderLayout.EAST); p11.add(b5); p11.add(b6); p22.add(b7); p22.add(b8); p22.add(b9); p22.add(b10); p1.add(p11,BorderLayout.CENTER); p2.add(p22,BorderLayout.CENTER); f.add(p1); f.add(p2); f.setBounds(100, 100, 400, 400); f.setVisible(true); }}

Note: A new creates a button object and allocates a memory space. Even if it is exactly the same button, that is, the same size name, etc., it is still one after all. When it is repeatedly added, it is only equivalent to changing its position. Unlike variables, it is not a simple copy, but a memory and object pointing problem.

After reading the above, do you know how to analyze the layout controller example in java GUI programming? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

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