Ruby with Java swing 表示计数为零
Ruby with Java swing is saying that count is nil
你好,我是 javax.swing 的新手,我想问问你们为什么我会收到这个错误(我会在我的代码后面加上确切的错误)。我已经尝试了我能找到的一切。谢谢你们,我很抱歉,如果这是一个简单的修复,我现在真的很糟糕ruby
testGui.rb
# javaSwingHello.rb
require 'java' # Line 2
JFrame = javax.swing.JFrame
JLabel = javax.swing.JLabel
JPanel = javax.swing.JPanel
JButton = javax.swing.JButton
BorderFactory = javax.swing.BorderFactory
BorderLayout = java.awt.BorderLayout
GridLayout = java.awt.GridLayout
count = 1
frame = JFrame.new
panel = JPanel.new
button = JButton.new "Click me"
button.addActionListener self
label = JLabel.new "Number of clicks: 0"
panel.setBorder BorderFactory.createEmptyBorder(70, 70, 20, 70)
panel.setLayout GridLayout.new(0, 1)
panel.add button
panel.add label
frame.add panel, BorderLayout::CENTER
frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
frame.setTitle "TEST GUI"
frame.pack
frame.setVisible true
def actionPerformed(event)
count += 1
texttoset = "Number of clicks " + count
label.setText(texttoset)
end
错误(当我按下按钮时出现此错误)
Exception in thread "AWT-EventQueue-0" org.jruby.exceptions.NoMethodError: (NoMethodError) undefined method `+' for nil:NilClass
at testGui.actionPerformed(testGui.rb:26)
更多 Ruby 问题 - 局部变量不会被复制到新范围:
def actionPerformed(event)
count += 1
texttoset = "Number of clicks " + count
label.setText(texttoset)
end
count
是方法的局部变量(最初是 nil
),因此失败。
如果你只是想测试一下,你可以使用全局变量来解决这个问题:
$count = 1
def actionPerformed(event)
$count += 1
texttoset = "Number of clicks " + count
label.setText(texttoset)
end
据说不要使用全局变量,而是设置一个适当的对象来封装状态和 'implement' actionPerformed
接口。
你没有说,你将如何使用 actionPerformed
,但一种可能是将方法设为 lambda:
count = 1
$ActionPerformed = -> (event) do
count += 1
texttoset = "Number of clicks " + count
label.setText(texttoset)
end
您必须将其调用为
$ActionPerformed.call(myEvent)
你好,我是 javax.swing 的新手,我想问问你们为什么我会收到这个错误(我会在我的代码后面加上确切的错误)。我已经尝试了我能找到的一切。谢谢你们,我很抱歉,如果这是一个简单的修复,我现在真的很糟糕ruby
testGui.rb
# javaSwingHello.rb
require 'java' # Line 2
JFrame = javax.swing.JFrame
JLabel = javax.swing.JLabel
JPanel = javax.swing.JPanel
JButton = javax.swing.JButton
BorderFactory = javax.swing.BorderFactory
BorderLayout = java.awt.BorderLayout
GridLayout = java.awt.GridLayout
count = 1
frame = JFrame.new
panel = JPanel.new
button = JButton.new "Click me"
button.addActionListener self
label = JLabel.new "Number of clicks: 0"
panel.setBorder BorderFactory.createEmptyBorder(70, 70, 20, 70)
panel.setLayout GridLayout.new(0, 1)
panel.add button
panel.add label
frame.add panel, BorderLayout::CENTER
frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
frame.setTitle "TEST GUI"
frame.pack
frame.setVisible true
def actionPerformed(event)
count += 1
texttoset = "Number of clicks " + count
label.setText(texttoset)
end
错误(当我按下按钮时出现此错误)
Exception in thread "AWT-EventQueue-0" org.jruby.exceptions.NoMethodError: (NoMethodError) undefined method `+' for nil:NilClass
at testGui.actionPerformed(testGui.rb:26)
更多 Ruby 问题 - 局部变量不会被复制到新范围:
def actionPerformed(event)
count += 1
texttoset = "Number of clicks " + count
label.setText(texttoset)
end
count
是方法的局部变量(最初是 nil
),因此失败。
如果你只是想测试一下,你可以使用全局变量来解决这个问题:
$count = 1
def actionPerformed(event)
$count += 1
texttoset = "Number of clicks " + count
label.setText(texttoset)
end
据说不要使用全局变量,而是设置一个适当的对象来封装状态和 'implement' actionPerformed
接口。
你没有说,你将如何使用 actionPerformed
,但一种可能是将方法设为 lambda:
count = 1
$ActionPerformed = -> (event) do
count += 1
texttoset = "Number of clicks " + count
label.setText(texttoset)
end
您必须将其调用为
$ActionPerformed.call(myEvent)