代码之家  ›  专栏  ›  技术社区  ›  John R Doner

用RUBY创建GUI

  •  0
  • John R Doner  · 技术社区  · 14 年前

    我对Ruby印象深刻,我正在NetBeans中尝试JRuby。但是很难获得更多关于在Swing中使用JRuby的信息。目前,我有以下程序,这是工作,除了评论行。

    require 'java'
    
    include_class 'java.awt.event.ActionListener'
    include_class 'javax.swing.JButton'
    include_class 'javax.swing.JFrame'
    
    class ClickAction
       include ActionListener
    
       def actionPerformed(event)
          puts "Button clicked"
       end
    end #ClickAction
    
    class MainWindow < JFrame
       def initialize
          super "JRuby Swing Demo"
          setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
          button = JButton.new "Click me"
          button.setSize 30, 100                  #this line does nothing
          button.addActionListener ClickAction.new
          add button        
          pack
       end
    end
    
    mainWindow = MainWindow.new
    mainWindow.setSize 300, 300
    mainWindow.setVisible(true)
    

    当我运行此命令时,按钮会自动展开以占据整个窗口。 为什么“setSize”在主窗口上工作,而不是在按钮上。

    还有,它有一个类似于Java的“setBounds”方法吗?

    谢谢你的帮助。我用Java编写自己的布局,这就是我想在JRuby中做的。

    1 回复  |  直到 14 年前
        1
  •  0
  •   lins314159    14 年前

    这与mainWindow的布局有关,与用Java编写时的行为相同。默认值是 BorderLayout ,这将更改框架内容的大小。你可以试试 FlowLayout 相反。

    当然有一个 setBounds 方法。