我一直在寻找一种使用复选框开发JTree的方法,然后我找到了这个答案:https://stackoverflow.com/a/21851201/5988526 它以某种方式有用,对我来说很好用,它会生成默认数据(颜色,食物等),但是当我尝试输入数据时,节点仅显示前2个字母,例如,节点文本为“ Cimetiere”,并且仅显示“ Ci ..”
package View;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
public class TransfertUI extends JFrame{
JPanel panel = new JPanel();
private JLabel lTitre = new JLabel("Opération de ransfert");
JPanel pSouth = new JPanel(new FlowLayout());
private JLabel label = new JLabel(">>", SwingConstants.CENTER);
private JButton bValider = new JButton("Valider");
private JButton bAnuuler = new JButton("Anuuler");
public TransfertUI() {
JCheckBoxTree tree1 = new JCheckBoxTree();
JCheckBoxTree tree2 = new JCheckBoxTree();
DefaultMutableTreeNode cimetiere=new DefaultMutableTreeNode("Cimetiere");
DefaultMutableTreeNode carre1=new DefaultMutableTreeNode("carré 1 ");
DefaultMutableTreeNode carre2=new DefaultMutableTreeNode("carré 2");
cimetiere.add(carre1);
cimetiere.add(carre2);
DefaultMutableTreeNode t1=new DefaultMutableTreeNode("tombe 1");
DefaultMutableTreeNode t2=new DefaultMutableTreeNode("tombe 2");
DefaultMutableTreeNode d1=new DefaultMutableTreeNode("defunt 1");
DefaultMutableTreeNode d2=new DefaultMutableTreeNode("defunt 2");
carre1.add(t1); carre1.add(t2); t1.add(d1); t2.add(d2);
DefaultTreeModel model = new DefaultTreeModel(cimetiere);
tree1.setModel(model);
// tree2.setModel(model);
JScrollPane scrollPane1 = new JScrollPane(tree1);
JScrollPane scrollPane2 = new JScrollPane(tree2);
panel.setLayout(new GridLayout(1,3));
panel.add(scrollPane1);
panel.add(label);
panel.add(scrollPane2);
this.setLayout(new BorderLayout());
pSouth.add(bValider);
pSouth.add(bAnuuler);
this.add(pSouth,BorderLayout.SOUTH);
this.add(lTitre,BorderLayout.NORTH);
this.add(panel,BorderLayout.CENTER);
this.setSize(650, 400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
this.setTitle("Opération de transfert - Finalys");
}
public static void main(String[] args) {
new TransfertUI();
}
}
在这里您可以看到结果。 对于默认数据,它显示的很好,当我输入数据时,它出错了