本文介绍了谁能知道实时Java树的编码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
public class MainClass {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JTreeEvents());
f.setSize(500, 500);
f.setVisible(true);
}
}
class JTreeEvents extends JPanel {
JTree tree;
JTextField jtf;
public JTreeEvents() {
setLayout(new BorderLayout());
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
top.add(a);
a.add(new DefaultMutableTreeNode("A1"));
a.add(new DefaultMutableTreeNode("A2"));
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
top.add(b);
b.add(new DefaultMutableTreeNode("B1"));
b.add(new DefaultMutableTreeNode("B2"));
b.add(new DefaultMutableTreeNode("B3"));
tree = new JTree(top);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(tree, v, h);
add(jsp, BorderLayout.CENTER);
jtf = new JTextField("", 20);
add(jtf, BorderLayout.SOUTH);
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
doMouseClicked(me);
}
});
}
void doMouseClicked(MouseEvent me) {
TreePath tp = tree.getPathForLocation(me.getX(), me.getY());
if (tp != null)
jtf.setText(tp.toString());
else
jtf.setText("");
}
}
在上面的编码中,我们给出了输入并获得了相同的输出.
但就我而言,我在网站上输入的内容必须采用这种树状形式.
现在就像
例如:
如果我输入为:
A2
A3
A1
B3
B1
B2
它给出的输出没有树形形式相同
A2
A3
A1
B3
B1
B2
有人可以知道实时Java树的编码吗?
In this above coding, we gave the input and get the same output.
but in my case ,what i have given in my website input that must be in tree form like this.
now it is like
for eg:
If i gave the input as:
A2
A3
A1
B3
B1
B2
it gives the output as same without tree form
A2
A3
A1
B3
B1
B2
Can anyone know the coding for real time java tree?
推荐答案
这篇关于谁能知道实时Java树的编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!