frame.default_close_operation = :exit_on_close

前の日記(http://d.hatena.ne.jp/take_tk/20090309)で

frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE);

の部分が気に入らない。setXX というメソッドは xx= という代入メソッドであるべきだね。これはできるらしい。

frame.default_close_operation = JFrame::EXIT_ON_CLOSE

しかし、「JFrame::EXIT_ON_CLOSE」という定数が気に入らない。次のようなシンボルであるべきだ。

frame.default_close_operation = :exit_on_close

「import javax.swing.JFrame」(JFrame = javax.swing.JFrame)の後で「javax.swing.JFrame」クラスにシンボルを定数に変換する機能を追加してしまえばよい。

ついでに、frame.bounds = [10, 10, 300, 200] というように配列でセットできるようにした。

import javax.swing.JFrame

・・・

####


def sym_to_const( sym, mod )
  return sym unless sym.is_a? Symbol
  mod.const_get(sym.to_s.upcase)
end

class  JFrame

    # frame.default_close_operation = JFrame::EXIT_ON_CLOSE
    #                                   ↓
    # frame.default_close_operation = :exit_on_close
    #
  def setDefaultCloseOperation_with_sym_to_const( const_or_sym , *args )
    setDefaultCloseOperation_without_sym_to_const( sym_to_const( const_or_sym, JFrame ) , *args )
  end
  alias setDefaultCloseOperation_without_sym_to_const  setDefaultCloseOperation
  alias setDefaultCloseOperation     setDefaultCloseOperation_with_sym_to_const
  alias set_default_close_operation  setDefaultCloseOperation_with_sym_to_const
  alias default_close_operation=     setDefaultCloseOperation_with_sym_to_const

    # frame.setBounds( 10, 10, 300, 200 );
    #                      ↓
    # frame.bounds  = [10, 10, 300, 200];
    #
  def setBounds_with_accept_array( *args )
    args = args[0] if args[0].is_a? Array
    setBounds_without_accept_array( *args )
  end
  alias setBounds_without_accept_array  setBounds
  alias setBounds   setBounds_with_accept_array
  alias set_bounds  setBounds_with_accept_array
  alias bounds=     setBounds_with_accept_array

end

####

  # frame = GridBagLayoutTest1.new.frame;
  # frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE);
  # frame.setBounds(10, 10, 300, 200);
  # frame.setTitle("タイトル");
  # frame.setVisible(true);
  #   ↓
frame = GridBagLayoutTest1.new.frame;
frame.default_close_operation = :exit_on_close
frame.bounds  = [10, 10, 300, 200];
frame.title   = "タイトル";
frame.visible = true;