<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>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java SWT 簡介 > 原文: [http://zetcode.com/gui/javaswt/introduction/](http://zetcode.com/gui/javaswt/introduction/) 在 Java SWT 編程教程的這一部分中,我們介紹 Java SWT 庫并創建我們的第一個程序。 本教程的目的是幫助您開始使用 Java SWT 工具包。 可以在此處下載本教程中使用的圖像。 我使用了 Gnome 項目的探戈圖標包中的一些圖標。 ## 關于 標準窗口小部件工具箱(SWT)是用于 Java 編程語言的圖形窗口小部件工具箱。 它最初是由 IBM 開發的。 它是 Swing 和 JavaFX 的替代方法。 SWT 使用 Winapi 和 GTK+ 等本機 GUI API 通過 Java 本機接口(JNI)創建其小部件。 ## 構建 SWT 應用 在 NetBeans 下,我們從官方網站下載了 SWT 包,并將`swt.jar`添加到項目庫中。 ![Adding swt.jar to the NetBeans project](https://img.kancloud.cn/26/98/2698306a18a51a05430ad2006cee1bcb_270x128.jpg) 圖:將`swt.jar`添加到 NetBeans 項目 對于 Eclipse,我們右鍵單擊項目,然后選擇“構建路徑”,“配置構建路徑”。 我們單擊“添加外部 JAR ...”按鈕,然后選擇平臺特定的 JAR 文件。 ## 使窗口居中 在第一個示例中,我們創建一個簡單的窗口。 窗口在屏幕上居中。 `CenterWindowEx.java` ```java package com.zetcode; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * In this program, we show a window in * the center of the screen * * Author: Jan Bodnar * Website: zetcode.com * Last modified: May 2015 */ public class CenterWindowEx { public CenterWindowEx(Display display) { Shell shell = new Shell(display); shell.setText("Center"); shell.setSize(250, 200); centerWindow(shell); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void centerWindow(Shell shell) { Rectangle bds = shell.getDisplay().getBounds(); Point p = shell.getSize(); int nLeft = (bds.width - p.x) / 2; int nTop = (bds.height - p.y) / 2; shell.setBounds(nLeft, nTop, p.x, p.y); } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); CenterWindowEx ex = new CenterWindowEx(display); display.dispose(); } } ``` 本示例在屏幕中央顯示一個`250x200`像素的窗口。 在每個 SWT 應用中,都有兩個重要的類:`Display`和`Shell`。 `Display`是 SWT 與基礎 OS 之間的連接。 它實現了事件循環并提供了有關操作系統的信息。 `Shell`代表一個窗口。 有頂級的外殼; 這些將`Display`作為父項。 其他外殼稱為輔助外殼。 ```java Shell shell = new Shell(display); ``` 創建一個頂層窗口。 ```java shell.setText("Center"); ``` 我們使用`setText()`方法為窗口設置標題。 ```java shell.setSize(250, 200); ``` 在這里,我們為外殼設置大小。 ```java shell.open(); ``` 窗口顯示在屏幕上。 ```java while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } ``` 這些行啟動事件主循環。 ```java Rectangle bds = shell.getDisplay().getBounds(); ``` 我們得到屏幕的分辨率。 如果使用多個顯示器,則可能需要調用`getMonitor()`方法而不是`getDisplay()`。 ```java int nLeft = (bds.width - p.x) / 2; int nTop = (bds.height - p.y) / 2; ``` 我們計算窗口的左坐標和頂坐標。 ```java shell.setBounds(nLeft, nTop, p.x, p.y); ``` 我們使用`setBounds()`方法設置殼的邊界。 ```java Display display = new Display(); ``` 創建了`Display`。 ```java CenterWindowEx ex = new CenterWindowEx(display); ``` 我們實例化示例程序。 ```java display.dispose(); ``` 應用終止后,我們釋放操作系統資源。 ## 創建工具提示 第二個示例顯示了一個工具提示。 工具提示是一個小的矩形窗口,它提供有關對象的簡短信息。 它通常是一個 GUI 組件。 它是應用幫助系統的一部分。 `TooltipEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * In this program, we show a tooltip. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: May 2015 */ public class TooltipEx { public TooltipEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); shell.setText("Tooltip"); shell.setToolTipText("This is a window"); shell.setSize(250, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); TooltipEx ex = new TooltipEx(display); display.dispose(); } } ``` 該示例創建一個窗口。 如果將鼠標指針懸停在窗口區域上方,則會彈出一個工具提示。 ```java Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); ``` `style`參數指定外殼的行為。 傳遞`SWT.CENTER`選項可使外殼位于窗口的中心。 `SWT.SHELL_TRIM`在窗口上裝飾。 它啟用標題和標題欄按鈕,并使窗口可調整大小。 這是外殼的默認樣式。 ```java shell.setToolTipText("This is a window"); ``` 此行為窗口創建工具提示。 ![Tooltip](https://img.kancloud.cn/c9/6f/c96f142e07b785b8c93fa15cd8ccba38_246x197.jpg) 圖:工具提示 ## 退出按鈕 在本節的最后一個示例中,我們將創建一個退出按鈕。 當我們按下此按鈕時,應用終止。 `QuitButtonEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; 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 example shows a button on a window. * Clicking on the button, we terminate * the application. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: May 2015 */ public class QuitButtonEx { public QuitButtonEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout layout = new RowLayout(); layout.marginLeft = 50; layout.marginTop = 50; shell.setLayout(layout); Button quitBtn = new Button(shell, SWT.PUSH); quitBtn.setText("Quit"); quitBtn.setLayoutData(new RowData(80, 30)); quitBtn.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { shell.getDisplay().dispose(); System.exit(0); } }); shell.setText("Quit button"); shell.setSize(250, 200); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); QuitButtonEx ex = new QuitButtonEx(display); display.dispose(); } } ``` 該示例中有一個`Button`小部件; 單擊該按鈕可終止該應用。 ```java RowLayout layout = new RowLayout(); layout.marginLeft = 50; layout.marginTop = 50; shell.setLayout(layout); ``` `RowLayout`用于將按鈕定位在窗口上。 該布局類將小部件放入簡單的行或列中。 ```java Button quitBtn = new Button(shell, SWT.PUSH); ``` `Button`小部件已創建。 它的父級是外殼。 `SWT.PUSH`指定按鈕的類型。 ```java quitBtn.setText("Quit"); ``` 我們使用`setText()`方法為按鈕設置標簽。 ```java quitBtn.setLayoutData(new RowData(80, 30)); ``` `setLayoutData()`方法指定退出按鈕的布局數據。 在這種情況下,這些是按鈕的大小。 ```java quitBtn.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { shell.getDisplay().dispose(); System.exit(0); } }); ``` 我們為按鈕添加一個選擇監聽器。 當我們單擊按鈕時,將調用`widgetSelected()`方法。 在此方法內部,我們釋放 OS 資源并退出應用。 ![Quit button](https://img.kancloud.cn/a6/e9/a6e916a13e68bb6de1c52aeafb2794f7_246x197.jpg) 圖:退出按鈕 ## 助記符 助記符是用于激活支持助記符的窗口小部件的快捷鍵。 例如,它們可以與標簽,按鈕或菜單項一起使用。 助記符是通過在小部件的標簽上添加&字符來創建的。 它使下一個字符成為助記符。 字符與無鼠標修飾符(通常為 `Alt` )結合在一起。 選擇的字符帶有下劃線,但是可以以平臺特定的方式強調。 在某些平臺上,僅在按下無鼠標修飾符后才對字符加下劃線。 `MnemonicEx.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 SWT tutorial * * This program creates a mnemonic for * a button widget. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class MnemonicEx { public MnemonicEx(Display display) { initUI(display); } private void initUI(Display display) { Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout layout = new RowLayout(); layout.marginLeft = 30; layout.marginTop = 30; layout.marginBottom = 150; layout.marginRight = 150; shell.setLayout(layout); Button btn = new Button(shell, SWT.PUSH); btn.setText("&Button"); btn.setLayoutData(new RowData(80, 30)); btn.addListener(SWT.Selection, event -> System.out.println("Button clicked")); shell.setText("Mnemonic"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); MnemonicEx ex = new MnemonicEx(display); display.dispose(); } } ``` 我們為按鈕小部件設置了助記符。 可以使用 `Alt + B` 鍵盤快捷鍵激活。 ```java Button btn = new Button(shell, SWT.PUSH); btn.setText("&Button"); ``` 通過在按鈕的標簽上添加&字符來創建助記符。 `Alt + B` 快捷鍵現在激活該按鈕。 ```java btn.addListener(SWT.Selection, event -> System.out.println("Button clicked")); ``` 激活后,該按鈕會將消息打印到控制臺。 lambda 表達式用于向按鈕添加監聽器。 目前,有三種激活按鈕的方式:單擊鼠標左鍵, `Alt + B` 快捷方式以及`空格鍵` 按鈕具有焦點)。 本章是 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>

                              哎呀哎呀视频在线观看