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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java Swing 對話框 > [http://zetcode.com/tutorials/javaswingtutorial/swingdialogs/](http://zetcode.com/tutorials/javaswingtutorial/swingdialogs/) 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。對話框是用戶與計算機程序之間進行通信的重要手段。 在 Java Swing 中,我們可以創建兩種對話框:標準對話框和自定義對話框。自定義對話框由程序員創建。 它們基于`JDialog`類。標準對話框是 Swing 工具箱中可用的預定義對話框,例如`JColorChooser`或`JFileChooser`。 這些是用于常見編程任務的對話框,例如顯示文本,接收輸入,加載和保存文件。 它們節省了程序員的時間,并增強了一些標準行為。 對話框有兩種基本類型:模態對話框和非模態對話框。模態對話框阻止輸入到其他頂級窗口。非模態對話框允許輸入其他窗口。 打開文件對話框是模態對話框的一個很好的例子。 選擇要打開的文件時,不應進行其他操作。 典型的非模態對話框是查找文本對話框。 能夠在文本控件中移動光標并定義從何處開始查找特定文本很方便。 ## `MessageDialog` 消息對話框是向用戶提供信息的簡單對話框。 消息對話框是使用`JOptionPane.showMessageDialog()`方法創建的。 `MessageDialogsEx.java` ```java package com.zetcode; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import java.awt.EventQueue; import static javax.swing.GroupLayout.DEFAULT_SIZE; public class MessageDialogsEx extends JFrame { private JPanel pnl; public MessageDialogsEx() { initUI(); } private void initUI() { pnl = (JPanel) getContentPane(); var warBtn = new JButton("Warning"); var errBtn = new JButton("Error"); var queBtn = new JButton("Question"); var infBtn = new JButton("Information"); warBtn.addActionListener(event -> JOptionPane.showMessageDialog(pnl, "A deprecated call!", "Warning", JOptionPane.WARNING_MESSAGE)); errBtn.addActionListener(event -> JOptionPane.showMessageDialog(pnl, "Could not open file!", "Error", JOptionPane.ERROR_MESSAGE)); queBtn.addActionListener(event -> JOptionPane.showMessageDialog(pnl, "Are you sure to quit?", "Question", JOptionPane.QUESTION_MESSAGE)); infBtn.addActionListener(event -> JOptionPane.showMessageDialog(pnl, "Download completed.", "Information", JOptionPane.INFORMATION_MESSAGE)); createLayout(warBtn, errBtn, queBtn, infBtn); setTitle("Message dialogs"); setSize(300, 200); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); } private void createLayout(JComponent... arg) { var pane = getContentPane(); var gl = new GroupLayout(pane); pane.setLayout(gl); gl.setAutoCreateGaps(true); gl.setHorizontalGroup(gl.createSequentialGroup() .addContainerGap(DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(gl.createParallelGroup() .addComponent(arg[0]) .addComponent(arg[2])) .addGroup(gl.createParallelGroup() .addComponent(arg[1]) .addComponent(arg[3])) .addContainerGap(DEFAULT_SIZE, Short.MAX_VALUE) ); gl.setVerticalGroup(gl.createSequentialGroup() .addContainerGap(DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(gl.createParallelGroup() .addComponent(arg[0]) .addComponent(arg[1])) .addGroup(gl.createParallelGroup() .addComponent(arg[2]) .addComponent(arg[3])) .addContainerGap(DEFAULT_SIZE, Short.MAX_VALUE) ); gl.linkSize(arg[0], arg[1], arg[2], arg[3]); pack(); } public static void main(String[] args) { EventQueue.invokeLater(() -> { var md = new MessageDialogsEx(); md.setVisible(true); }); } } ``` 該示例顯示了錯誤,警告,問題和信息消息對話框。 ```java var warBtn = new JButton("Warning"); var errBtn = new JButton("Error"); var queBtn = new JButton("Question"); var infBtn = new JButton("Information"); ``` 這四個按鈕顯示四個不同的消息對話框。 ```java errBtn.addActionListener(event -> JOptionPane.showMessageDialog(pnl, "Could not open file!", "Error", JOptionPane.ERROR_MESSAGE)); ``` 要創建消息對話框,我們調用`JOptionPane`類的靜態`showMessageDialog()`方法。 我們提供對話框的父項,消息文本,標題和消息類型。 消息類型是以下常量之一: * `ERROR_MESSAGE` * `WARNING_MESSAGE` * `QUESTION_MESSAGE` * `INFORMATION_MESSAGE` 顯示的圖標取決于此常數。 ![Question message dialog](https://img.kancloud.cn/e1/ea/e1ea0c42dd44cf68d4171489f8197244_264x115.jpg) 圖:問題消息對話框 ## 自定義對話框 在下面的示例中,我們創建一個簡單的自定義對話框。 它是有關在許多 GUI 應用中找到的對話框的示例,通常位于“幫助”菜單中。 `CustomDialogEx.java` ```java package com.zetcode; import javax.swing.Box; import javax.swing.GroupLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import java.awt.EventQueue; import java.awt.Font; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import static javax.swing.GroupLayout.Alignment.CENTER; class AboutDialog extends JDialog { public AboutDialog(Frame parent) { super(parent); initUI(); } private void initUI() { var icon = new ImageIcon("src/resources/notes.png"); var imgLabel = new JLabel(icon); var textLabel = new JLabel("Notes, 1.23"); textLabel.setFont(new Font("Serif", Font.BOLD, 13)); var okBtn = new JButton("OK"); okBtn.addActionListener(event -> dispose()); createLayout(textLabel, imgLabel, okBtn); setModalityType(ModalityType.APPLICATION_MODAL); setTitle("About Notes"); setDefaultCloseOperation(DISPOSE_ON_CLOSE); setLocationRelativeTo(getParent()); } private void createLayout(JComponent... arg) { var pane = getContentPane(); var gl = new GroupLayout(pane); pane.setLayout(gl); gl.setAutoCreateContainerGaps(true); gl.setAutoCreateGaps(true); gl.setHorizontalGroup(gl.createParallelGroup(CENTER) .addComponent(arg[0]) .addComponent(arg[1]) .addComponent(arg[2]) .addGap(200) ); gl.setVerticalGroup(gl.createSequentialGroup() .addGap(30) .addComponent(arg[0]) .addGap(20) .addComponent(arg[1]) .addGap(20) .addComponent(arg[2]) .addGap(30) ); pack(); } } public class CustomDialogEx extends JFrame implements ActionListener { public CustomDialogEx() { initUI(); } private void initUI() { createMenuBar(); setTitle("Simple Dialog"); setSize(350, 250); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); } private void createMenuBar() { var menubar = new JMenuBar(); var fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); var helpMenu = new JMenu("Help"); helpMenu.setMnemonic(KeyEvent.VK_H); var aboutMemuItem = new JMenuItem("About"); aboutMemuItem.setMnemonic(KeyEvent.VK_A); helpMenu.add(aboutMemuItem); aboutMemuItem.addActionListener(this); menubar.add(fileMenu); menubar.add(Box.createGlue()); menubar.add(helpMenu); setJMenuBar(menubar); } @Override public void actionPerformed(ActionEvent e) { showAboutDialog(); } private void showAboutDialog() { var aboutDialog = new AboutDialog(this); aboutDialog.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(() -> { var ex = new CustomDialogEx(); ex.setVisible(true); }); } } ``` 從“幫助”菜單中,我們可以彈出一個小對話框。 該對話框顯示文本,圖標和按鈕。 ```java class AboutDialog extends JDialog { ``` 自定義對話框基于`JDialog`類。 ```java setModalityType(ModalityType.APPLICATION_MODAL); ``` `setModalityType()`方法設置對話框的模態類型。 `ModalityType.APPLICATION_MODAL`阻止來自同一應用的所有頂級窗口的輸入。 在我們的例子中,在對話框的生存期內,應用框架的輸入被阻止。 ```java setLocationRelativeTo(getParent()); ``` `setLocationRelativeTo()`方法將對話框窗口居中在框架窗口的區域上方。 ```java setDefaultCloseOperation(DISPOSE_ON_CLOSE); ``` `setDefaultCloseOperation()`設置用戶單擊窗口的“關閉”按鈕時發生的情況。 該對話框將被隱藏和處置。 ```java private void showAboutDialog() { var aboutDialog = new AboutDialog(this); aboutDialog.setVisible(true); } ``` 對話框窗口使用`setVisible()`方法顯示在屏幕上。 ![Custom dialog](https://img.kancloud.cn/84/47/8447d5c2a08258affa286d05ecd9ed98_202x243.jpg) 圖:自定義對話框 ## `JFileChooser` `JFileChooser`是用于從文件系統中選擇文件的標準對話框。 `FileChooserEx.java` ```java package com.zetcode; import javax.swing.AbstractAction; import javax.swing.GroupLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JToolBar; import javax.swing.filechooser.FileNameExtensionFilter; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import static javax.swing.GroupLayout.DEFAULT_SIZE; public class FileChooserEx extends JFrame { private JPanel panel; private JTextArea area; public FileChooserEx() { initUI(); } private void initUI() { panel = (JPanel) getContentPane(); area = new JTextArea(); var spane = new JScrollPane(); spane.getViewport().add(area); var toolbar = createToolBar(); createLayout(toolbar, spane); setTitle("JFileChooser"); setSize(400, 300); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); } private JToolBar createToolBar() { var openIcon = new ImageIcon("src/resources/document-open.png"); var toolbar = new JToolBar(); var openBtn = new JButton(openIcon); openBtn.addActionListener(new OpenFileAction()); toolbar.add(openBtn); return toolbar; } private void createLayout(JComponent... arg) { var pane = getContentPane(); var gl = new GroupLayout(pane); pane.setLayout(gl); gl.setHorizontalGroup(gl.createParallelGroup() .addComponent(arg[0], DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(gl.createSequentialGroup() .addComponent(arg[1])) ); gl.setVerticalGroup(gl.createSequentialGroup() .addComponent(arg[0]) .addGap(4) .addComponent(arg[1]) ); pack(); } public String readFile(File file) { String content = ""; try { content = new String(Files.readAllBytes(Paths.get( file.getAbsolutePath()))); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "Could not read file", "Error", JOptionPane.ERROR_MESSAGE); } return content; } private class OpenFileAction extends AbstractAction { @Override public void actionPerformed(ActionEvent e) { var fileChooser = new JFileChooser(); var filter = new FileNameExtensionFilter("Java files", "java"); fileChooser.addChoosableFileFilter(filter); int ret = fileChooser.showDialog(panel, "Open file"); if (ret == JFileChooser.APPROVE_OPTION) { var file = fileChooser.getSelectedFile(); var text = readFile(file); area.setText(text); } } } public static void main(String[] args) { EventQueue.invokeLater(() -> { var ex = new FileChooserEx(); ex.setVisible(true); }); } } ``` 該代碼示例將演示如何使用`JFileChooser`將文件內容加載到文本區域組件中。 ```java var fileChooser = new JFileChooser(); ``` 這是文件選擇器對話框的構造器。 ```java var filter = new FileNameExtensionFilter("Java files", "java"); fileChooser.addChoosableFileFilter(filter); ``` 在這里,我們定義文件過濾器。 在本例中,我們將具有擴展名為`.java`的 Java 文件。 我們還有默認的“所有文件”選項。 ```java int ret = fileChooser.showDialog(panel, "Open file"); ``` `showDialog()`方法在屏幕上顯示對話框。 單擊“是”或“確定”按鈕時,將返回`JFileChooser.APPROVE_OPTION`。 ```java if (ret == JFileChooser.APPROVE_OPTION) { var file = fileChooser.getSelectedFile(); var text = readFile(file); area.setText(text); } ``` 在這里,我們獲得所選文件的名稱。 我們讀取文件的內容并將文本設置到文本區域中。 ![JFileChooser dialog](https://img.kancloud.cn/6b/84/6b84782d2fd14edab416984e77adb845_519x351.jpg) 圖:`JFileChooser`對話框 ## `JColorChooser` `JColorChooser`是用于選擇顏色的標準對話框。 `ColorChooserEx.java` ```java package com.zetcode; import javax.swing.GroupLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JToolBar; import java.awt.Color; import java.awt.EventQueue; import static javax.swing.GroupLayout.DEFAULT_SIZE; public class ColorChooserEx extends JFrame { private JPanel colourPanel; public ColorChooserEx() { initUI(); } private void initUI() { colourPanel = new JPanel(); colourPanel.setBackground(Color.WHITE); var toolbar = createToolBar(); createLayout(toolbar, colourPanel); setTitle("JColorChooser"); setSize(400, 300); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); } private JToolBar createToolBar() { var openIcon = new ImageIcon("src/resources/colourdlg.png"); var toolbar = new JToolBar(); var openBtn = new JButton(openIcon); openBtn.addActionListener(e -> { var color = JColorChooser.showDialog(colourPanel, "Choose colour", Color.white); colourPanel.setBackground(color); }); toolbar.add(openBtn); return toolbar; } private void createLayout(JComponent... arg) { var pane = getContentPane(); var gl = new GroupLayout(pane); pane.setLayout(gl); gl.setHorizontalGroup(gl.createParallelGroup() .addComponent(arg[0], DEFAULT_SIZE, DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(gl.createSequentialGroup() .addGap(30) .addComponent(arg[1]) .addGap(30)) ); gl.setVerticalGroup(gl.createSequentialGroup() .addComponent(arg[0]) .addGap(30) .addComponent(arg[1]) .addGap(30) ); pack(); } public static void main(String[] args) { EventQueue.invokeLater(() -> { var ex = new ColorChooserEx(); ex.setVisible(true); }); } } ``` 在示例中,我們有一個白色面板。 我們將通過從`JColorChooser`中選擇一種顏色來更改面板的背景顏色。 ```java var color = JColorChooser.showDialog(colourPanel, "Choose colour", Color.white); colourPanel.setBackground(color); ``` 此代碼顯示顏色選擇器對話框。 `showDialog()`方法返回所選的顏色值。 我們將`colourPanel's`背景更改為新選擇的顏色。 在 Java Swing 教程的這一部分中,我們介紹了對話框。
                  <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>

                              哎呀哎呀视频在线观看