在 JRuby Swing 编程教程的这一部分中,我们将使用对话框。
对话框窗口或对话框是大多数现代 GUI 应用必不可少的部分。 对话被定义为两个或更多人之间的对话。 在计算机应用中,对话框是一个窗口,用于与应用“对话”。 对话框用于输入数据,修改数据,更改应用设置等。对话框是用户与计算机程序之间进行通信的重要手段。
MessageDialog
消息框是方便的对话框,可向应用的用户提供消息。 该消息由文本和图像数据组成。
#!/usr/local/bin/jruby# ZetCode JRuby Swing tutorial## This program demonstrates# message dialogs.## author: Jan Bodnar# website: www.zetcode.com# last modified: December 2010include Javaimport java.awt.GridLayoutimport javax.swing.JFrameimport javax.swing.JButtonimport javax.swing.JPanelimport javax.swing.JOptionPaneclass Example < JFramedef initializesuper "Message boxes"self.initUIenddef initUIpanel = JPanel.newpanel.setLayout GridLayout.new 2, 2errorButton = JButton.new "Error"errorButton.addActionListener do |e|JOptionPane.showMessageDialog panel, "Could not open file","Error", JOptionPane::ERROR_MESSAGEendwarningButton = JButton.new "Warning"warningButton.addActionListener do |e|JOptionPane.showMessageDialog panel, "A deprecated call","Warning", JOptionPane::WARNING_MESSAGEendquestionButton = JButton.new "Question"questionButton.addActionListener do |e|JOptionPane.showMessageDialog panel, "Are you sure to quit?","Question", JOptionPane::QUESTION_MESSAGEendinformButton = JButton.new "Information"informButton.addActionListener do |e|JOptionPane.showMessageDialog panel, "Download completed","Information", JOptionPane::INFORMATION_MESSAGEendpanel.add errorButtonpanel.add warningButtonpanel.add questionButtonpanel.add informButtonself.add panelself.setDefaultCloseOperation JFrame::EXIT_ON_CLOSEself.setSize 300, 200self.setLocationRelativeTo nilself.setVisible trueendendExample.new
我们使用GridLayout管理器来设置四个按钮的网格。 每个按钮显示一个不同的消息框。
errorButton.addActionListener do |e|JOptionPane.showMessageDialog panel, "Could not open file","Error", JOptionPane::ERROR_MESSAGEend
如果按下错误按钮,则会显示错误对话框。 我们使用showMessageDialog方法在屏幕上显示对话框。 此方法的第一个参数是面板,在其中显示对话框。 第二个参数是要显示的消息。 第三个参数是对话框的标题。 最后一个参数是消息类型。 默认图标由消息类型决定。 在本例中,错误对话框的消息类型为ERROR_MESSAGE。

图:错误消息 dialog
JFileChooser
JFileChooser对话框允许用户从文件系统中选择一个文件。
#!/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 2010include Javaimport java.awt.BorderLayoutimport java.awt.Colorimport javax.swing.JFrameimport javax.swing.JButtonimport javax.swing.JPanelimport javax.swing.JToolBarimport javax.swing.JFileChooserimport javax.swing.JTextAreaimport javax.swing.JTextPaneimport javax.swing.JScrollPaneimport javax.swing.BorderFactoryimport javax.swing.filechooser::FileNameExtensionFilterclass Example < JFramedef initializesuper "FileChooser"self.initUIenddef initUI@panel = JPanel.new@panel.setLayout BorderLayout.newtoolbar = JToolBar.newopenb = JButton.new "Choose file"openb.addActionListener do |e|chooseFile = JFileChooser.newfilter = FileNameExtensionFilter.new "c files", "c"chooseFile.addChoosableFileFilter filterret = chooseFile.showDialog @panel, "Choose file"if ret == JFileChooser::APPROVE_OPTIONfile = chooseFile.getSelectedFiletext = self.readFile file@area.setText text.to_sendendtoolbar.add openb@area = JTextArea.new@area.setBorder BorderFactory.createEmptyBorder 10, 10, 10, 10pane = JScrollPane.newpane.getViewport.add @area@panel.setBorder BorderFactory.createEmptyBorder 10, 10, 10, 10@panel.add paneself.add @panelself.add toolbar, BorderLayout::NORTHself.setDefaultCloseOperation JFrame::EXIT_ON_CLOSEself.setSize 450, 400self.setLocationRelativeTo nilself.setVisible trueenddef readFile filefilename = file.getCanonicalPathf = File.open filename, "r"text = IO.readlines filenamereturn textendendExample.new
在我们的代码示例中,我们使用JFileChooser对话框选择一个 C 文件并将其内容显示在JTextArea中。
@area = JTextArea.new
这是JTextArea,我们将在其中显示所选文件的内容。
chooseFile = JFileChooser.newfilter = FileNameExtensionFilter.new "c files", "c"chooseFile.addChoosableFileFilter filter
我们创建JFileChooser对话框的实例。 我们创建一个仅显示 C 文件的过滤器。
ret = chooseFile.showDialog @panel, "Choose file"
对话框显示在屏幕上。 我们得到了返回值。
if ret == JFileChooser::APPROVE_OPTIONfile = chooseFile.getSelectedFiletext = self.readFile file@area.setText text.to_send
如果用户选择了文件,我们将获得文件名。 阅读其内容并将文本设置为文本区域组件。
def readFile filefilename = file.getCanonicalPathf = File.open filename, "r"text = IO.readlines filenamereturn textend
此代码从文件中读取文本。 getCanonicalPath返回绝对文件名。

图:JFileChooser
在 JRuby Swing 教程的这一部分中,我们使用了对话框窗口。
