gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:13:56 24 4
gpt4 key购买 nike

我想知道如何使以下代码替换 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();

关于java - 如何替换 Java Swing 中的 JTree 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6801900/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com