使用 Swing 在 JRUBY 中创建 GUI



我正在尝试使用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

这是因为您没有向其添加任何内容 - 您只是在操作侦听器块中定义了方法,但没有添加到新帧中 - 大多数方法都已损坏,请参阅注释部分:

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

附言下次请在发布前尝试格式化代码。

相关内容

  • 没有找到相关文章

最新更新