我有2幀:父和孩子。我想要執行以下操作: 當父框架被激活或取消消息時,子框架執行相同的操作。Java swing:從另一幀中收聽和重複的事件
private void attachListeners()
{
if (parentFrame != null)
{
parentFrame.addComponentListener(this);
parentFrame.addWindowListener(new ViewEventAdapter(this));
}
@Override
public void viewActivated()
{
// show it when the parent is activated
if (!childFrame.isVisible())
{
childFrame.setVisible(true);
parentFrame.toFront();
}
}
但是這會導致無限循環的閃爍。如果我刪除toFront(),我不能將焦點切換到父框架。我只需要以某種方式正確地切換焦點。
監聽器代碼:
public class ViewEventAdapter implements WindowListener, InternalFrameListener
{
private MyViewListener listener = null;
public ViewEventAdapter(MiaViewListener inListener)
{
listener = inListener;
}
@Override
public void windowActivated(WindowEvent inE)
{
listener.viewActivated();
}
@Override
public void windowClosed(WindowEvent inE)
{
listener.viewClosed();
}
@Override
public void windowClosing(WindowEvent inE)
{
listener.viewClosing();
}
@Override
public void windowDeactivated(WindowEvent inE)
{
listener.viewDeactivated();
}
@Override
public void windowDeiconified(WindowEvent inE)
{
listener.viewDeiconified();
}
@Override
public void windowIconified(WindowEvent inE)
{
listener.viewIconified();
}
@Override
public void windowOpened(WindowEvent inE)
{
listener.viewOpened();
}
@Override
public void internalFrameActivated(InternalFrameEvent inE)
{
listener.viewActivated();
}
@Override
public void internalFrameClosed(InternalFrameEvent inE)
{
listener.viewClosed();
}
@Override
public void internalFrameClosing(InternalFrameEvent inE)
{
listener.viewClosing();
}
@Override
public void internalFrameDeactivated(InternalFrameEvent inE)
{
listener.viewDeactivated();
}
@Override
public void internalFrameDeiconified(InternalFrameEvent inE)
{
listener.viewDeiconified();
}
@Override
public void internalFrameIconified(InternalFrameEvent inE)
{
listener.viewIconified();
}
@Override
public void internalFrameOpened(InternalFrameEvent inE)
{
listener.viewOpened();
}
}
public interface MyViewListener
{
/**
* Called when a view is activated
*/
public void viewActivated();
/**
* Called when a view is closed
*/
public void viewClosed();
/**
* Called when a view is closing
*/
public void viewClosing();
/**
* Called when a view is deactivated
*/
public void viewDeactivated();
/**
* Called when a view is deiconified
*/
public void viewDeiconified();
/**
* Called when a view is iconified
*/
public void viewIconified();
/**
* Called when a view is opened
*/
public void viewOpened();
}
請幫助。提前致謝。
這段代碼甚至沒有編譯:)你可以發佈'ViewEventAdapter'代碼嗎? – GETah 2011-12-21 19:47:17
爲ViewEventAdapter添加代碼 – bunnyjesse112 2011-12-22 07:42:55