Adding ActionListener to a Panel - Panel implements ActionListener vs Panel HAS A ActionListener

前端 未结 2 1859
温柔的废话
温柔的废话 2021-01-16 10:21

I made a panel for my program. It consists of RadioButtons only. When a radiobutton is selected, I want to set a boolean in other code. This panel will be used as a componen

2条回答
  •  清酒与你
    2021-01-16 11:19

    Option 3. Don't extend JPanel at all if you can reach the goal by using it :-)

    All JSomething have built-in functionality that's designed to be used as-is. The built-in functionality of a JPanel is that of a general purpose container. As all you are doing is to add children, there's no need to extend - configuring the children with listeners can be done in ... configuration code.

    If extending indeed adds functionality (for a JPanel that might be implementing custom background painting), the general rule is - as always for a well-behaved OO citizen - to never expose public api that's not intended for public usage: consequently, variant 1 is not an option.

提交回复
热议问题