How would I use a button in Java to open an existing frame in the same package?
I have a bunch of jFrames in the same package. How would I go about opening all of them using buttons from one "Master Frame".
i.e, Master Frame named "Bob" has a bunch of开发者_JAVA技巧 buttons then will allow me to open jFrames that have already been created.
In your event handler, do newFrame.setVisible(true);
You could use this technique. I'm using it to set visible, but you could also use it for creation.
Map<String,Frame> myFrames = new HashMap<String,Frame>();
buttonForFrameA.setActionCommand("FRAME_A");
buttonForFrameB.setActionCommand("FRAME_B");
myFrames.put("FRAME_A",aFrame);
myFrames.put("FRAME_B",bFrame);
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().startsWith("FRAME_") {
for(Frame frame : myFrames.values())
frame.setVisible(false);
Frame selectedFrame = myFrames.get(e.getActionCommand());
if(selectedFrame != null) selectedFrame.setVisible(true);
}
精彩评论