我对 JRuby 和 Java Swing GUI 开发还很陌生。我正在尝试通过 GUI 获取用户输入并在程序的后面部分使用它。我能够生成 GUI,但不知道如何捕获复选框或下拉列表的状态。
下面是一个简化的例子。1. 如何退出 GUI 但仍继续程序?2. 有没有办法只在用户选择 OK 按钮后捕获 GUI 数据?
谢谢你。
# Menu Title: GUI - Get user input
include Java
import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.JCheckBox
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JLabel
import javax.swing.JComboBox
import javax.swing.JDialog
class Example < JFrame
def initialize
# header title
super("Test Settings")
self.initUI
end
def initUI
# define components
panel = JPanel.new
panel.setLayout(nil)
self.getContentPane.add(panel)
cb_itemA = JCheckBox.new("Check item A", true)
cb_itemB = JCheckBox.new("Check item B", false)
lbl_dropdown = JLabel.new("Traversal: ")
jCombo_dropdown = JComboBox.new()
jCombo_dropdown.addItem("Choice 1")
jCombo_dropdown.addItem("Choice 2")
jCombo_dropdown.setSelectedIndex(1) # default choice is #2
okButton = JButton.new("OK")
cancelButton = JButton.new("Cancel")
# hard-code positions
cb_itemA.setBounds(10, 10, 150, 20)
cb_itemB.setBounds(10, 30, 150, 20)
lbl_dropdown.setBounds(10, 50, 150, 20)
jCombo_dropdown.setBounds(10, 70, 150, 20)
okButton.setBounds(10, 110, 80, 25 )
cancelButton.setBounds(100, 110, 80, 25)
panel.add(cb_itemA); panel.add(cb_itemB)
panel.add(lbl_dropdown); panel.add(jCombo_dropdown)
panel.add(okButton); panel.add(cancelButton)
# == ACTION LISTENERS ==
actionTaken = ClickAction.new()
cb_itemA.addActionListener(actionTaken)
cb_itemB.addActionListener(actionTaken)
jCombo_dropdown.addActionListener(actionTaken)
okButton.addActionListener(actionTaken) # proceed to next code
cancelButton.addActionListener( actionTaken ) # exit script
# == WINDOW SETTINGS ==
win_width = 200
win_height = 180
self.setSize(win_width, win_height)
self.setLocationRelativeTo(nil)
self.setVisible(true)
self.setDefaultCloseOperation(JFrame::DISPOSE_ON_CLOSE)
end
# centralized action handler
class ClickAction
include java.awt.event.ActionListener
def actionPerformed(evt)
if evt.getActionCommand() == "Check item A"
javax.swing.JOptionPane.showMessageDialog(nil, "item A checked")
elsif evt.getActionCommand() == "Check item B"
javax.swing.JOptionPane.showMessageDialog(nil, "item B checked")
elsif evt.getActionCommand() == "Cancel"
# how to exit GUI from here?
elsif evt.getActionCommand() == "OK"
# how to exit GUI from here?
end
end
end
end
# --------------
Example.new
# <use user input selection here>