<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/dialogs/](http://zetcode.com/gui/javaswt/dialogs/) 在 Java SWT 教程的這一部分中,我們介紹對話框。 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。對話框是用戶與計算機程序之間進行通信的重要手段。 ## `MessageDialog` `MessageBox`是一個簡單的對話框,向用戶提供一些信息。 它可以包含消息,圖標和各種按鈕。 以可用的`MessageBox`樣式選擇圖標和按鈕。 例如,`SWT.ICON_ERROR`標志在對話框上放置一個錯誤圖標。 `MessageBoxEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a simple MessageBox. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class MessageBoxEx { private Shell shell; public MessageBoxEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout layout = new RowLayout(); layout.marginTop = 50; layout.marginBottom = 150; layout.marginLeft = 50; layout.marginRight = 150; shell.setLayout(layout); Button msgBtn = new Button(shell, SWT.PUSH); msgBtn.setText("Show message"); msgBtn.addListener(SWT.Selection, event -> doShowMessageBox()); shell.setText("Message box"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void doShowMessageBox() { int style = SWT.ICON_INFORMATION | SWT.OK; MessageBox dia = new MessageBox(shell, style); dia.setText("Information"); dia.setMessage("Download completed."); dia.open(); } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); MessageBoxEx ex = new MessageBoxEx(display); display.dispose(); } } ``` 窗口上有一個按鈕。 單擊該按鈕將顯示一個信息消息對話框。 ```java int style = SWT.ICON_INFORMATION | SWT.OK; ``` 該對話框包含一個信息圖標和一個確定按鈕。 ```java MessageBox dia = new MessageBox(shell, style); ``` 創建`MessageBox`的實例,將樣式作為第二個參數。 ```java dia.setText("Information"); ``` 對話框的標題通過`setText()`方法設置。 ```java dia.setMessage("Download completed."); ``` 該消息通過`setMessage()`方法設置。 ![MessageBox](https://img.kancloud.cn/6b/5d/6b5d4f25a03658ec6c66bfbb0f949c87_215x154.jpg) 圖:`MessageBox` ## 請求終止 為了防止數據丟失,以數據為中心的應用在關閉時通常會顯示一個確認對話框。 僅在確認對話框后,應用才會終止。 `MessageBoxEx2.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.MessageBox; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a confirmation dialog * when its closing is initiated. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class MessageBoxEx2 { private Shell shell; public MessageBoxEx2(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); shell.addListener(SWT.Close, event -> doShowMessageBox(event)); shell.setText("Message box"); shell.setSize(350, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void doShowMessageBox(Event event) { int style = SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES | SWT.NO; MessageBox messageBox = new MessageBox(shell, style); messageBox.setText("Information"); messageBox.setMessage("Really close application?"); event.doit = messageBox.open() == SWT.YES; } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); MessageBoxEx2 ex = new MessageBoxEx2(display); display.dispose(); } } ``` 此示例顯示啟動關閉對話框時的確認對話框。 ```java shell.addListener(SWT.Close, event -> doShowMessageBox(event)); ``` 我們將監聽器掛鉤到`SWT.Close`事件類型。 ```java int style = SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES | SWT.NO; ``` 確認對話框是模式對話框,包含一個“問題”圖標以及“是”和“否”按鈕。 (模式對話框將阻止主應用,直到將其關閉。) ```java event.doit = messageBox.open() == SWT.YES; ``` 將`doit`事件設置為`false`會取消該事件; 設置為`true`允許它。 ## 目錄對話框 `DirectoryDialog`是一個對話框,用于選擇特定目錄的路徑。 `DirectoryDialogEx.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.DirectoryDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a directory dialog. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class DirectoryDialogEx { private Shell shell; private Label status; public DirectoryDialogEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); status = new Label(shell, SWT.BORDER); status.setText("Ready"); FormLayout layout = new FormLayout(); shell.setLayout(layout); FormData labelData = new FormData(); labelData.left = new FormAttachment(0); labelData.right = new FormAttachment(100); labelData.bottom = new FormAttachment(100); status.setLayoutData(labelData); shell.addListener(SWT.MouseDown, event -> onMouseDown()); shell.setText("DirectoryDialog"); shell.setSize(350, 250); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void onMouseDown() { DirectoryDialog dialog = new DirectoryDialog(shell); String path = dialog.open(); if (path != null) { status.setText(path); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); DirectoryDialogEx ex = new DirectoryDialogEx(display); display.dispose(); } } ``` 在我們的示例中,我們選擇帶有`DirectoryDialog`的目錄,并在狀態欄中顯示其路徑。 通過單擊窗口區域可以顯示該對話框。 ```java shell.addListener(SWT.MouseDown, event -> onMouseDown()); ``` 將`SWT.MouseDown`事件的鼠標監聽器添加到外殼中。 當我們在窗口上按下鼠標按鈕時,將調用`onMouseDown()`方法。 ```java DirectoryDialog dialog = new DirectoryDialog(shell); ``` 創建了`DirectoryDialog`。 ```java String path = dialog.open(); ``` 我們獲得所選目錄的路徑。 ```java if (path != null) { status.setText(path); } ``` 如果路徑不為空,則在狀態標簽中顯示該路徑。 ![Directory dialog](https://img.kancloud.cn/3b/f6/3bf64d43c812538006eaa571ec4f2e89_491x374.jpg) 圖:目錄對話框 ## `FontDialog` `FontDialog`是用于選擇字體的對話框。 它通常用于進行一些文本編輯或格式化的應用中。 `FontDialogEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FontDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a font dialog. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class FontDialogEx { private Shell shell; private Label label; public FontDialogEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout layout = new RowLayout(); layout.marginHeight = 100; layout.marginWidth = 100; shell.setLayout(layout); label = new Label(shell, SWT.NONE); label.setText("ZetCode Java SWT tutorial"); shell.addListener(SWT.MouseDown, event -> onMouseDown()); shell.setText("FontDialog"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void onMouseDown() { FontDialog dialog = new FontDialog(shell); FontData fdata = dialog.open(); if (fdata != null) { Font font = new Font(shell.getDisplay(), fdata); label.setFont(font); label.pack(); shell.pack(); font.dispose(); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); FontDialogEx ex = new FontDialogEx(display); display.dispose(); } } ``` 在代碼示例中,我們使用`FontDialog`更改標簽的字體。 ```java FontDialog dialog = new FontDialog(shell); ``` `FontDialog`已創建。 ```java Font font = new Font(shell.getDisplay(), fdata); ``` 根據字體數據創建`Font`對象,由字體對話框返回。 ```java label.setFont(font); ``` 字體通過`setFont()`方法應用于標簽。 ```java label.pack(); shell.pack(); ``` `pack()`方法使標簽和外殼適應新的字體類型。 ```java font.dispose(); ``` 字體是操作系統資源。 因此,必須在不再需要時將其丟棄。 ![FontDialog](https://img.kancloud.cn/97/85/9785ebcff90124c35d885e74fa9ac45a_460x363.jpg) 圖:`FontDialog` ## `ColorDialog` `ColorDialog`是用于選擇顏色的對話框。 `ColorDialogEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.ColorDialog; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a color dialog. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class ColorDialogEx { private Shell shell; private Label label; public ColorDialogEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout layout = new RowLayout(); layout.marginHeight = 100; layout.marginWidth = 100; shell.setLayout(layout); label = new Label(shell, SWT.NONE); label.setText("ZetCode Java SWT tutorial"); shell.addListener(SWT.MouseDown, event -> onMouseDown()); shell.setText("ColorDialog"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void onMouseDown() { ColorDialog dialog = new ColorDialog(shell); RGB rgb = dialog.open(); if (rgb != null) { Color col = new Color(shell.getDisplay(), rgb); label.setForeground(col); col.dispose(); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); ColorDialogEx ex = new ColorDialogEx(display); display.dispose(); } } ``` 該示例與上一個示例非常相似。 這次我們更改標簽的顏色。 單擊窗口區域顯示`ColorDialog`。 ```java ColorDialog dialog = new ColorDialog(shell); ``` 我們創建`ColorDialog`。 ```java RGB rgb = dialog.open(); ``` 使用`ColorDialog`的`open()`方法,我們可以獲得 RGB 值。 ```java Color col = new Color(shell.getDisplay(), rgb); label.setForeground(col); ``` 我們獲得顏色值并修改標簽的顏色。 ```java col.dispose(); ``` `Color`是 OS 資源,因此我們在不再需要它時將其處理。 ![ColorDialog](https://img.kancloud.cn/4f/b9/4fb987a503d1fee4685bc5671e2c7fbb_531x318.jpg) 圖:`ColorDialog` ## 文件對話框 `FileDialog`用于選擇文件名。 `FileDialogEx.java` ```java package com.zetcode; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; /** * ZetCode Java SWT tutorial * * This example shows a file dialog. * * Author: Jan Bodnar * Website: zetcode.com * Last modified: June 2015 */ public class FileDialogEx { private Shell shell; private Label label; public FileDialogEx(Display display) { initUI(display); } private void initUI(Display display) { shell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); RowLayout layout = new RowLayout(); layout.marginHeight = 50; layout.marginWidth = 50; shell.setLayout(layout); label = new Label(shell, SWT.NONE); String homeDir = System.getProperty("user.home"); label.setText(homeDir); label.pack(); shell.addListener(SWT.MouseDown, event -> onMouseDown()); shell.setText("FileDialog"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } } private void onMouseDown() { FileDialog dialog = new FileDialog(shell, SWT.OPEN); String[] filterNames = new String[] {"Java sources", "All Files (*)"}; String[] filterExtensions = new String[] {"*.java", "*"}; dialog.setFilterNames(filterNames); dialog.setFilterExtensions(filterExtensions); String path = dialog.open(); if (path != null) { label.setText(path); label.pack(); shell.pack(); } } @SuppressWarnings("unused") public static void main(String[] args) { Display display = new Display(); FileDialogEx ex = new FileDialogEx(display); display.dispose(); } } ``` 該代碼示例使用`FileDialog`選擇文件。 該對話框使用過濾器僅顯示 Java 源。 所選文件的名稱顯示在標簽中。 ```java label = new Label(shell, SWT.NONE); String homeDir = System.getProperty("user.home"); label.setText(homeDir); label.pack(); ``` 首先,標簽小部件將顯示用戶的主目錄。 ```java FileDialog dialog = new FileDialog(shell, SWT.OPEN); ``` 我們用`SWT.OPEN`標志創建一個`FileDialog`。 該對話框可用于打開或保存文件。 對話框的保存行為通過`SWT.SAVE`常量啟用。 ```java String[] filterNames = new String[] {"Java sources", "All Files (*)"}; String[] filterExtensions = new String[] {"*.java", "*"}; dialog.setFilterNames(filterNames); dialog.setFilterExtensions(filterExtensions); ``` 我們使用兩個過濾器; 一種用于 Java 源,一種用于所有文件類型。 ```java String path = dialog.open(); ``` 使用`FileDialog`的`open()`方法檢索路徑名。 ```java if (path != null) { label.setText(path); label.pack(); shell.pack(); } ``` 路徑名通過`setText()`方法設置為標簽。 標簽和外殼通過`pack()`方法適應返回路徑的大小。 Java SWT 教程的這一部分是關于 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>

                              哎呀哎呀视频在线观看