JFrame居中
方法一:
setLocationRelativeTo(null); php
注意:必須在整個frame初始化完成後再加上此語句,不然將顯示在屏幕右下角 html
方法二:
private Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
/** 屏幕寬度 */
private int screenWidth = screenSize.width;
/** 屏幕高度 */
private int screenHeight = screenSize.height;
jFrame.setLocation((screenWidth - FRAME_WIDTH) / 2, (screenHeight - FRAME_HEIGHT) / 2); java
package component.tree; import java.awt.Dialog; import java.awt.Dimension; import java.awt.Point; import java.awt.Toolkit; import javax.swing.JDialog; import javax.swing.JTree; /*2015-5-26*/ public class MyDialog extends JDialog{ private JTree tree=new JTree(); public MyDialog(Dialog owner) { super(owner,true); setSize(800,600); add(tree); setLocation(getCenterPoint()); } private Point getCenterPoint(){ int windowWidth = this.getWidth(); //得到窗口寬 int windowHeight = this.getHeight(); //得到窗口高 Toolkit kit = Toolkit.getDefaultToolkit(); //定義工具包 Dimension screenSize = kit.getScreenSize(); //獲取屏幕的尺寸 int screenWidth = screenSize.width; //獲取屏幕的寬 int screenHeight = screenSize.height; //獲取屏幕的高 return new Point(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);//窗口居中時的x,y } public JTree getTree() { return tree; } }
package component.tree; import static common.SwingConsole.run; import java.awt.HeadlessException; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; /*2015-5-26*/ public class Main extends JFrame { public Main() throws HeadlessException { super(); JButton button=new JButton("OK"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { MyDialog dlg=new MyDialog(null); DefaultMutableTreeNode root = new DefaultMutableTreeNode("資源管理器"); DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("個人公文包"); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("個人電腦"); DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("收藏夾"); DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Readme"); DefaultTreeModel treeModel = new DefaultTreeModel(root); /* * DefaultTreeModel類所提供的insertNodeInto()方法加入節點到父節點的數量. * 利用DefaultMutableTreeNode類所提供的getChildCount()方法取得目前子節點的數量. */ treeModel.insertNodeInto(node1, root, root.getChildCount()); treeModel.insertNodeInto(node2, root, root.getChildCount()); treeModel.insertNodeInto(node3, root, root.getChildCount()); treeModel.insertNodeInto(node4, root, root.getChildCount()); DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("公司文件"); // DefaultTreeModel類所提供的insertNodeInto()方法加入節點到父節點的數量. treeModel.insertNodeInto(leafnode, node1, node1.getChildCount()); leafnode = new DefaultMutableTreeNode("我的信件"); treeModel.insertNodeInto(leafnode, node1, node1.getChildCount()); leafnode = new DefaultMutableTreeNode("私人文件"); treeModel.insertNodeInto(leafnode, node1, node1.getChildCount()); leafnode = new DefaultMutableTreeNode("本機磁盤(C:)"); treeModel.insertNodeInto(leafnode, node2, node2.getChildCount()); leafnode = new DefaultMutableTreeNode("本機磁盤(D:)"); treeModel.insertNodeInto(leafnode, node2, node2.getChildCount()); leafnode = new DefaultMutableTreeNode("本機磁盤(E:)"); treeModel.insertNodeInto(leafnode, node2, node2.getChildCount()); DefaultMutableTreeNode node31 = new DefaultMutableTreeNode("網站列表"); treeModel.insertNodeInto(node31, node3, node3.getChildCount()); leafnode = new DefaultMutableTreeNode("奇摩站"); treeModel.insertNodeInto(leafnode, node3, node3.getChildCount()); leafnode = new DefaultMutableTreeNode("職棒消息"); treeModel.insertNodeInto(leafnode, node3, node3.getChildCount()); leafnode = new DefaultMutableTreeNode("網絡書店"); treeModel.insertNodeInto(leafnode, node3, node3.getChildCount()); dlg.getTree().setModel(treeModel); dlg.setVisible(true); } }); add(button); } public static void main(String[] args) { run(new Main(),200,100); } }
package common; import javax.swing.JFrame; import javax.swing.SwingUtilities; /*2015-5-26*/ public class SwingConsole { public static void run(final JFrame frame,final int width,final int height){ SwingUtilities.invokeLater(new Runnable() { @Override public void run() { frame.setSize(width, height); frame.setTitle(frame.getClass().getSimpleName()); frame.setVisible(true); } }); } }
http://www.iteedu.com/plang/java/jtswingchxshj/56.phpnode
http://www.blogjava.net/santicom/archive/2011/03/23/346796.html網絡
http://www.cnblogs.com/happyPawpaw/archive/2013/01/10/2854675.htmlapp
http://etwo.iteye.com/blog/1458414less
package swing.tree; import static common.SwingConsole.run; import javax.swing.JFrame; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; /*2015-6-1*/ public class JTreeDemo extends JFrame{ private static final long serialVersionUID = 1L; public JTreeDemo() { JTree tree=new JTree(); /* DefaultMutableTreeNode root=new DefaultMutableTreeNode("個人電腦"); DefaultMutableTreeNode c=new DefaultMutableTreeNode("C:"); DefaultMutableTreeNode d=new DefaultMutableTreeNode("D:"); DefaultMutableTreeNode c1=new DefaultMutableTreeNode("C1"); DefaultMutableTreeNode c2=new DefaultMutableTreeNode("C2");*/ Dir dir=new Dir("root", "My computer", "Y"); Dir c=new Dir("C:", "第一個盤", "N"); Dir d=new Dir("D:", "第二個盤", "Y"); Dir c1=new Dir("c1", "盤一C1", "Y"); Dir c2=new Dir("c2", "盤一C2", "Y"); DefaultMutableTreeNode root=new DefaultMutableTreeNode(dir); DefaultMutableTreeNode cNode=new DefaultMutableTreeNode(c); DefaultMutableTreeNode dNode=new DefaultMutableTreeNode(d); DefaultMutableTreeNode c1Node=new DefaultMutableTreeNode(c1); DefaultMutableTreeNode c2Node=new DefaultMutableTreeNode(c2); DefaultTreeModel model=new DefaultTreeModel(root); model.insertNodeInto(cNode, root, root.getChildCount()); model.insertNodeInto(dNode, root, root.getChildCount()); model.insertNodeInto(c1Node, cNode,cNode.getChildCount()); model.insertNodeInto(c2Node, cNode, cNode.getChildCount()); tree.setModel(model); this.add(tree); tree.setSize(400, 200); } public static void main(String[] args) { run(new JTreeDemo(), 800, 600); } } class Dir{ private String path; private String name; private String access; public Dir(String path, String name, String access) { super(); this.path = path; this.name = name; this.access = access; } @Override public String toString() { return path;//GUI上樹顯示的節點信息 } }
package swing.tree; import static common.SwingConsole.run; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import javax.swing.BorderFactory; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTree; import javax.swing.border.Border; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; import com.sun.security.auth.UserPrincipal; /*2015-6-1*/ public class JTreeDemo extends JFrame { private static final long serialVersionUID = 1L; private JTree tree; private DefaultMutableTreeNode root; private DefaultTreeModel model; private JPanel content; private Border inBorder = BorderFactory.createLineBorder(Color.LIGHT_GRAY); public JTreeDemo() { initGUI(); initListener(); } private void initGUI() { content = new JPanel(); content.setLayout(new BorderLayout()); getContentPane().add(content); root = new DefaultMutableTreeNode(); model = new DefaultTreeModel(root); for (int i = 1; i <= 3; i++) { DemoNode cate = new DemoNode(produceImage("arrow_left.png"), new UserPrincipal("個人好友" + i)); for (int j = 1; j <= 3; j++) { DemoNode node = new DemoNode(produceImage("avatar.png"), new UserPrincipal("好友" + i + "-" + j), "人生若只如初見"); cate.add(node); } root.add(cate); } tree = new JTree(); tree.setModel(model); tree.setRootVisible(false); tree.setToggleClickCount(1); tree.setCellRenderer(new DemoRenderer());// 更改節點圖標 tree.putClientProperty("JTree.lineStyle", "None");// 去掉父子節點之間的線條 content.add(tree, BorderLayout.CENTER); } private Icon produceImage(String name) { ImageIcon backImage = new ImageIcon(getClass().getClassLoader().getResource(name)); return backImage; } private void initListener() { tree.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { TreePath path = tree.getSelectionPath(); if (null != path) { Object object = path.getLastPathComponent(); DemoNode node = (DemoNode) object; // 二級節點(分組)咱就無論了 if (node.getLevel() == 2) { node.nodeContent.setBorder(inBorder); node.nodeContent.setBackground(Color.ORANGE); // 去掉選中以外其餘全部節點的特效 for (int i = 0; i < root.getChildCount(); i++) { DemoNode cate = (DemoNode) root.getChildAt(i); // 三級節點 for (int j = 0; j < cate.getChildCount(); j++) { DemoNode buddy = (DemoNode) cate.getChildAt(j); if (node != buddy) { buddy.nodeContent.setBorder(null); buddy.nodeContent.setBackground(null); } model.reload(buddy); } } model.reload(node); } } } }); tree.addMouseMotionListener(new MouseMotionAdapter() { // 沒有使用moseEnter事件,是由於此方法裏面沒有能夠獲取當前節點的API // 你們如果知道有更好的方法,能夠告訴我一下,感激涕零 // 還有,你們看到以下方法,循環太多,若是節點過多的話,效率就不行了,可是事件只能加到jtree上,若是能加到我重繪的UI上就行了。。。 @Override public void mouseMoved(MouseEvent e) { TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (null != path) { Object object = path.getLastPathComponent(); DemoNode node = (DemoNode) object; // 無論三七二十一,先把本身的特效加上再說 // 這裏由於是鼠標移動事件,觸發太快了,因此要判斷是否在當前節點上移動鼠標 if (node.getLevel() == 1 && node.cateContent.getBorder() != inBorder) { node.cateContent.setBorder(inBorder); } if (node.getLevel() == 2 && node.nodeContent.getBorder() != inBorder) { node.nodeContent.setBorder(inBorder); } model.reload(node); // 去掉選中以外其餘全部節點的特效 for (int i = 0; i < root.getChildCount(); i++) { DemoNode cate = (DemoNode) root.getChildAt(i); if (node != cate && cate.cateContent.getBackground() != Color.ORANGE) { cate.cateContent.setBorder(null); } model.reload(cate); // 三級節點 for (int j = 0; j < cate.getChildCount(); j++) { DemoNode buddy = (DemoNode) cate.getChildAt(j); if (node != buddy && buddy.nodeContent.getBackground() != Color.ORANGE) { buddy.nodeContent.setBorder(null); } model.reload(buddy); } } } } }); } public static void main(String[] args) { run(new JTreeDemo(), 300, 600); } } class UserObject { private String path; private String name; private String access; public UserObject(String path, String name, String access) { super(); this.path = path; this.name = name; this.access = access; } @Override public String toString() { return name;// GUI上樹顯示的節點信息 } }
package swing.tree; import java.awt.Component; import java.awt.Dimension; import javax.swing.Icon; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.tree.DefaultMutableTreeNode; /*2015-6-7*/ public class DemoNode extends DefaultMutableTreeNode{ private static final long serialVersionUID = 1L; /** 圖片 */ private Icon icon; /** 節點信息 */ private Object userObject; /** 簽名 */ private String sign; public JPanel cateContent; public JPanel nodeContent; public JLabel iconLabel; public JLabel nameLabel; public JLabel signLabel; /** * 初始化分組節點 * @param name 名稱 */ public DemoNode(Icon icon, Object name) { this.icon = icon; this.userObject = name; // 初始化UI initCateGUI(); } /** * 初始化好友節點 * @param icon 頭像 * @param nick 暱稱 * @param sign 簽名 */ public DemoNode(Icon icon, Object nick, String sign) { this.icon = icon; this.userObject = nick; this.sign = sign; // 初始化UI initNodeGUI(); } /** * 自定義分組UI */ private void initCateGUI() { cateContent = new JPanel(); cateContent.setLayout(null); // cateContent.setOpaque(false); // 這裏你們注意,當咱們寫好UI以後可能會發現他的顏色不太對, // 這時候千萬不要用上面那句,否則當咱們想再次改變其顏色的時候,就生效不了 // 紅綠藍分別爲255的這個顏色趨近於透明,咱們能夠用它來代替setOpaque // cateContent.setBackground(new Color(255,255,255)); // 忽然發現置成null也能夠 cateContent.setBackground(null); cateContent.setPreferredSize(new Dimension(300, 25)); // cateContent.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); iconLabel = new JLabel(icon); iconLabel.setBounds(6, 5, 20, 16); cateContent.add(iconLabel); nameLabel = new JLabel(userObject.toString()); nameLabel.setBounds(23, 0, 132, 28); cateContent.add(nameLabel); } /** * 自定義好友UI */ private void initNodeGUI() { nodeContent = new JPanel(); nodeContent.setLayout(null); // nodeContent.setOpaque(false); nodeContent.setBackground(null); nodeContent.setPreferredSize(new Dimension(300, 50)); // nodeContent.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY)); iconLabel = new JLabel(icon); iconLabel.setBounds(8, 4, 39, 42); nodeContent.add(iconLabel); nameLabel = new JLabel(userObject.toString()); nameLabel.setBounds(59, 5, 132, 19); nodeContent.add(nameLabel); signLabel = new JLabel(sign); signLabel.setBounds(59, 28, 132, 17); nodeContent.add(signLabel); } /** * 將自定義UI返回給渲染器 <br/> * 供渲染器調用,返回的必須是一個Component * @return */ public Component getCateView() { return cateContent; } /** * 將自定義UI返回給渲染器 <br/> * 供渲染器調用,返回的必須是一個Component * @return */ public Component getNodeView() { return nodeContent; } }
package swing.tree; import java.awt.Component; import javax.swing.ImageIcon; import javax.swing.JTree; import javax.swing.tree.DefaultTreeCellRenderer; public class DemoRenderer extends DefaultTreeCellRenderer { private static final long serialVersionUID = 1L; @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { // DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; // // 根節點從0開始,依次往下 // // 分組 // if (node.getLevel() == 1) { // if (expanded) { // this.setIcon(new ImageIcon(getClass().getClassLoader().getResource("arrow_down.png"))); // } else { // this.setIcon(new ImageIcon(getClass().getClassLoader().getResource("arrow_left.png"))); // } // } // // 好友 // if (node.getLevel() == 2) { // this.setIcon(new ImageIcon(getClass().getClassLoader().getResource("qq_icon.png"))); // } // this.setText(value.toString()); // return this; // 你們這裏注意,我爲了節省時間,因此就沒有寫兩個node類 // 全部的代碼寫在了同一個類中,而後根據不一樣的節點來調用相應的方法 DemoNode node = (DemoNode) value; if (node.getLevel() == 1) { if (expanded) { node.iconLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("arrow_down.png"))); } else { node.iconLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("arrow_left.png"))); } return node.getCateView(); } if (node.getLevel() == 2) { node.iconLabel.setIcon(new ImageIcon(getClass().getClassLoader().getResource("qq_icon.png"))); return node.getNodeView(); } return this; } }
http://www.iteye.com/topic/1138211ide