Swing布局管理器介紹_第1頁
Swing布局管理器介紹_第2頁
Swing布局管理器介紹_第3頁
Swing布局管理器介紹_第4頁
Swing布局管理器介紹_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、內(nèi)容面板代碼:public static void addComponentsToPane(Container pane) pane.setLayout(new FlowLayout();pane.add(new JButton("Button 1");pane.add(new JButton("Button 2");pane.add(new JButton("Button 3");pane.add(new JButton("Long-Named Button 4");pane.add(new JButton(&q

2、uot;5");BorderLayout一個BorderLayout對象將界面分成五大區(qū)域,分別用BorderLayout類的靜態(tài)常量指定: -PAGE_START-PAGE_END-LINE_START-LINE_END-CENTER效果:內(nèi)容面板代碼:public static void addComponentsToPane(Container pane) JButton button = new JButton("Button 1 (PAGE_START)"); pane.add(button, BorderLayout.PAGE_START);butto

3、n = new JButton("Button 2 (CENTER)");button.setPreferredSize(new Dimension(200, 100);pane.add(button, BorderLayout.CENTER);button = new JButton("Button 3 (LINE_START)");pane.add(button, BorderLayout.LINE_START);button = new JButton("Long-Named Button 4 (PAGE_END)"); pan

4、e.add(button, BorderLayout.PAGE_END);button = new JButton("5 (LINE_END)");pane.add(button, BorderLayout.LINE_END);BoxLayoutBoxLayout可以將組件由上至下或由左至右依次加入當前面板。效果:內(nèi)容面板代碼:public static void addComponentsToPane(Container pane) JPanel xPanel = new JPanel();xPanel.setLayout(new BoxLayout(xPanel, Bo

5、xLayout.X_AXIS); addButtons(xPanel);JPanel yPanel = new JPanel();yPanel.setLayout(new BoxLayout(yPanel, BoxLayout.Y_AXIS); addButtons(yPanel);pane.add(yPanel, BorderLayout.PAGE_START);pane.add(xPanel, BorderLayout.PAGE_END);private static void addAButton(String text, Container container) JButton but

6、ton = new JButton(text);button.setAlignmentX(Component.CENTER_ALIGNMENT);container.add(button);private static void addButtons(Container container) addAButton("Button 1", container);addAButton("Button 2", container);addAButton("Button 3", container);addAButton("Long

7、-Named Button 4", container);addAButton("5", container);CardLayout卡片布局和其他布局不同,因為它隱藏了一些組件。卡片布局就是一組容器或者組件,它們一次僅僅顯是一個,組中的每個容器稱為卡片。效果:內(nèi)容面板代碼:public void addComponentToPane(Container pane) final JPanel contentPanel = new JPanel();JPanel controlPanel = new JPanel();final CardLayout cardLayo

8、ut=new CardLayout();pane.setLayout(new BorderLayout();pane.add(contentPanel, BorderLayout.CENTER);pane.add(controlPanel, BorderLayout.PAGE_END);controlPanel.setLayout(new FlowLayout();JButton b = new JButton10;for (int i = 0; i < 10; i+) bi = new JButton("No." + i);contentPanel.add(bi);

9、contentPanel.setLayout(cardLayout);JButton nextButton = new JButton("next");nextButton.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) cardLayout.next(contentPanel););controlPanel.add(nextButton);GridLayoutGridLayout讓你建立一個組件表格,并且當組件加入時,會依序又左至右,由上至下填充到每個格子,它

10、不能由你指定想放那個格子就放那個格子效果:內(nèi)容面板代碼:public static void addComponentsToPane(Container pane) JButton buttons = new JButton9;pane.setLayout(new GridLayout(3, 3);for (int i = 0; i < buttons.length; i+) buttonsi = new JButton(i + "");pane.add(buttonsi);GridBagLayoutGridBagLayout是所有AWT布局管理器當中最復雜的,同時他

11、的功能也是最強大的。GridBagLayout同GridLayout一樣,在容器中以網(wǎng)格形式來管理組件。但GridBagLayout功能要來得強大得多。1、GridBagLayout管理的所有行和列都可以是大小不同的;2、GridLayout把每個組件限制到一個單元格,而GridBagLayout并不這樣:組件在容器中可以占據(jù)任意大小的矩形區(qū)域。GridBagLayout通常由一個專用類來對他布局行為進行約束,該類叫GridBagConstraints。其中有11個公有成員變量,GridBagConstraints可以從這11個方面來進行控制和操縱。這些內(nèi)容是:1、gridx組件的橫向坐標;2

12、、girdy組件的縱向坐標;3、gridwidth組件的橫向?qū)挾?,也就是指組件占用的列數(shù);4、gridheight組件的縱向長度,也就是指組件占用的行數(shù);5、weightx指行的權(quán)重,告訴布局管理器如何分配額外的水平空間;6、weighty指列的權(quán)重,告訴布局管理器如何分配額外的垂直空間;7、anchor當組件小于其顯示區(qū)域時使用此字段;8、fill如果顯示區(qū)域比組件的區(qū)域大的時候,可以用來控制組件的行為??刂平M件是垂直填充,還是水平填充,或者兩個方向一起填充;9、insets指組件與表格空間四周邊緣的空白區(qū)域的大小;10、ipadx 組件間的橫向間距,組件的寬度就是這個組件的最小寬度加上ip

13、adx值;11、ipady 組件間的縱向間距,組件的高度就是這個組件的最小高度加上ipady值。說明:1、gridx,gridy:其實就是組件行列的設(shè)置,注意都是從0開始的,比如 gridx=0,gridy=1時放在0行1列;2、gridwidth,gridheight:默認值為1;GridBagConstraints.REMAINDER常量,代表此組件為此行或此列的最后一個組件,會占據(jù)所有剩余的空間;3、weightx,weighty:當窗口變大時,設(shè)置各組件跟著變大的比例。比如組件A的weightx=0.5,組件B的weightx=1,那么窗口X軸變大時剩余的空間就會以1:2的比例分配給組

14、件A和B;4、anchor:當組件空間大于組件本身時,要將組件置于何處。 有CENTER(默認值)、NORTH、NORTHEAST、EAST、SOUTHEAST、WEST、NORTHWEST選擇。5、insets:設(shè)置組件之間彼此的間距。它有四個參數(shù),分別是上,左,下,右,默認為(0,0,0,0)。效果:內(nèi)容面板代碼:public static void addComponentsToPane(Container pane) JButton button;pane.setLayout(new GridBagLayout();GridBagConstraints c = new GridBagC

15、onstraints();button = new JButton("Button 1");c.fill = GridBagConstraints.HORIZONTAL;c.gridx = 0;c.gridy = 0;pane.add(button, c);button = new JButton("Button 2");c.fill = GridBagConstraints.HORIZONTAL;c.weightx = 0.5;c.gridx = 1;c.gridy = 0;pane.add(button, c);button = new JButto

16、n("Button 3");c.fill = GridBagConstraints.HORIZONTAL;c.weightx = 0.5;c.gridx = 2;c.gridy = 0;pane.add(button, c);button = new JButton("Long-Named Button 4"); c.fill = GridBagConstraints.HORIZONTAL;c.ipady = 40; / make this component tallc.weightx = 0.0;c.gridwidth = 3;c.gridx = 0

17、;c.gridy = 1;pane.add(button, c);button = new JButton("5");c.fill = GridBagConstraints.HORIZONTAL;c.ipady = 0; / reset to defaultc.weighty = 1.0; / request any extra vertical spacec.anchor = GridBagConstraints.PAGE_END; / bottom of space c.insets = new Insets(10, 0, 0, 0); / top paddingc.g

18、ridx = 1; / aligned with button 2c.gridwidth = 2; / 2 columns widec.gridy = 2; / third rowpane.add(button, c);一個GardBagLayout布局的左右選擇框,代碼GridBagLayoutFrame.java見附件,效果:GridBagLayoutFrame.java附件源碼public class GridBagLayoutFrame extends JFrame private JButton addButton = new JButton();private JButton le

19、ftButton = new JButton();private JButton rightButton = new JButton();private JLabel label = new JLabel();private JTextField field = new JTextField();private DefaultListModel leftModel = new DefaultListModel(); private DefaultListModel rightMOdel = new DefaultListModel(); private JList leftList = new

20、 JList(leftModel);private JList rightList = new JList(rightMOdel);public GridBagLayoutFrame(String title) setTitle("GridBagLayoutFrameDemo");setPreferredSize(new Dimension(600, 400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); initComponent();addData();pack();setVisible(true);private s

21、tatic void createAndShowGUI() new GridBagLayoutFrame("GridBagLayoutFrameDemo");private void initComponent() label.setText("添加選項:");addButton.setText("添加");leftList.setPreferredSize(new Dimension(150, 150);rightList.setPreferredSize(leftList.getPreferredSize(); leftButto

22、n.setText("左");rightButton.setText("右");mainPanel.setBorder(BorderFactory.createTitledBorder("左右選擇框"); mainPanel.setLayout(new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; / 0行0列 c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.fill =

23、GridBagConstraints.HORIZONTAL; c.weightx = 0; c.weighty = 0; mainPanel.add(label, c); c.gridx+; c.weightx = 1; mainPanel.add(field, c); c.gridx+; c.weightx = 0; c.gridwidth = 1; c.gridheight = 1; / c.fill = GridBagConstraints.HORIZONTAL; mainPanel.add(addButton, c); c.gridx = 0; c.gridy = 1; c.weigh

24、tx = 1; c.weighty = 1; c.gridwidth = 2; c.gridheight = 2; c.fill = GridBagConstraints.BOTH; mainPanel.add(leftList, c); c.gridx = 2; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1; c.weightx = 0; c.weighty = 0.5; c.anchor = GridBagConstraints.SOUTH; c.fill = GridBagConstraints.HORIZONTAL; mainPanel.

25、add(leftButton, c);c.gridx = 2; c.gridy = 2; c.anchor = GridBagConstraints.NORTH; c.fill = GridBagConstraints.HORIZONTAL; mainPanel.add(rightButton, c); c.gridx = 3; c.gridy = 1; c.gridwidth = 1; c.gridheight = 2; c.weightx = 1; c.weighty = 1; c.fill = GridBagConstraints.BOTH; mainPanel.add(rightList, c); this.getContentPane().add(mainPanel); private void addData() addButton.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) addItem(); ); leftButton.addActionListener(new ActionListener() public void actionPerformed(Ac

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論