两个按钮(JButton)分别为bt1和bt2,两个内部窗体(JInternalFrame)分别为jf1和jf2
点击bt1就出现jf1,点击bt2就出现jf2。
如何使得点击bt1和bt2时jf1和jf2只能出现一次,并且要满足以下情况:点击bt1后会出现jf1,此时jf1被选中,再点击bt2后出现
jf2,此时jf1和jf2同时出现在JDesktopPane中,被选中的是jf2。在这种情况下,再点击bt1,要使得jf1被选中,应如何实现?

解决方案 »

  1.   

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;public class Test implements ActionListener {    JButton jb1 = new JButton("1");    JButton jb2 = new JButton("2");    JFrame jf = new JFrame();    JPanel pane = new JPanel();    JDesktopPane desktopPane = new JDesktopPane();    JInternalFrame jif1 = null;    JInternalFrame jif2 = null;    public static void main(String[] args) {
            new Test().go();
        }    public void go() {
            JPanel internalPane = new JPanel();
            pane.setLayout(new BorderLayout());
            pane.add(internalPane, BorderLayout.SOUTH);
            internalPane.setLayout(new FlowLayout());
            internalPane.add(jb1);
            internalPane.add(jb2);        jf.setContentPane(pane);
            pane.add(desktopPane);
            jf.setSize(500, 500);
            jf.setVisible(true);
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jb1.addActionListener(this);
            jb2.addActionListener(this);    }    public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if (e.getSource() == jb1) {
                if (jif1 == null) {
                    jif1 = new JInternalFrame("Internal 1", true, true, true, true);
                    jif1.setLocation(100, 100);
                    jif1.setSize(200, 200);
                    jif1.setVisible(true);
                    jif1.add(new JButton("Internal Button1"));
                    desktopPane.add(jif1);
                }
                else {
                    jif1.toFront();
                }
            }
            if (e.getSource() == jb2) {
                if (jif2 == null) {
                    jif2 = new JInternalFrame("Internal 2", true, true, true, true);
                    jif2.setLocation(200, 100);
                    jif2.setSize(200, 200);
                    jif2.setVisible(true);
                    jif2.add(new JButton("Internal Button2"));
                    desktopPane.add(jif2);
                }
                else {
                    jif2.toFront();
                }
            }
        }}