java 图形化界面 布局管理器
Posted 千彧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 图形化界面 布局管理器相关的知识,希望对你有一定的参考价值。
1 package Layout; 2 import java.awt.*; 3 import javax.swing.*; 4 public class MyBorderLayout extends JFrame{ 5 private JButton b1 = new JButton("东"); 6 private JButton b2 = new JButton("南"); 7 private JButton b3 = new JButton("西"); 8 private JButton b4 = new JButton("北"); 9 private JButton b5 = new JButton("中"); 10 public MyBorderLayout(){ 11 12 setTitle("用户界面"); 13 setSize(500, 500); 14 setLocation(100, 100); 15 add(b1,BorderLayout.EAST); 16 add(b2,BorderLayout.SOUTH); 17 add(b3,BorderLayout.WEST); 18 add(b4,BorderLayout.NORTH); 19 add(b5,BorderLayout.CENTER); 20 21 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 22 setVisible(true); 23 } 24 25 26 public static void main(String[] args) { 27 MyBorderLayout l = new MyBorderLayout(); 28 29 } 30 31 }
1 package Layout; 2 import java.awt.*; 3 import java.util.HashMap; 4 import java.util.Map; 5 6 import javax.swing.*; 7 public class MyFlowLayout extends JFrame{ 8 private Map<String,JButton> map = new HashMap(); 9 private JButton b = new JButton(); 10 public MyFlowLayout(){ 11 map.put("B1",new JButton("B1")); 12 map.put("B2",new JButton("B2")); 13 map.put("B3",new JButton("B3")); 14 map.put("B4",new JButton("B4")); 15 map.put("B5",new JButton("B5")); 16 this.setLayout(new FlowLayout()); 17 // this.setLayout(new FlowLayout(FlowLayout.LEFT)); 18 // this.setLayout(new FlowLayout(FlowLayout.RIGHT)); 19 this.add(map.get("B1")); 20 this.add(map.get("B2")); 21 this.add(map.get("B3")); 22 this.add(map.get("B4")); 23 this.add(map.get("B5")); 24 this.setTitle("用户界面"); 25 this.setSize(500, 400); 26 this.setLocation(400, 500); 27 this.setVisible(true); 28 } 29 30 31 public static void main(String[] args) { 32 33 MyFlowLayout l = new MyFlowLayout(); 34 } 35 36 }
1 package Layout; 2 import java.awt.*; 3 import java.util.HashMap; 4 import java.util.Map; 5 import javax.swing.*; 6 public class MyGridLayout extends JFrame { 7 private Map<String,JButton> map = new HashMap(); 8 public MyGridLayout (){ 9 10 map.put("B1",new JButton("B1")); 11 map.put("B2",new JButton("B2")); 12 map.put("B3",new JButton("B3")); 13 map.put("B4",new JButton("B4")); 14 map.put("B5",new JButton("B5")); 15 map.put("B6",new JButton("B6")); 16 map.put("B7",new JButton("B7")); 17 map.put("B8",new JButton("B8")); 18 map.put("B9",new JButton("B9")); 19 this.add(map.get("B1")); 20 this.add(map.get("B2")); 21 this.add(map.get("B3")); 22 this.add(map.get("B4")); 23 this.add(map.get("B5")); 24 this.add(map.get("B6")); 25 this.add(map.get("B7")); 26 this.add(map.get("B8")); 27 this.add(map.get("B9")); 28 29 this.setTitle("用户界面"); 30 this.setSize(500, 600); 31 this.setLocation(500, 500); 32 this.setVisible(true); 33 this.setLayout(new GridLayout(3,3,15,15)); 34 35 } 36 public static void main(String[] args) { 37 MyGridLayout l = new MyGridLayout(); 38 39 } 40 41 }
以上是关于java 图形化界面 布局管理器的主要内容,如果未能解决你的问题,请参考以下文章
三十Java图形化界面设计——布局管理器之CardLayout(卡片布局)
三十Java图形化界面设计——布局管理器之GridLayout(网格布局)
Java图形化界面设计——布局管理器之GridBagLayout
Java图形化界面设计——布局管理器之null布局(空布局)