初学java,如何把JTree组件添加到界面中?
3个回答
展开全部
/**
* 把代码复制到文件,可以运行。
*/
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);
}
}
}
2016-05-22
展开全部
public PicsTree() {
// ...
final JTree tree = new JTree(top);
// 添加 JTree
this.add(tree);
}
将 JTree 添加至 PicsTree
追问
按照你的改了,并且GUI上加了getContentPane就可以了,谢谢啦
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-05-26 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
public PicsTree() {
// ...
final JTree tree = new JTree(top);
// 添加 JTree
this.add(tree);
}
将 JTree 添加至 PicsTree
// ...
final JTree tree = new JTree(top);
// 添加 JTree
this.add(tree);
}
将 JTree 添加至 PicsTree
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询