<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # JRuby Swing 中的對話框 > 原文: [http://zetcode.com/gui/jrubyswing/dialogs/](http://zetcode.com/gui/jrubyswing/dialogs/) 在 JRuby Swing 編程教程的這一部分中,我們將使用對話框。 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。對話框是用戶與計算機程序之間進行通信的重要手段。 ## `MessageDialog` 消息框是方便的對話框,可向應用的用戶提供消息。 該消息由文本和圖像數據組成。 ```rb #!/usr/local/bin/jruby # ZetCode JRuby Swing tutorial # # This program demonstrates # message dialogs. # # author: Jan Bodnar # website: www.zetcode.com # last modified: December 2010 include Java import java.awt.GridLayout import javax.swing.JFrame import javax.swing.JButton import javax.swing.JPanel import javax.swing.JOptionPane class Example < JFrame def initialize super "Message boxes" self.initUI end def initUI panel = JPanel.new panel.setLayout GridLayout.new 2, 2 errorButton = JButton.new "Error" errorButton.addActionListener do |e| JOptionPane.showMessageDialog panel, "Could not open file", "Error", JOptionPane::ERROR_MESSAGE end warningButton = JButton.new "Warning" warningButton.addActionListener do |e| JOptionPane.showMessageDialog panel, "A deprecated call", "Warning", JOptionPane::WARNING_MESSAGE end questionButton = JButton.new "Question" questionButton.addActionListener do |e| JOptionPane.showMessageDialog panel, "Are you sure to quit?", "Question", JOptionPane::QUESTION_MESSAGE end informButton = JButton.new "Information" informButton.addActionListener do |e| JOptionPane.showMessageDialog panel, "Download completed", "Information", JOptionPane::INFORMATION_MESSAGE end panel.add errorButton panel.add warningButton panel.add questionButton panel.add informButton self.add panel self.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE self.setSize 300, 200 self.setLocationRelativeTo nil self.setVisible true end end Example.new ``` 我們使用`GridLayout`管理器來設置四個按鈕的網格。 每個按鈕顯示一個不同的消息框。 ```rb errorButton.addActionListener do |e| JOptionPane.showMessageDialog panel, "Could not open file", "Error", JOptionPane::ERROR_MESSAGE end ``` 如果按下錯誤按鈕,則會顯示錯誤對話框。 我們使用`showMessageDialog`方法在屏幕上顯示對話框。 此方法的第一個參數是面板,在其中顯示對話框。 第二個參數是要顯示的消息。 第三個參數是對話框的標題。 最后一個參數是消息類型。 默認圖標由消息類型決定。 在本例中,錯誤對話框的消息類型為`ERROR_MESSAGE`。 ![Error message dialog](https://img.kancloud.cn/d5/a9/d5a96ab0fb6b817b361a739380141d3d_264x122.jpg) 圖:錯誤消息 dialog ## `JFileChooser` `JFileChooser`對話框允許用戶從文件系統中選擇一個文件。 ```rb #!/usr/local/bin/jruby # ZetCode JRuby Swing tutorial # # In this program, we use a JFileChooser # to load a c file. # # author: Jan Bodnar # website: www.zetcode.com # last modified: December 2010 include Java import java.awt.BorderLayout import java.awt.Color import javax.swing.JFrame import javax.swing.JButton import javax.swing.JPanel import javax.swing.JToolBar import javax.swing.JFileChooser import javax.swing.JTextArea import javax.swing.JTextPane import javax.swing.JScrollPane import javax.swing.BorderFactory import javax.swing.filechooser::FileNameExtensionFilter class Example < JFrame def initialize super "FileChooser" self.initUI end def initUI @panel = JPanel.new @panel.setLayout BorderLayout.new toolbar = JToolBar.new openb = JButton.new "Choose file" openb.addActionListener do |e| chooseFile = JFileChooser.new filter = FileNameExtensionFilter.new "c files", "c" chooseFile.addChoosableFileFilter filter ret = chooseFile.showDialog @panel, "Choose file" if ret == JFileChooser::APPROVE_OPTION file = chooseFile.getSelectedFile text = self.readFile file @area.setText text.to_s end end toolbar.add openb @area = JTextArea.new @area.setBorder BorderFactory.createEmptyBorder 10, 10, 10, 10 pane = JScrollPane.new pane.getViewport.add @area @panel.setBorder BorderFactory.createEmptyBorder 10, 10, 10, 10 @panel.add pane self.add @panel self.add toolbar, BorderLayout::NORTH self.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE self.setSize 450, 400 self.setLocationRelativeTo nil self.setVisible true end def readFile file filename = file.getCanonicalPath f = File.open filename, "r" text = IO.readlines filename return text end end Example.new ``` 在我們的代碼示例中,我們使用`JFileChooser`對話框選擇一個 C 文件并將其內容顯示在`JTextArea`中。 ```rb @area = JTextArea.new ``` 這是`JTextArea`,我們將在其中顯示所選文件的內容。 ```rb chooseFile = JFileChooser.new filter = FileNameExtensionFilter.new "c files", "c" chooseFile.addChoosableFileFilter filter ``` 我們創建`JFileChooser`對話框的實例。 我們創建一個僅顯示 C 文件的過濾器。 ```rb ret = chooseFile.showDialog @panel, "Choose file" ``` 對話框顯示在屏幕上。 我們得到了返回值。 ```rb if ret == JFileChooser::APPROVE_OPTION file = chooseFile.getSelectedFile text = self.readFile file @area.setText text.to_s end ``` 如果用戶選擇了文件,我們將獲得文件名。 閱讀其內容并將文本設置為文本區域組件。 ```rb def readFile file filename = file.getCanonicalPath f = File.open filename, "r" text = IO.readlines filename return text end ``` 此代碼從文件中讀取文本。 `getCanonicalPath`返回絕對文件名。 ![JFileChooser](https://img.kancloud.cn/57/72/5772a0bd6f7f47af291407b979ba85e3_519x358.jpg) 圖:`JFileChooser` 在 JRuby 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>

                              哎呀哎呀视频在线观看