开发者

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);

}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜