package com.java.view; import java.awt.Color; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.print.attribute.standard.Sides; import javax.swing.ButtonGroup; import javax.swing.DefaultComboBoxModel; import javax.swing.DefaultListModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JProgressBar; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JSlider; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JTree; import javax.swing.ListSelectionModel; import javax.swing.SwingConstants; import javax.swing.UIManager; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; import javax.swing.table.DefaultTableModel; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper; import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI; import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI.NormalColor; public class HelloWorld { private static TreePath path; public static void main(String[] args) { try { BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated; //UIManager.put("RootPane.setupButtonVisible", false); org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF(); } catch(Exception e) { //TODO exception } // 建立窗體物件 JFrame jFrame =new JFrame(); // 設定窗體大小 jFrame.setSize(800, 500); // 設定窗體全屏展示 //jFrame.setExtendedState(JFrame.MAXIMIZED_BOTH); // 設定窗體顯示位置 //jFrame.setLocation(100,200); // 設定窗體顯示正中間 jFrame.setLocationRelativeTo(null); // 設定窗體標題 jFrame.setTitle("窗體標題"); // 設定窗體不可全屏顯示 //jFrame.setResizable(false); // 設定窗體關閉後退出程式 jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設定此視窗是否應該始終位於其他視窗上方 jFrame.setAlwaysOnTop(true); // 設定窗體圖示 jFrame.setIconImage(new ImageIcon(HelloWorld.class.getResource("/images/book.png")).getImage()); JPanel jPanel =new JPanel(null); DefaultMutableTreeNode root=new DefaultMutableTreeNode("河南"); DefaultMutableTreeNode a=new DefaultMutableTreeNode("鄭州"); DefaultMutableTreeNode b=new DefaultMutableTreeNode("洛陽"); root.add(a); root.add(b); DefaultMutableTreeNode a1=new DefaultMutableTreeNode("金水區"); a.add(a1); JTree tree =new JTree(root); // 設定tree為單選 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); // 新增 JButton addBtn =new JButton("add"); addBtn.setSize(50,40); addBtn.setLocation(210, 10); // 為tree新增一個選擇監聽器,當每次選擇的時候會獲取這個選擇的根節點以及子節點物件 tree.addTreeSelectionListener(new TreeSelectionListener() { @Override public void valueChanged(TreeSelectionEvent e) { path=e.getPath(); System.out.println(path); } }); addBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 獲取被選中的節點 DefaultMutableTreeNode temp=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if(temp ==null) { System.out.println("請選擇一個節點"); }else { temp.add(new DefaultMutableTreeNode("aaaa")); // 重新展開當前選中的節點 tree.expandPath(path); // 重新整理樹 tree.updateUI(); } } }); // 刪除 JButton delBtn =new JButton("del"); delBtn.setSize(50,40); delBtn.setLocation(300, 10); delBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // 獲取被選中的節點 DefaultMutableTreeNode temp=(DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if(temp ==null) { System.out.println("請選擇一個節點"); } if(temp.isRoot()) { System.out.println("根節點不能刪除"); } // 刪除當前選中的節點 temp.removeFromParent(); // 重新展開當前選中的節點 tree.expandPath(path); // 重新整理樹 tree.updateUI(); } }); JScrollPane scrollPane =new JScrollPane(tree); scrollPane.setSize(200,400); scrollPane.setLocation(10, 10); jPanel.add(scrollPane); jPanel.add(addBtn); jPanel.add(delBtn); jFrame.setContentPane(jPanel); // 設定窗體可見 jFrame.setVisible(true); } }