我正在创建一个将表格添加到表单并将其布置在特定布局中的应用程序。每当需要将新表添加到表单中时,我都会在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
}
附言运行应用程序时增加窗口大小(或最大化)。
最佳答案
SwingX与Swing不同。 Swing(javax.swing软件包)是Java的一部分,SwingX(org.jdesktop.swingx软件包)是一个开放源代码库,提供了其他组件。
根据您的进口
import org.jdesktop.swingx.MultiSplitPane;
您正在尝试使用SwingX。最新版本没有org.jdesktop.swingx.MultiSplitPane,它们具有JXMultiSplitPane。检查您的SwingX版本。
关于java - 在MultiSpliPane中安排新叶子的问题(使用SSCCE),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12354608/