1

我正在创建一个应用程序,它将表格添加到表单并以特定布局排列它们。每次需要将新表添加到表单时,我都会在 MultiSplitPane 中创建一个新叶,并且我正在将新表添加到叶中。一切正常,直到我移动一个分隔线,然后尝试在里面添加一个带有表格的新叶子

有谁知道是什么导致了这个混乱以及我该如何解决它?

这是您可以复制的有效 SSCCE:

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.GraphicsConfiguration;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitPane;

/**
 * @author Igor
*/
public class Example extends javax.swing.JFrame {

/**
 * Creates new form Example
 */
@Override
public void add(Component cmpnt, Object o) {
    super.add(cmpnt, o);
}

@Override
public void add(PopupMenu pm) {
    super.add(pm);
}

@Override
public boolean action(Event event, Object o) {
    return super.action(event, o);
}

public Example(GraphicsConfiguration gc) {
    super(gc);
}

@Override
public Component add(Component cmpnt, int i) {
    return super.add(cmpnt, i);
}

@Override
public Component add(Component cmpnt) {
    return super.add(cmpnt);
}
// Initialize a MultiSplitPane
MultiSplitPane multiSplitPane = new MultiSplitPane();
int boxCount = 0;
String left = " (LEAF name=left0 weight=1)";
String right = "(LEAF name=right0 weight=0)";
String layoutDef = "(ROW" + left + " " + right + ")";
MultiSplitLayout.Node modelRoot = MultiSplitLayout.parseModel(layoutDef);

public Example() {
    initComponents();

    multiSplitPane.getMultiSplitLayout().setModel(modelRoot);
    setLayout(new BorderLayout());
    add(new JScrollPane(multiSplitPane), BorderLayout.CENTER);
    add(new JButton("Go") {
        {
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Example.this.addNode();
                }
            });
        }
    }, BorderLayout.SOUTH);

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    pack();

}
//a function that adds a new Leaf to the multiSplitPane
private void addNode() {

    if (boxCount == 1) {
        left = " (LEAF name=left0 weight=0.5)";
        right = " (LEAF name=right0 weight=0.5)";
    } else if (boxCount == 2) {
        left = "(COLUMN weight=0.5 left0 left" + new Integer(boxCount - 1).toString() + ")";
    } else if (boxCount == 3) {
        right = "(COLUMN weight=0.5 right0 right" + new Integer(boxCount - 2).toString() + ")";
    } else if ((boxCount % 2 == 0) && (boxCount != 0)) {
        left = left.substring(0, left.length() - 1);
        left += " left" + new Integer(boxCount / 2).toString() + ")";
    } else if ((boxCount % 2 != 0)) {
        right = right.substring(0, right.length() - 1);
        right += " right" + new Integer(boxCount / 2).toString() + ")";
    }
    String layoutDef = "(ROW " + left + " " + right + ")";
    MultiSplitLayout.Node modelRoot = MultiSplitLayout.parseModel(layoutDef);
    multiSplitPane.getMultiSplitLayout().setModel(modelRoot);

    JTable t = new JTable(1, 1);
    t.getColumnModel().getColumn(0).setHeaderValue("Box Title" + boxCount);
    Dimension dimension = new Dimension(190, 190);
    t.setPreferredScrollableViewportSize(dimension);
    t.setRowHeight(800);

    if (boxCount == 0) {
        multiSplitPane.add(new JScrollPane(t), "left0");
    } else if (boxCount == 1) {
        multiSplitPane.add(new JScrollPane(t), "right0");
    } else if (boxCount == 2) {
        multiSplitPane.add(new JScrollPane(t), "left1");
    } else if (boxCount == 3) {
        multiSplitPane.add(new JScrollPane(t), "right1");
    } else if (boxCount % 2 == 0) {
        multiSplitPane.add(new JScrollPane(t), "left" + new Integer(boxCount / 2).toString());
    } else if (boxCount % 2 != 0) {
        multiSplitPane.add(new JScrollPane(t), "right" + new Integer(boxCount / 2).toString());
    }

    multiSplitPane.revalidate();
    boxCount++;
}// End of AddNode *****************

/**
 * 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() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, 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 {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Example.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Example().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
// End of variables declaration
}

PS 运行应用程序时增加窗口大小(或最大化)。

4

2 回答 2

0

SwingX 与 Swing 不同。Swing(javax.swing 包)是 Java 的一部分,SwingX(org.jdesktop.swingx 包)是一个提供附加组件的开源库。

根据你的进口

import org.jdesktop.swingx.MultiSplitPane;

您正在尝试使用 SwingX。最近的版本没有 org.jdesktop.swingx.MultiSplitPane,它们有 JXMultiSplitPane。检查您的 SwingX 版本。

于 2012-09-11T09:10:53.517 回答
0

我认为 multiSplitPane.setFloatingDividers(false) 解决了这个问题。

于 2012-10-10T15:58:24.550 回答