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.
精彩评论