JFrame居中
方法一:
setLocationRelativeTo(null);
注意:必须在整个frame初始化完成后再加上此语句,否则将显示在屏幕右下角
方法二:
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);
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.php
http://www.blogjava.net/santicom/archive/2011/03/23/346796.html
http://www.cnblogs.com/happyPawpaw/archive/2013/01/10/2854675.html
http://etwo.iteye.com/blog/1458414
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/1138211