使用秋千在jruby中创造gui

时间:2014-01-18 12:16:34

标签: swing user-interface jruby

我正在尝试使用jruby创建一个gui。我想要做的是当我单击一个按钮并将一些按钮和文本字段添加到新窗口时打开一个新框架。

但我的新窗口没有显示任何按钮。

代码是:

          include Java

             import javax.swing.JButton
              import javax.swing.JFrame
              import javax.swing.JPanel
                import javax.swing.JTextArea
                import javax.swing.JScrollBar
              import javax.swing.JLabel
              import java.lang.System
              import java.awt.Font

        class Project < JFrame

        def initUI

          frame = JFrame.new "My frame"

          panel = JPanel.new
          frame.getContentPane.add panel

          panel.setLayout nil 
           panel.setToolTipText "A Panel container"

           repo = JButton.new "CREATE REPO"
    repo.setBounds 300, 60, 150, 40
    repo.setToolTipText "Create Repo "
    repo.add_action_listener do

    frame1 = JFrame.new "Hello new frame !!"
    frame.setVisible false     

     def mygui
      frame1.newint
     end

        def newint
        panel1 = JPanel.new "panel"
        frame1.getContentPane.add panel1
        panel1.setFlowLayout 

        mybutton = JButton.new "NEW BUTTON"
        mybutton.setBounds 50,30,100,30

        mybutton1 = JButton.new "button"
        mybutton1.setBounds 10,10,10,10

        panel1.add mybutton
        panel1.add mybutton1

      end

      frame1.setVisible true
      frame1.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
      frame1.setSize 500,500

    end


    afile = JButton.new "ADD FILE"
    afile.setBounds 300, 130, 150 , 40
    afile.setToolTipText "Add File"



    efile = JButton.new "EDIT FILE"
    efile.setBounds 300 , 190 , 150 , 40
    efile.setToolTipText "Edit File"

    txtarea = JTextArea.new 
    txtarea.setBounds 10, 60 , 200 , 170
    txtarea.setEditable false

    cancel = JButton.new "CANCEL"
    cancel.setBounds 110 , 250 , 300 , 50
    cancel.setToolTipText "Cancel"
    cancel.add_action_listener do 
       System.exit 0
    end

    label = JLabel.new "GIT INTEGRATED FILESYSTEM"
    label.setFont Font.new "Sans serif" , Font::PLAIN , 14
    label.setBounds 100 ,10 , 300 , 20

    panel.add repo
    panel.add afile
    panel.add efile
    panel.add txtarea
    panel.add cancel
    panel.add label

    frame.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
    frame.setSize 500, 500
    frame.setLocationRelativeTo nil
    frame.setVisible true
            end
            end
            obj = Project.new
            obj.initUI

1 个答案:

答案 0 :(得分:0)

这是因为你没有添加任何东西 - 你只是在动作监听器块中定义了方法,但是没有任何东西被添加到新框架中 - 大多数方法都被破坏了,请参阅注释部分:

include Java

import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JTextArea
import javax.swing.JScrollBar
import javax.swing.JLabel
import java.lang.System
import java.awt.Font

class Project < JFrame

  def initUI

    frame = JFrame.new "My frame"

    panel = JPanel.new
    frame.getContentPane.add panel

    panel.setLayout nil 
    panel.setToolTipText "A Panel container"

    repo = JButton.new "CREATE REPO"
    repo.setBounds 300, 60, 150, 40
    repo.setToolTipText "Create Repo "

    repo.add_action_listener do
      frame1 = JFrame.new "Hello new frame !!"
      frame.setVisible false     

      #def mygui
      #  frame1.newint
      #end

      #def newint
    panel1 = JPanel.new #"panel"
    frame1.getContentPane.add panel1
    #panel1.setFlowLayout 

    mybutton = JButton.new "NEW BUTTON"
    mybutton.setBounds 50,30,100,30

    mybutton1 = JButton.new "button"
    mybutton1.setBounds 10,10,10,10

    panel1.add mybutton
    panel1.add mybutton1

      #end

      frame1.setVisible true
      frame1.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
      frame1.setSize 500,500

    end


    afile = JButton.new "ADD FILE"
    afile.setBounds 300, 130, 150 , 40
    afile.setToolTipText "Add File"


    efile = JButton.new "EDIT FILE"
    efile.setBounds 300 , 190 , 150 , 40
    efile.setToolTipText "Edit File"

    txtarea = JTextArea.new 
    txtarea.setBounds 10, 60 , 200 , 170
    txtarea.setEditable false

    cancel = JButton.new "CANCEL"
    cancel.setBounds 110 , 250 , 300 , 50
    cancel.setToolTipText "Cancel"
    cancel.add_action_listener do 
       System.exit 0
    end

    label = JLabel.new "GIT INTEGRATED FILESYSTEM"
    label.setFont Font.new "Sans serif" , Font::PLAIN , 14
    label.setBounds 100 ,10 , 300 , 20

    panel.add repo
    panel.add afile
    panel.add efile
    panel.add txtarea
    panel.add cancel
    panel.add label

    frame.setDefaultCloseOperation JFrame::EXIT_ON_CLOSE
    frame.setSize 500, 500
    frame.setLocationRelativeTo nil
    frame.setVisible true
  end
end

obj = Project.new
obj.initUI

P.S。下次请在发布前尝试格式化代码。