Swing组件显示在JTabbedPane中错误的JPanel上



我得到的组件泄漏到JTabbedPane中错误的JPanel上。请参阅以下内容:

https://i.gyazo.com/c41aa45b5782bbbbfb2be322db87b535.mp4

我不知道为什么会发生这种事,我以前从未见过。

这是第一次发射。如果我切换选项卡,它就会停止,不再发生。

它最初看起来像是一个绘画问题,但它出现在完全错误的JPanel上,所以我不明白它是怎么做到的。

public class RepoPanel extends javax.swing.JPanel {
/**
* Creates new form RepoPanel
*/
public RepoPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
FLOWSLIST = new javax.swing.JList<>();
STARTFLOWS = new javax.swing.JButton();
DELETEFLOWS = new javax.swing.JButton();
REFRESHFLOWS = new javax.swing.JButton();
FLOWSLIST.setModel(new javax.swing.AbstractListModel<String>() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public String getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(FLOWSLIST);
STARTFLOWS.setText("Start");
DELETEFLOWS.setText("Delete");
REFRESHFLOWS.setText("Refresh");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(STARTFLOWS)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(DELETEFLOWS)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(REFRESHFLOWS)
.addGap(0, 675, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(STARTFLOWS)
.addComponent(DELETEFLOWS)
.addComponent(REFRESHFLOWS))
.addGap(18, 18, 18)
.addComponent(jScrollPane1)
.addContainerGap())
);
}// </editor-fold>

// Variables declaration - do not modify
private javax.swing.JButton DELETEFLOWS;
private javax.swing.JList<String> FLOWSLIST;
private javax.swing.JButton REFRESHFLOWS;
private javax.swing.JButton STARTFLOWS;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration

}

public class RippleUI extends javax.swing.JFrame {
public GraphEditor editor;
private RepoPanel repoPanel = null;
/**
* Creates new form TabbedUi
*/
public RippleUI() {
repoPanel = new RepoPanel();
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
mxSwingConstants.SHADOW_COLOR = Color.LIGHT_GRAY;
mxConstants.W3C_SHADOWCOLOR = "#D3D3D3";
editor = new GraphEditor();
BACKPANEL = new javax.swing.JPanel();
TABS = new javax.swing.JTabbedPane();
CONTROL_PANEL = new javax.swing.JPanel();
JPanel EDITOR = editor.createFrame(new EditorMenuBar(editor));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(CONTROL_PANEL);
CONTROL_PANEL.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 1020, Short.MAX_VALUE));
jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 556, Short.MAX_VALUE));
TABS.addTab("Control Panel", CONTROL_PANEL);
TABS.addTab("Repository", repoPanel);
repoPanel.setVisible(true);
TABS.addTab("Editor", EDITOR);
javax.swing.GroupLayout BACKPANELLayout = new javax.swing.GroupLayout(BACKPANEL);
BACKPANEL.setLayout(BACKPANELLayout);
BACKPANELLayout.setHorizontalGroup(
BACKPANELLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(TABS));
BACKPANELLayout.setVerticalGroup(
BACKPANELLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(TABS));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(BACKPANEL,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(BACKPANEL,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
// <editor-fold defaultstate="collapsed" desc=" Look and feel setting code
// (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the default
* look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
UIManager.setLookAndFeel(new com.formdev.flatlaf.FlatLightLaf());
} catch (Exception ex) {
System.err.println("Failed to initialize LaF");
}
// </editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new RippleUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JPanel BACKPANEL;
public javax.swing.JPanel CONTROL_PANEL;
public javax.swing.JTabbedPane TABS;
// End of variables declaration

}

问题是这一行:

repoPanel.setVisible(true);

JTabbedPane的工作是决定在任何给定时间都可以看到它的选项卡组件。你自己这么做是在踩JTabbedPane。

请注意,除了窗口之外的所有组件在创建时都是可见的,因此不需要此行。

去掉那条线,事情就会如预期的那样运转。

最新更新