📄 treedemo.java
字号:
/* TreeDemo.java -- Demostrates JTree Copyright (C) 2006 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package gnu.classpath.examples.swing;import java.awt.BorderLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTree;import javax.swing.SwingUtilities;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.DefaultTreeSelectionModel;import javax.swing.tree.TreePath;public class TreeDemo extends JPanel implements ActionListener{ TreeDemo() { super(); createContent(); } private void createContent() { // non-leafs DefaultMutableTreeNode root = new DefaultMutableTreeNode("Exotic Subsistence"); DefaultMutableTreeNode fruit = new DefaultMutableTreeNode("Interesting Fruit"); DefaultMutableTreeNode veg = new DefaultMutableTreeNode("Extraordinary Vegetables"); DefaultMutableTreeNode liq = new DefaultMutableTreeNode("Peculiar Liquids"); // leafs DefaultMutableTreeNode f1 = new DefaultMutableTreeNode("Abiu"); DefaultMutableTreeNode f2 = new DefaultMutableTreeNode("Bamboo Shoots"); DefaultMutableTreeNode f3 = new DefaultMutableTreeNode("Breadfruit"); DefaultMutableTreeNode f4 = new DefaultMutableTreeNode("Canistel"); DefaultMutableTreeNode f5 = new DefaultMutableTreeNode("Duku"); DefaultMutableTreeNode f6 = new DefaultMutableTreeNode("Guava"); DefaultMutableTreeNode f7 = new DefaultMutableTreeNode("Jakfruit"); DefaultMutableTreeNode f8 = new DefaultMutableTreeNode("Quaribea"); DefaultMutableTreeNode v1 = new DefaultMutableTreeNode("Amaranth"); DefaultMutableTreeNode v2 = new DefaultMutableTreeNode("Kiwano"); DefaultMutableTreeNode v3 = new DefaultMutableTreeNode("Leeks"); DefaultMutableTreeNode v4 = new DefaultMutableTreeNode("Luffa"); DefaultMutableTreeNode v5 = new DefaultMutableTreeNode("Chayote"); DefaultMutableTreeNode v6 = new DefaultMutableTreeNode("Jicama"); DefaultMutableTreeNode v7 = new DefaultMutableTreeNode("Okra"); DefaultMutableTreeNode l1 = new DefaultMutableTreeNode("Alcoholic"); DefaultMutableTreeNode l11 = new DefaultMutableTreeNode("Caipirinha"); DefaultMutableTreeNode l21 = new DefaultMutableTreeNode("Mojito"); DefaultMutableTreeNode l31 = new DefaultMutableTreeNode("Margarita"); DefaultMutableTreeNode l41 = new DefaultMutableTreeNode("Martini"); DefaultMutableTreeNode l5 = new DefaultMutableTreeNode("Non Alcoholic"); DefaultMutableTreeNode l55 = new DefaultMutableTreeNode("Babaji"); DefaultMutableTreeNode l65 = new DefaultMutableTreeNode("Chikita"); root.add(fruit); root.add(veg); root.add(liq); fruit.add(f1); fruit.add(f2); fruit.add(f3); fruit.add(f4); fruit.add(f5); fruit.add(f6); fruit.add(f7); fruit.add(f8); veg.add(v1); veg.add(v2); veg.add(v3); veg.add(v4); veg.add(v5); veg.add(v6); veg.add(v7); liq.add(l1); l1.add(l11); l1.add(l21); l1.add(l31); l1.add(l41); liq.add(l5); l5.add(l55); l5.add(l65); final JTree tree = new JTree(root); tree.setLargeModel(true); tree.setEditable(true); DefaultTreeSelectionModel dtsm = new DefaultTreeSelectionModel(); dtsm.setSelectionMode(DefaultTreeSelectionModel.SINGLE_TREE_SELECTION); tree.setSelectionModel(dtsm); // buttons to add and delete JButton add = new JButton("add element"); add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { for (int i = 0; i < tree.getRowCount(); i++) { if (tree.isRowSelected(i)) { TreePath p = tree.getPathForRow(i); DefaultMutableTreeNode n = (DefaultMutableTreeNode) p. getLastPathComponent(); n.add(new DefaultMutableTreeNode("New Element")); tree.repaint(); break; } } } }); setLayout(new BorderLayout()); JPanel p2 = new JPanel(); p2.add(add); add(p2, BorderLayout.NORTH); add(new JScrollPane(tree), BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("CLOSE")) { System.exit(0); } } /** * When the demo is run independently, the frame is displayed, so we should * initialise the content panel (including the demo content and a close * button). But when the demo is run as part of the Swing activity board, * only the demo content panel is used, the frame itself is never displayed, * so we can avoid this step. */ void initFrameContent() { JPanel closePanel = new JPanel(); JButton closeButton = new JButton("Close"); closeButton.setActionCommand("CLOSE"); closeButton.addActionListener(this); closePanel.add(closeButton); add(closePanel, BorderLayout.SOUTH); } public static void main(String[] args) { SwingUtilities.invokeLater (new Runnable() { public void run() { TreeDemo app = new TreeDemo(); app.initFrameContent(); JFrame frame = new JFrame("Tree Demo"); frame.getContentPane().add(app); frame.pack(); frame.setVisible(true); } }); } /** * Returns a DemoFactory that creates a TreeDemo. * * @return a DemoFactory that creates a TreeDemo */ public static DemoFactory createDemoFactory() { return new DemoFactory() { public JComponent createDemo() { return new TreeDemo(); } }; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -