我创建了一个应用程序,其中有两个选项卡标题为 Tabbedpane 1 和 Tabbedpane 2 。在一个标签页中, Tabbedpane 1 包含一个JInternalFrame
,其中有一个搜索按钮。点击该按钮后,另一个JInternalFrame
会在主JInternalFrame
内打开。
任何人都可以告诉我如何在点击标题为 Tabbedpane 2
的第二个标签时关闭所述已打开的JInternalFrame
即搜索JInternalFrame
我已尝试.setClosed(true)
和.setVisible(true)
进行财产更改,但它对我无效。
答案 0 :(得分: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()
方法,允许我们访问在{中创建的Search类当前实例{1}},在JIFrame1
中我调用changedState(..)
,这将确保我们处理已创建的实例:
jiFrame1.getSearch().dispose()
更新3:
NPE的原因在于:
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
}
改为:
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);
}
}