JAVA中的常用用三种布局管理器(FlowLayout GridLayout BorderLayout)配合使作

Java中的布局管理

1.  Java中的布局管理器经常用到的也就是三种(流布局管理器FlowLayout、边界布局管理器BorderLayout、网格布局管理器GridLayout),复杂的界面采用单一的布局管理器完成比较困难,一般(流布局管理器、边界布局管理器、网格布局管理器)三者相接合方式去完成。

2.  先在草图上完成所需要布局及组件,再确定采用哪种布局管理器更合理。

3.  一般情况下,先采用边界布局BorderLayout分区,再用网格布局GridLayout定位,后采用流式布局FlowLayout进行调整,对齐方式,水平,垂直方向间距.

4.  先将组件进行流布局FlowLayout加到面板中,再将流面板添加到网格布局GridLayout面板中,后将网格面板添加到边界GridLayout面板中。

5.  仔细阅读一下实例,对以后布局管理器应用有帮助。

6.     参考下面实例:

7.     package SerailComm;

8.     import java.awt.*;

9.     import java.awt.font.*;

10.  import javax.swing.*;

11.  import java.awt.color.*;

12.  import javax.swing.JComponent;

13.  public class SerailComm extends JFrame{

14.  public SerailComm(){

15.           String[]port={"COM1","COM2","CO3","COM4","COM5","COM6","COM7","COM8","COM9","COM10"

16.                              ,"COM11","COM12","COM13","COM14","COM15"};

17.           String[]baud={"2400","4800","9600","115200","384000"};

18.           String[]data={"6","7","8"};

19.           String[]xor={"0","1","2"};

20.           String[]stop={"0","1","1.5","2"};

21.           //定义标签

22.           JLabel lb_port=newJLabel("端口号:");

23.           JLabel lb_baud=newJLabel("波特率:");

24.           JLabel lb_data=newJLabel("数据位:");

25.           JLabel lb_xor=newJLabel("校验位:");

26.           JLabel lb_stop=newJLabel("停止位:");

27.           lb_port.setHorizontalAlignment(SwingConstants.RIGHT);

28.           lb_baud.setHorizontalAlignment(SwingConstants.RIGHT);

29.           lb_data.setHorizontalAlignment(SwingConstants.RIGHT);

30.           lb_xor.setHorizontalAlignment(SwingConstants.RIGHT);

31.           lb_stop.setHorizontalAlignment(SwingConstants.RIGHT);

32.          

33.           //定义下拉列表框

34.           JComboBox box_port=newJComboBox(port);

35.           JComboBox box_baud=newJComboBox(baud);

36.           JComboBox box_data=newJComboBox(data);

37.           JComboBox box_xor=newJComboBox(xor);

38.           JComboBox box_stop=newJComboBox(stop);

39.           //定义面板

40.   

41.           //将北部面板定义为网格布局为1行1列

42.           JPanel northPane = newJPanel(); 

43.           northPane.setLayout(newGridLayout(1, 1));

44.           //将定义左边的面板为网格布局为3行2列

45.           JPanel leftPane = newJPanel(); 

46.           leftPane.setOpaque(false); 

47.           leftPane.setLayout(newGridLayout(3, 2));

48.           //定义流布局1

49.           JPanel letflow = newJPanel();

50.           FlowLayoutflowlayout1=new FlowLayout(FlowLayout.LEFT,5,5);

51.           letflow.setLayout(flowlayout1);

52.           letflow.add(lb_port);

53.           letflow.add(box_port);

54.           //定义流布局2

55.           JPanel lfpan = newJPanel();

56.           FlowLayoutflowlayout2=new FlowLayout(FlowLayout.LEFT,5,5);

57.           lfpan.setLayout(flowlayout2);

58.           lfpan.add(lb_baud);

59.           lfpan.add(box_baud);

60.           //定义流布局3

61.           JPanel lfpan2= newJPanel();

62.           FlowLayoutflowlayout3=new FlowLayout(FlowLayout.LEFT,5,5);

63.           lfpan2.setLayout(flowlayout3);

64.           lfpan2.add(lb_data);

65.           lfpan2.add(box_data);

66.           //将3个流布局的面板,添加到3*2网格布局面板上

67.           leftPane.add(letflow);

68.           leftPane.add(lfpan);

69.           leftPane.add(lfpan2);

70.           //定义右边的面板为网格布局为3行2列

71.           JPanel rightPane = newJPanel(); 

72.           rightPane.setOpaque(false); 

73.           rightPane.setLayout(newGridLayout(3, 2));

74.           //定义流布局1

75.           JPanel rightpan1=newJPanel();

76.           FlowLayoutrightflowlayout1 =new FlowLayout(FlowLayout.LEFT,5,5);

77.           rightpan1.setLayout(rightflowlayout1);

78.           rightpan1.add(lb_xor);

79.           rightpan1.add(box_xor);

80.           //定义流布2

81.           JPanel rightpan2=newJPanel();

82.           FlowLayoutrightflowlayout2 =new FlowLayout(FlowLayout.LEFT,5,5);

83.           rightpan2.setLayout(rightflowlayout2);

84.           rightpan2.add(lb_stop);

85.           rightpan2.add(box_stop);

86.           //定义流布局3

87.           JPanel rightpan3=newJPanel();

88.           FlowLayoutrightflowlayout3 =new FlowLayout(FlowLayout.LEFT,5,5);

89.           rightpan2.setLayout(rightflowlayout3);

90.           JButton openBt=newJButton("打开端口");

91.           JButton closeBt=newJButton("关闭端口");

92.           rightpan3.add(openBt);

93.           rightpan3.add(closeBt);

94.           //添加三个流布局面板到3*2网络面板上

95.           rightPane.add(rightpan1);

96.           rightPane.add(rightpan2);

97.           rightPane.add(rightpan3);

98.           //将左右面板添加到北边的面板上

99.           northPane.add(leftPane);

100.                     northPane.add(rightPane);

101.                     //设置中间面板各组件

102.                     JPanel centerPane = new JPanel();

103.                     centerPane.setOpaque(false); 

104.                     centerPane.setLayout(new GridLayout(2, 2));

105.                     //定义流布局面板1

106.                     JPanel cetpane1=new JPanel();

107.                     FlowLayout centerflow1=new FlowLayout(FlowLayout.LEFT,5,5);

108.                     cetpane1.setLayout(centerflow1);

109.                     JTextField sendTf = new JTextField(20);

110.                     JLabel sendMsgBtn = new JLabel(" 发送数据:"); 

111.                     cetpane1.add(sendMsgBtn);

112.                     cetpane1.add(sendTf);

113.                     //定义流布局面板2

114.                     JPanel cetpane2=new JPanel();

115.                     FlowLayout centerflow2=new FlowLayout(FlowLayout.LEFT,5,0);

116.                     cetpane2.setLayout(centerflow2);

117.                     JTextArea readTa = new JTextArea(2, 20);

118.                     readTa.setEditable(false);

119.                     readTa.setBackground(new Color(255,255, 255)); 

120.                     JLabel reviedRf=new JLabel("接收数据:");

121.                     cetpane2.add(reviedRf);

122.                     cetpane2.add(readTa);

123.                     centerPane.add(cetpane1);

124.                     centerPane.add(cetpane2);

125.                    

126.                     //底部布局

127.                     JPanel south =new JPanel();

128.                     south.setLayout(new GridLayout(1,2));

129.                     south.setOpaque(false);

130.                     //底部流布局

131.                     JPanel southpane=new JPanel();

132.                     FlowLayout souflow=new FlowLayout(FlowLayout.CENTER,5,5);

133.                     southpane.setLayout(souflow);

134.                     JButton enter=new JButton("确认");

135.                     JButton esc =new JButton("退出");

136.                     southpane.add(enter);

137.                     southpane.add(esc);

138.                     south.add(southpane);

139.                     //获取容器组件

140.                     JPanel contentPane = (JPanel) getContentPane(); 

141.                     contentPane.setLayout(new BorderLayout());  

142.                     contentPane.setOpaque(false); 

143.                     contentPane.add(northPane, BorderLayout.NORTH); 

144.                     contentPane.add(centerPane, BorderLayout.CENTER); 

145.                     contentPane.add(south , BorderLayout.SOUTH);

146.                     }

147.                    

148.            public static voidmain(String[] agrs){

149.                     SerailComm frame=new SerailComm();

150.                     frame.setSize(400, 300);

151.                     frame.setTitle("串行通信窗口");

152.                     frame.pack();

153.                     frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

154.                     frame.setVisible(true);

155.            }

156.            }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值