当前位置: 技术问答>java相关
200分求一个打印的例子我老也做不对(再线等待)
来源: 互联网 发布时间:2015-10-11
本文导语: 200分求一个打印的例子我老也做不对(再线等待) | import javax.swing.*; import javax.swing.border.Border; import javax.swing.border.EmptyBorder; import javax.swing.tree.TreeModel; import javax.swing.tree.DefaultTreeModel; import...
200分求一个打印的例子我老也做不对(再线等待)
|
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.TreeModel;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
public class TreeImage extends JFrame {
DefaultTreeModel model;
JTree tree;
public TreeImage() {
super("");
setSize(300, 200);
model = createModel();
tree = new JTree(model);
tree.setCellRenderer(new MyTree());
JScrollPane jsp = new JScrollPane(tree);
getContentPane().add(jsp, BorderLayout.CENTER);
setVisible(true);
}
public DefaultTreeModel createModel() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("a");
DefaultMutableTreeNode b = new DefaultMutableTreeNode("b");
DefaultMutableTreeNode c = new DefaultMutableTreeNode("c");
root.add(b);
root.add(c);
return new DefaultTreeModel(root);
}
public static void main(String args[]) {
TreeImage ti = new TreeImage();
}
}
class MyTree extends DefaultTreeCellRenderer {
Border border = new EmptyBorder(1, 1, 1, 1);
public MyTree() {
// setOpaque(true);
// setBorder(border);
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel,
boolean expanded,
boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
setText(value.toString());
if (sel)
setForeground(getTextSelectionColor());
else
setForeground(getTextNonSelectionColor());
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
String str = value.toString();
if (str == "a") {
this.setIcon(new ImageIcon("Add.gif"));
}
if (str == "b") {
this.setIcon(new ImageIcon("Copy.gif"));
}
if (str == "c") {
this.setIcon(new ImageIcon("Delete.gif"));
}
return this;
}
}
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.tree.TreeModel;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
public class TreeImage extends JFrame {
DefaultTreeModel model;
JTree tree;
public TreeImage() {
super("");
setSize(300, 200);
model = createModel();
tree = new JTree(model);
tree.setCellRenderer(new MyTree());
JScrollPane jsp = new JScrollPane(tree);
getContentPane().add(jsp, BorderLayout.CENTER);
setVisible(true);
}
public DefaultTreeModel createModel() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("a");
DefaultMutableTreeNode b = new DefaultMutableTreeNode("b");
DefaultMutableTreeNode c = new DefaultMutableTreeNode("c");
root.add(b);
root.add(c);
return new DefaultTreeModel(root);
}
public static void main(String args[]) {
TreeImage ti = new TreeImage();
}
}
class MyTree extends DefaultTreeCellRenderer {
Border border = new EmptyBorder(1, 1, 1, 1);
public MyTree() {
// setOpaque(true);
// setBorder(border);
}
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel,
boolean expanded,
boolean leaf, int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
setText(value.toString());
if (sel)
setForeground(getTextSelectionColor());
else
setForeground(getTextNonSelectionColor());
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
String str = value.toString();
if (str == "a") {
this.setIcon(new ImageIcon("Add.gif"));
}
if (str == "b") {
this.setIcon(new ImageIcon("Copy.gif"));
}
if (str == "c") {
this.setIcon(new ImageIcon("Delete.gif"));
}
return this;
}
}