开发者

Getting action events of buttons placed in a different component

开发者 https://www.devze.com 2023-03-18 13:20 出处:网络
Is it possible to display a button outside the component in which it is placed. My question seems weird, but the situation is: I have a JFrame. I also have a JPanel class in which I override the paint

Is it possible to display a button outside the component in which it is placed. My question seems weird, but the situation is: I have a JFrame. I also have a JPanel class in which I override the paintComponent() method, getPreferredSize() method etc. I create an object of the JPanel subclass and put it in the JFrame. So far so 开发者_Python百科good. Now I want things inside the JPanel to behave according to a button click. But I don't want to place the button inside the JPanel because it will block some animation going on inside the panel. So I will place it outside the JPanel, maybe in another JPanel in the JFrame. But then how do I get the button click event which is occurring inside the second panel into the first.


In this animation, SubwayPanel has access to a ButtonPanel that controls one aspect of the simulation. When the timer fires, SubwayPanel can check a button's state via ButtonPanel#getButton().

Addendum: A more general solution is to use Action, which "can be used to separate functionality and state from a component." Here's a related example.

0

精彩评论

暂无评论...
验证码 换一张
取 消