2

我创建了一个应用程序,其中有两个名为Tabbedpane 1Tabbedpane 2的选项卡。在一个选项卡正文中, Tabbedpane 1包含一个JInternalFrame其中有一个搜索按钮的选项卡。单击按钮时,另一个JInternalFrame在 main 中打开JInternalFrame

谁能告诉我如何在单击标题为Tabbedpane 2JInternalFrame的第二个选项卡时关闭上述打开的搜索JInternalFrame

我已经尝试过.setClosed(true),并且.setVisible(true)在更改属性时仍然对我不起作用。

4

1 回答 1

3

使用状态更改ChangeListener时会收到通知JTabbedPane(即标签切换):

final JTabbedPane tabbedPane = new JTabbedPane();

tabbedPane.addChangeListener(new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
       //tab has been changed
    }
});

问题是我们需要一种方法来监控前一个选项卡,即我们在更改之前所在的选项卡,这可以通过以下方式完成:

tabbedPane.addChangeListener(new ChangeListener() {
    int prev_index = 0;
    int curr_index = 0;

    public void stateChanged(ChangeEvent e) {
        prev_index = curr_index;
        curr_index = tabbedPane.getSelectedIndex();

        System.out.println("Tab (Current): " + curr_index);
        System.out.println("Tab (Previous): " + prev_index);
    }
});

更新 1:

要关闭JInternalFrame,我建议调用dispose()它的实例

更新 2:

这是您的固定代码,基本上为Search类添加了一个 getter,因此JIFrame1有一个getSearch()方法允许我们访问在 中创建的搜索类当前实例JIFrame1,在changedState(..)我调用中jiFrame1.getSearch().dispose(),这将确保我们处理已经创建的实例:

import java.awt.Container;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Sample extends javax.swing.JFrame {

    JIFrame1 jiframe1 = new JIFrame1();

    public Sample() {
        initComponents();

        Container jiframe1cont = tab1;
        for (MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe1.getUI()).getNorthPane().getMouseListeners()) {
            ((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe1.getUI()).getNorthPane().removeMouseListener(listener);
        }
        jiframe1.setLocation(10, 10);
        jiframe1cont.add(jiframe1);
        jiframe1.setVisible(true);

        JIFrame2 jiframe2 = new JIFrame2();
        Container jiframe2cont = tab2;
        for (MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe2.getUI()).getNorthPane().getMouseListeners()) {
            ((javax.swing.plaf.basic.BasicInternalFrameUI) jiframe2.getUI()).getNorthPane().removeMouseListener(listener);
        }
        jiframe2.setLocation(10, 10);
        jiframe2cont.add(jiframe2);
        jiframe2.setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        tab1 = new javax.swing.JDesktopPane();
        tab2 = new javax.swing.JDesktopPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tabbedPane.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        tabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabbedPaneStateChanged(evt);
            }
        });

        tab1.setBackground(new java.awt.Color(240, 240, 240));
        tabbedPane.addTab("Tabbedpane 1", tab1);

        tab2.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        tabbedPane.addTab("Tabbedpane 2", tab2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 731, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(tabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE));

        pack();
    }// </editor-fold>

    private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
        try {
            jiframe1.getSearch().dispose();
        } catch (Exception ex) {
            Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Sample().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JDesktopPane tab1;
    private javax.swing.JDesktopPane tab2;
    private final javax.swing.JTabbedPane tabbedPane = new javax.swing.JTabbedPane();
    // End of variables declaration
}

class Search extends javax.swing.JInternalFrame {

    public Search() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        jTextField1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null},
                    {null, null, null, null}
                },
                new String[]{
                    "Title 1", "Title 2", "Title 3", "Title 4"
                }));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("CLOSE");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE)
                .addContainerGap())
                .addGroup(layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(205, Short.MAX_VALUE))))
                .addGroup(layout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(172, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        this.dispose();
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
}

class JIFrame1 extends javax.swing.JInternalFrame {

    public JIFrame1() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        pane = new javax.swing.JDesktopPane();
        jButton1 = new javax.swing.JButton();

        pane.setBackground(new java.awt.Color(240, 240, 240));

        jButton1.setText("SEARCH");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(170, 60, 90, 23);
        pane.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pane, javax.swing.GroupLayout.DEFAULT_SIZE, 567, Short.MAX_VALUE));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pane, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE));

        pack();
    }// </editor-fold>
    private Search search;

    public Search getSearch() {
        return search;
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        search = new Search();
        Container searchcont = pane;
        for (MouseListener listener : ((javax.swing.plaf.basic.BasicInternalFrameUI) search.getUI()).getNorthPane().getMouseListeners()) {
            ((javax.swing.plaf.basic.BasicInternalFrameUI) search.getUI()).getNorthPane().removeMouseListener(listener);
        }
        search.setLocation(10, 10);
        searchcont.add(search);
        search.setVisible(true);
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JDesktopPane pane;
    // End of variables declaration
}

class JIFrame2 extends javax.swing.JInternalFrame {

    public JIFrame2() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();

        jLabel1.setText("JIFrame2..............");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(100, 100, 100)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(137, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(86, 86, 86)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(145, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
}

更新 3:

NPE的原因在这里:

private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
        try {
            jiframe1.getSearch().dispose();
        } catch (Exception ex) {
            Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

改为:

private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {
    try {
        Search s=jiframe1.getSearch();
        if(s!=null)
        s.dispose();
    } catch (Exception ex) {
        Logger.getLogger(Sample.class.getName()).log(Level.SEVERE, null, ex);
    }
}
于 2012-12-11T16:45:40.957 回答