gpt4 book ai didi

java - 在更改 JTabbedPane 的选项卡时关闭在另一个 JInternalFrame 中打开的 Opened JInternalFrame

转载 作者:行者123 更新时间:2023-12-04 05:19:28 25 4
gpt4 key购买 nike

我创建了一个应用程序,其中有两个标题为 的选项卡选项卡 Pane 1 选项卡 Pane 2 .在一个标签正文中 选项卡 Pane 1 包含 JInternalFrame其中有一个搜索按钮。单击按钮时,另一个 JInternalFrame在主要 JInternalFrame 内打开.

谁能告诉我如何关闭所说打开的JInternalFrame即搜索JInternalFrame同时单击标题为 的第二个选项卡选项卡 Pane 2

我试过.setClosed(true).setVisible(true)在属性(property)变化上仍然对我不起作用。

最佳答案

使用 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 添加了一个 setter/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);
}
}

关于java - 在更改 JTabbedPane 的选项卡时关闭在另一个 JInternalFrame 中打开的 Opened JInternalFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13824065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com