<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Java SWT 中的布局管理 > 原文: [http://zetcode.com/gui/javaswt/layout/](http://zetcode.com/gui/javaswt/layout/) 在本章中,我們將展示如何在窗口或對話框中布置窗口小部件。 在設計應用的 GUI 時,我們決定使用哪些小部件以及如何在應用中組織這些小部件。 為了組織小部件,我們使用稱為布局容器的專用非可見小部件。 `Composite`是用于放置子窗口小部件的容器。 `Composite`的布局管理器是通過`setLayout()`方法設置的。 `Shell`也是`Composite`。 它沒有默認的布局管理器,在這種情況下,將使用絕對定位來放置小部件。 SWT 具有以下標準布局類: * `FillLayout` * `RowLayout` * `FormLayout` * `GridLayout` `FillLayout`在單個行或列中布置大小相等的小部件。 `RowLayout`在行或列中布置小部件,并具有填充,環繞和間距選項。 `FormLayout`通過為小部件的每一側創建附件來布局小部件。 `GridLayout`將小部件布置在網格中。 布局類可以具有對應的布局數據類,其中包含特定子項的布局數據。 例如,`RowLayout`具有名為`RowData`的布局數據類,`GridLayout`具有`GridData`,而`FormLayout`具有`FormData`。 ## 絕對定位 在大多數情況下,程序員應使用布局管理器。 在某些情況下,我們也可以使用絕對定位。 在絕對定位中,程序員以像素為單位指定每個小部件的位置和大小。 如果我們調整窗口大小,則小部件的大小和位置不會改變。 在各種平臺上,應用看起來都不同,在 Linux 上看起來不錯,在 Mac OS 上看起來不太正常。 在應用中更改字體可能會破壞布局。 如果我們將應用翻譯成另一種語言,則必須重做布局。 對于所有這些問題,僅在有理由的情況下才使用絕對定位,或者您的應用是簡單的測試。 絕對定位是通過`setSize()`,`setLocation()`和`setBounds()`方法完成的。 `AbsoluteLayoutEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * In this program, we position two * buttons using absolute coordinates. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class AbsoluteLayoutEx { public AbsoluteLayoutEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); Button btn1 = new Button(shell, SWT.PUSH); btn1.setText("Button"); btn1.setBounds(20, 50, 80, 30); Button btn2 = new Button(shell, SWT.PUSH); btn2.setText("Button"); btn2.setSize(80, 30); btn2.setLocation(50, 100); shell.setText("Absolute layout"); shell.setSize(300, 250); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); AbsoluteLayoutEx ex = new AbsoluteLayoutEx(display); display.dispose(); } } ``` 在我們的示例中,我們使用絕對定位在窗口上放置了兩個按鈕。 ```java btn1.setBounds(20, 50, 80, 30); ``` `setBounds()`方法有兩件事:將按鈕定位在`x = 20`和`y = 50`,并將按鈕的大小設置為`width = 80`和`height = 30`。 ```java button2.setSize(80, 30); button2.setLocation(50, 100); ``` 在這里,我們分兩個步驟進行相同的操作。 首先,我們使用`setSize()`方法調整按鈕的大小。 然后,我們使用`setLocation()`方法將其定位在窗口上。 ![Absolute layout](https://img.kancloud.cn/61/ba/61baf32672bb4919421153fc3e5c2dde_296x247.jpg) 圖:絕對布局 ## `FillLayout`管理器 `FillLayout`是最簡單的布局類。 它將小部件布置在一行或一列中,迫使它們具有相同的大小。 `FillLayoutEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Device; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This program demonstrates the FillLayout * manager * * Author: Jan Bodnar * Website: zetcode.com * Last modified: May 2015 */ public class FillLayoutEx { private Image castle; public FillLayoutEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); shell.setLayout(new FillLayout()); loadImage(shell); Label label = new Label(shell, SWT.IMAGE_PNG); label.setImage(castle); shell.setText("FillLayout"); Rectangle rect = castle.getBounds(); shell.setSize(rect.width, rect.height); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void loadImage(Shell shell) { Device dev = shell.getDisplay(); try { castle = new Image(dev, "redrock.png"); } catch(Exception e) { System.out.println("Cannot load image"); System.out.println(e.getMessage()); System.exit(1); } } @Override public void finalize() { castle.dispose(); } public static void main(String[] args) { Display display = new Display(); FillLayoutEx app = new FillLayoutEx(display); app.finalize(); display.dispose(); } } ``` 在我們的示例中,我們使用此管理器顯示圖像。 ```java shell.setLayout(new FillLayout()); ``` 我們將`FillLayout`設置為外殼的布局類。 使用`setLayout()`方法設置布局。 ```java Rectangle rect = castle.getBounds(); shell.setSize(rect.width, rect.height); ``` 我們找出圖片的大小來調整外殼的大小,以完全適合圖像的大小。 ```java Label label = new Label(shell, SWT.IMAGE_PNG); label.setImage(castle); ``` 我們將圖像設置為標簽小部件。 ```java private void loadImage(Shell shell) { Device dev = shell.getDisplay(); try { castle = new Image(dev, "redrock.png"); } catch(Exception e) { System.out.println("Cannot load image"); System.out.println(e.getMessage()); System.exit(1); } } ``` `loadImage()`方法從磁盤加載圖像。 ![FillLayout](https://img.kancloud.cn/3e/f7/3ef75be3ca4f6f2a9e53f659479d4f84_452x252.jpg) 圖:`FillLayout` ## `RowLayout` `RowLayout`管理器將所有小部件放置在一行或一列中。 `RowLayoutEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowData; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This program demonstrates the RowLayout * manager. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class RowLayoutEx { public RowLayoutEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL); rowLayout.marginTop = 10; rowLayout.marginBottom = 10; rowLayout.marginLeft = 5; rowLayout.marginRight = 5; rowLayout.spacing = 10; shell.setLayout(rowLayout); Button btn1 = new Button(shell, SWT.PUSH); btn1.setText("Button"); btn1.setLayoutData(new RowData(80, 30)); Button btn2 = new Button(shell, SWT.PUSH); btn2.setText("Button"); btn2.setLayoutData(new RowData(80, 30)); Button btn3 = new Button(shell, SWT.PUSH); btn3.setText("Button"); btn3.setLayoutData(new RowData(80, 30)); shell.setText("RowLayout"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); RowLayoutEx ex = new RowLayoutEx(display); display.dispose(); } } ``` 在我們的示例中,我們創建了三個按鈕的行。 ```java RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL); ``` 將創建水平`RowLayout`。 這些小部件將放置在一行中。 ```java rowLayout.marginTop = 10; rowLayout.marginBottom = 10; rowLayout.marginLeft = 5; rowLayout.marginRight = 5; ``` 邊距指定沿容器邊緣的空間。 ```java rowLayout.spacing = 10; ``` `spacing`屬性指定按鈕之間的間距。 ```java shell.setLayout(rowLayout); ``` 我們將行布局指定為外殼布局。 ```java Button btn1 = new Button(shell, SWT.PUSH); btn1.setText("Button"); btn1.setLayoutData(new RowData(80, 30)); ``` 創建了`Button`。 `setLayoutData()`指定按鈕的大小。 ![RowLayout manager](https://img.kancloud.cn/c6/18/c618f31bda3e4f68731fba66f1df3fc5_272x76.jpg) 圖:`RowLayout`管理器 ## 按鈕 在最后一個示例中,我們使用`FormLayout`管理器創建一個示例。 該管理器使用兩個對象`FormData`和`FormAttachment`控制子項的位置和大小。 `ButtonsEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * In this program, we position two buttons * in the bottom right corner of the window. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: May 2015 */ public class ButtonsEx { public ButtonsEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); FormLayout layout = new FormLayout(); shell.setLayout(layout); Button okBtn = new Button(shell, SWT.PUSH); okBtn.setText("OK"); Button cancBtn = new Button(shell, SWT.PUSH); cancBtn.setText("Cancel"); FormData cancelData = new FormData(80, 30); cancelData.right = new FormAttachment(98); cancelData.bottom = new FormAttachment(95); cancBtn.setLayoutData(cancelData); FormData okData = new FormData(80, 30); okData.right = new FormAttachment(cancBtn, -5, SWT.LEFT); okData.bottom = new FormAttachment(cancBtn, 0, SWT.BOTTOM); okBtn.setLayoutData(okData); shell.setText("Buttons"); shell.setSize(350, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); ButtonsEx ex = new ButtonsEx(display); display.dispose(); } } ``` 在此代碼示例中,我們在窗口的右下角放置了兩個按鈕。 ```java FormLayout layout = new FormLayout(); shell.setLayout(layout); ``` `FormLayout`管理器已創建。 ```java Button okBtn = new Button(shell, SWT.PUSH); okBtn.setText("OK"); Button cancBtn = new Button(shell, SWT.PUSH); cancBtn.setText("Cancel"); ``` 創建兩個按鈕并將其設置到外殼。 ```java FormData cancelData = new FormData(80, 30); ``` 取消按鈕的大小為`80x30`。 ```java cancelData.right = new FormAttachment(98); cancelData.bottom = new FormAttachment(95); ``` 按鈕的右側附著在窗口寬度的 98% 處。 按鈕的底部固定在窗口高度的 95% 處。 ```java okData.right = new FormAttachment(cancelButton, -5, SWT.LEFT); okData.bottom = new FormAttachment(cancelButton, 0, SWT.BOTTOM); ``` “確定”按鈕的右側位于“取消”按鈕的左側 5 像素處。 “確定”按鈕的底部與“取消”按鈕的底部對齊。 ![Buttons](https://img.kancloud.cn/2e/67/2e67696995e5f19321161a491051d3ed_346x197.jpg) 圖:按鈕 ## 新建文件夾 在下面的示例中,我們使用`FormLayout`和`RowLayout`管理器創建窗口布局。 `NewFolderEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.layout.RowData; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; /** * ZetCode Java SWT tutorial * * This program creates a layout using a * FormLayout and a RowLayout. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class NewFolderEx { public NewFolderEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); shell.setLayout(new FormLayout()); Label lbl = new Label(shell, SWT.LEFT); lbl.setText("Name:"); FormData data1 = new FormData(); data1.left = new FormAttachment(0, 5); data1.top = new FormAttachment(0, 10); lbl.setLayoutData(data1); Text text = new Text(shell, SWT.SINGLE); FormData data2 = new FormData(); data2.left = new FormAttachment(lbl, 15); data2.top = new FormAttachment(0, 10); data2.right = new FormAttachment(100, -5); text.setLayoutData(data2); Composite com = new Composite(shell, SWT.NONE); RowLayout rowLayout = new RowLayout(); com.setLayout(rowLayout); Button okBtn = new Button(com, SWT.PUSH); okBtn.setText("OK"); okBtn.setLayoutData(new RowData(80, 30)); Button closeBtn = new Button(com, SWT.PUSH); closeBtn.setText("Close"); closeBtn.setLayoutData(new RowData(80, 30)); FormData data3 = new FormData(); data3.bottom = new FormAttachment(100, -5); data3.right = new FormAttachment(100, 0); com.setLayoutData(data3); Text mainText = new Text(shell, SWT.MULTI | SWT.BORDER); FormData data4 = new FormData(); data4.width = 250; data4.height = 180; data4.top = new FormAttachment(text, 10); data4.left = new FormAttachment(0, 5); data4.right = new FormAttachment(100, -5); data4.bottom = new FormAttachment(com, -10); mainText.setLayoutData(data4); shell.setText("New folder"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); NewFolderEx ex = new NewFolderEx(display); display.dispose(); } } ``` 在示例中,有標簽,文本和按鈕小部件。 ```java Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); shell.setLayout(new FormLayout()); ``` `FormLayout`設置為外殼的主布局管理器。 ```java Label lbl = new Label(shell, SWT.LEFT); lbl.setText("Name:"); FormData data1 = new FormData(); data1.left = new FormAttachment(0, 5); data1.top = new FormAttachment(0, 10); lbl.setLayoutData(data1); ``` 標簽窗口小部件附在窗口的左上角。 ```java Text text = new Text(shell, SWT.SINGLE); FormData data2 = new FormData(); data2.left = new FormAttachment(lbl, 15); data2.top = new FormAttachment(0, 10); data2.right = new FormAttachment(100, -5); text.setLayoutData(data2); ``` 在標簽旁邊,我們放置一個`Text`控件。 文本控件的左側相對于標簽放置。 ```java Composite com = new Composite(shell, SWT.NONE); RowLayout rowLayout = new RowLayout(); com.setLayout(rowLayout); ``` 創建一個`Composite`并將其設置為`RowLayout`管理器。 這兩個按鈕進入該容器。 將`RowLayout`用于按鈕要比直接通過`FormLayout`進行組織要容易一些。 ```java Button okBtn = new Button(com, SWT.PUSH); okBtn.setText("OK"); okBtn.setLayoutData(new RowData(80, 30)); Button closeBtn = new Button(com, SWT.PUSH); closeBtn.setText("Close"); closeBtn.setLayoutData(new RowData(80, 30)); ``` 創建兩個按鈕。 他們的父部件是`Composite`。 ```java FormData data3 = new FormData(); data3.bottom = new FormAttachment(100, -5); data3.right = new FormAttachment(100, 0); com.setLayoutData(data3); ``` `Composite`本身與`FormLayout`一起放置在窗口的底部。 負值是與相鄰小部件或窗口邊界的偏移量。 ```java Text mainText = new Text(shell, SWT.MULTI | SWT.BORDER); FormData data4 = new FormData(); data4.width = 250; data4.height = 180; data4.top = new FormAttachment(text, 10); data4.left = new FormAttachment(0, 5); data4.right = new FormAttachment(100, -5); data4.bottom = new FormAttachment(com, -10); mainText.setLayoutData(data4); ``` 最后,創建了`Text`主窗口小部件。 它占用了大部分窗口區域。 `width`和`height`屬性指定控件的初始首選大小。 ![New folder](https://img.kancloud.cn/98/be/98be4af64b4a3480dedc5b014a54c506_267x302.jpg) 圖:新文件夾 ## `GridLayout` `GridLayout`管理器將其子窗口小部件放入網格中。 `GridLayoutEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example presents the GridLayout. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class GridLayoutEx { public GridLayoutEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); Color col = new Color(display, 100, 200, 100); shell.setBackground(col); col.dispose(); GridLayout layout = new GridLayout(2, false); shell.setLayout(layout); Label lbl1 = new Label(shell, SWT.NONE); GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true); lbl1.setLayoutData(gd1); Color col1 = new Color(display, 250, 155, 100); lbl1.setBackground(col1); col1.dispose(); Label lbl2 = new Label(shell, SWT.NONE); GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true); gd2.heightHint = 100; lbl2.setLayoutData(gd2); Color col2 = new Color(display, 10, 155, 100); lbl2.setBackground(col2); col2.dispose(); Label lbl3 = new Label(shell, SWT.NONE); GridData gd3 = new GridData(SWT.FILL, SWT.FILL, true, true); gd3.widthHint = 300; gd3.heightHint = 100; gd3.horizontalSpan = 2; lbl3.setLayoutData(gd3); Color col3 = new Color(display, 100, 205, 200); lbl3.setBackground(col3); col3.dispose(); shell.setText("Grid"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); GridLayoutEx ex = new GridLayoutEx(display); display.dispose(); } } ``` 在示例中,我們在網格中放置了三個標簽。 每個標簽具有不同的背景色。 ```java Color col = new Color(display, 100, 200, 100); shell.setBackground(col); col.dispose(); ``` `setBackground()`方法為外殼設置背景色。 ```java GridLayout layout = new GridLayout(2, false); shell.setLayout(layout); ``` 實例化`GridLayout`管理器并將其設置為外殼的布局管理器。 網格由 2 列組成。 ```java Label lbl1 = new Label(shell, SWT.NONE); GridData gd1 = new GridData(SWT.FILL, SWT.FILL, true, true); lbl1.setLayoutData(gd1); ``` 第一個標簽進入網格的左上角單元格。 `GridData`類的四個參數使標簽組件填充??其單元格并在兩個方向上擴展。 ```java Label lbl2 = new Label(shell, SWT.NONE); GridData gd2 = new GridData(SWT.FILL, SWT.FILL, true, true); gd2.heightHint = 100; lbl2.setLayoutData(gd2); ``` 第二個標簽轉到相鄰的單元格。 `heightHint`屬性指定標簽的首選高度。 請注意,它也會影響先前的窗口小部件,因為該屬性有效地設置了行的首選高度。 ```java Label lbl3 = new Label(shell, SWT.NONE); GridData gd3 = new GridData(SWT.FILL, SWT.FILL, true, true); gd3.widthHint = 300; gd3.heightHint = 100; gd3.horizontalSpan = 2; lbl3.setLayoutData(gd3); ``` 第三個標簽進入第二行。 `horizontalSpan`屬性使標簽跨越兩列。 ![Simple GridLayout](https://img.kancloud.cn/df/1e/df1ee13bc0198d8c09a24712b5074985_312x241.jpg) 圖:簡單 GridLayout ## 計算器 在下面的示例中,我們使用`GridLayout`管理器創建計算器的框架。 `CalculatorEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; /** * ZetCode Java SWT tutorial * * In this program, we use the GridLayout to * create a calculator skeleton. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class CalculatorEx { public CalculatorEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.DIALOG_TRIM | SWT.CENTER); GridLayout gl = new GridLayout(4, true); gl.marginHeight = 5; shell.setLayout(gl); String[] buttons = { "Cls", "Bck", "", "Close", "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" }; Text text = new Text(shell, SWT.SINGLE); GridData gridData = new GridData(); gridData.horizontalSpan = 4; gridData.horizontalAlignment = GridData.FILL; text.setLayoutData(gridData); for (int i = 0; i < buttons.length; i++) { if (i == 2) { Label lbl = new Label(shell, SWT.CENTER); GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false); lbl.setLayoutData(gd); } else { Button btn = new Button(shell, SWT.PUSH); btn.setText(buttons[i]); GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false); gd.widthHint = 50; gd.heightHint = 30; btn.setLayoutData(gd); } } shell.setText("Calculator"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); CalculatorEx ex = new CalculatorEx(display); display.dispose(); } } ``` 我們使用`GridLayout`管理器創建計算器的框架。 我們使用三種類型的小部件:文本小部件,標簽小部件和幾個按鈕。 ```java Shell shell = new Shell(display, SWT.DIALOG_TRIM | SWT.CENTER); ``` 使用`SWT.DIALOG_TRIM`標志,使窗口不可調整大小。 ```java GridLayout gl = new GridLayout(4, true); gl.marginHeight = 5; shell.setLayout(gl); ``` 我們創建一個具有 4 列的`GridLayout`,并提供頂部和底部頁邊距。 ```java Text text = new Text(shell, SWT.SINGLE); GridData gridData = new GridData(); gridData.horizontalSpan = 4; gridData.horizontalAlignment = GridData.FILL; text.setLayoutData(gridData); ``` `GridData`是與`GridLayout`關聯的布局數據對象。 使用`horizontalSpan`屬性,我們使文本小部件跨越所有四列。 設置為`GridData.FILL`的`horizontalAlignment`使文本窗口小部件填充布局管理器分配給它的整個區域。 ```java Button btn = new Button(shell, SWT.PUSH); btn.setText(buttons[i]); GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false); gd.widthHint = 50; gd.heightHint = 30; btn.setLayoutData(gd); ``` 在`for`循環內,我們創建按鈕并將其放入網格中。 通過`widthHint`和`heightHint`屬性,我們可以設置按鈕的首選大小。 ![Calculator skeleton](https://img.kancloud.cn/1f/49/1f49c046c99bdf4f1b3e736e99cbc2a0_227x232.jpg) 圖:計算機骨架 在 Java SWT 教程的這一部分中,我們討論了小部件的布局管理。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看