我想知道如何在替换JTree组件时使以下代码正常工作。目前,我正在获取java.lang.NullPointerException

at RandomJTree$1.keyPressed(RandomJTree.java:64)
at java.awt.Component.processKeyEvent(Component.java:6221)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2801)
at java.awt.Component.processEvent(Component.java:6040)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1848)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:704)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:969)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:841)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:668)
at java.awt.Component.dispatchEventImpl(Component.java:4502)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)


如何替换Java Swing中的JTree组件?

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;


public class RandomJTree extends JFrame {

    private final JTextField jtf = new JTextField(10);
    private JTree tree;
    private Random generator = new Random();

    private JTree buildTree(int nnodes) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(0);
        DefaultMutableTreeNode cnode = node;
        for (int i = 1; i <= nnodes; i++) {
            System.out.println(i);
            DefaultMutableTreeNode newn = new DefaultMutableTreeNode(i);
            int j=generator.nextInt(2);System.out.println("j="+j);
            if ( j== 0 && i != 1) {
                DefaultMutableTreeNode parent = (DefaultMutableTreeNode)cnode.getParent();
                if (parent != null) {
                    parent.add(newn);
                    cnode = parent;
                } else {
                    node.add(newn);
                    cnode = newn;
                }
            } else {
                cnode.add(newn);
                cnode = newn;
            }
        }
        return new JTree(node);
    }

    public RandomJTree() {
        setLayout(new BorderLayout());
        add(jtf, BorderLayout.NORTH);
        tree = buildTree(10);
        add(tree, BorderLayout.CENTER);

        jtf.addKeyListener(new KeyAdapter() {
                public void keyPressed(KeyEvent e) {
                    int key = e.getKeyCode();
                    if (key == KeyEvent.VK_ENTER) {
                        tree =  buildTree(Integer.getInteger(jtf.getText()));
                        tree.revalidate();
                        tree.repaint();
                    }
                }
            });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        new RandomJTree();
    }
}

最佳答案

您要使用Integer.getInteger(String)而不是Interger.parseInt(String)

/ e1
使用它,树将随机化:

invalidate();
remove(tree);
tree = buildTree(Integer.parseInt(jtf.getText()));
add(tree, BorderLayout.CENTER);
validate();

09-12 18:21