在swing中,TitledBorder可以给一个Border定义一个标题,如何实现把这个标题定义成一个控件,比如按钮控件(JButton)?
谢谢

解决方案 »

  1.   

    构造中标题是字符串参数:String title
    除非你从底层重写,否则不能用JButton当作title用
      

  2.   

    底层重写paTitledBorder的intBorder(Component c, Graphics g, int x, int y, int width, int height)方法.
    此类有一个变量:protected  String title就是这个边框的标题.
      

  3.   

    底层重写TitledBorder的paintBorder(Component c, Graphics g, int x, int y, int width, int height)方法.
    此类有一个变量:protected String title就是这个边框的标题.
      

  4.   

    但是这个title变量的类型还是String类型的呀,如何把他改成JButton类型?
      

  5.   

    建议楼主多研究一下java2D,要知道,JButton也是由Graphics2D画出来的.
    给你介绍一个类吧.
    java.awt.geom.Area类.这个类有一个boolean contains(double x,double y)方法.是用来判断这个坐标是不是在这个区域内的.如果参数为鼠标坐标,就可以满足你的需要了.
      

  6.   

    光画出来不行呀,还要写他的响应事件,比如:actionListener,该如何写?
      

  7.   

    如果你不想重写border得话, 可以换个思路.
    把border看成一个panel, panel得左上角默认有个button.panel里面嵌个panel.
    所有得组件都放在里面得panel上, 外面得panel用定制得layout, 保证button永远在那个位置, 里面得panel永远充满除了button以外得地方.
      

  8.   

    我不太理解“所有得组件都放在里面得panel上”?里面的panel是一个border对吗?
      

  9.   

    http://www.jroller.com/page/santhosh?entry=component_titled_border