JAVA怎么设置树状节点,使之在点击各个节点时,在标签上显示相应图片呢? 30
2016-06-04 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
点击节点的时候有事件触发的,可以得到节点本身信息。
接下来是使用Ajax
接下来是使用Ajax
追问
就是我要做一个小程序,用JFrame或者Japplet。用netbeans的设计视图做就可以。然后左边是树状列表,右边是标签。要实现点击左边目录里的项目,右边标签上出现相应的图片。然后我现在不知道怎么实现,是要在树状列表上对每一个节点进行鼠标点击事件的监听吗?但是要怎么写代码呢?你能明白我要问的意思吗?
展开全部
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
/**
*
* @author beans
*/
public class TreeMain {
public static void main(String[] args) {
new TreeMain().showDialog();
}
public TreeMain() {
}
/**
* 显示窗口
*/
private void showDialog() {
JDialog dialog = new JDialog();
dialog.setBounds(new Rectangle(50, 50, 380, 280));
dialog.setTitle("演示树");
dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
dialog.add(this.getPanel(), BorderLayout.CENTER);
dialog.setVisible(true);
}
private JPanel getPanel() {
JPanel panel = new JPanel();
JScrollPane treePanel = new JScrollPane();
treePanel.setViewportView(this.getTree());
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(panel);
panel.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(treePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(treePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(18, Short.MAX_VALUE))
);
panel.add(treePanel, BorderLayout.CENTER);
return panel;
}
/**
* 取得树。
* @return
*/
private JTree getTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
root.add(this.getNode());
root.add(this.getNode());
root.add(this.getNode());
JTree jtree = new JTree(root);
jtree.setRootVisible(false);
jtree.setCellRenderer(new CTreeCellRenderer());
jtree.expandRow(1);
return jtree;
}
/**
* 取得树节点。
* @return
*/
private DefaultMutableTreeNode getNode() {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(new NodeObject(true, "节"));
for(int i = 0; i < 5; i++) {
DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(new NodeObject(false, "叶" + i));
node.add(leaf);
}
return node;
}
/**
* 树节点和树叶,关联对象。
*/
class NodeObject {
boolean isNode;
String name;
/**
*
* @param isNode the value of isNode
* @param name the value of name
*/
NodeObject(boolean isNode, String name) {
this.isNode = isNode;
this.name = name;
}
/**
* 图标
* @param isSelect 选中节点时返回不同的图标。
* @return
*/
ImageIcon getIcon(boolean isSelect) {
int wh = 20;
BufferedImage image = new BufferedImage(wh, wh, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
image = g2.getDeviceConfiguration().createCompatibleImage(wh, wh, Transparency.TRANSLUCENT);
Graphics2D g2d = image.createGraphics();
Font font = new Font("Dialog", Font.PLAIN, wh - 4);
g2d.setFont(font);
g2d.setBackground(Color.WHITE);
g2d.setColor(Color.BLACK);
g2d.drawString(isSelect ? " S " : " N ", 0, wh - 1);
g2d.setColor(this.isNode ? Color.RED : Color.YELLOW);
g2d.drawLine(0, 5, wh, 5);
g2d.drawLine(0, 10, wh, 10);
g2d.drawLine(0, 15, wh, 15);
g2d.dispose();
g2.dispose();
return new ImageIcon(image);
}
String getName() {
return this.name;
}
}
/**
* 树渲染器
*/
protected class CTreeCellRenderer extends JLabel implements TreeCellRenderer {
protected Color m_textSelectionColor;
protected Color m_textNonSelectionColor;
protected Color m_bkSelectionColor;
protected Color m_bkNonSelectionColor;
protected Color m_borderSelectionColor;
protected boolean m_selected;
public CTreeCellRenderer() {
m_textSelectionColor = UIManager.getColor("Tree.selectionForeground");
m_textNonSelectionColor = UIManager.getColor("Tree.textForeground");
m_bkSelectionColor = UIManager.getColor("Tree.selectionBackground");
m_bkNonSelectionColor = UIManager.getColor("Tree.textBackground");
m_borderSelectionColor = UIManager.getColor("Tree.selectionBorderColor");
}
@Override
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
NodeObject obj = (NodeObject) node.getUserObject();
this.setIcon(obj.getIcon(selected));
this.setText(" " + obj.getName() + " ");
this.setForeground(selected ? m_textSelectionColor : m_textNonSelectionColor);
this.setBackground(selected ? m_bkSelectionColor : m_bkNonSelectionColor);
this.m_selected = selected;
return this;
}
@Override
public void paint(Graphics g) {
Color bColor = this.getBackground();
Icon icon = this.getIcon();
g.setColor(bColor);
int offset = 0;
if (icon != null && getText() != null) {
offset = (icon.getIconWidth() + this.getIconTextGap());
}
g.fillRect(offset, 0, this.getWidth() - 1 - offset, this.getHeight() - 1);
if (this.m_selected) {
g.setColor(this.m_borderSelectionColor);
g.drawRect(offset, 0, this.getWidth() - 1 - offset, this.getHeight() - 1);
}
super.paint(g);
}
}
}
追问
你知道怎么用设计视图做吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
嗯
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询