📄 jtreeframe.java
字号:
package cn.dxm.frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.XMLDecoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import searchjet.annotation.GetImageFeature;
import cn.dxm.XMLAnaysis.Dom.DomParserForPathSet;
import cn.dxm.dbconn.DBConnect;
import cn.dxm.util.XMLEditor;
public class JTreeFrame extends JFrame implements ActionListener,
TreeSelectionListener {
private static final long serialVersionUID = 3604526229381602695L;
private JButton btnAdd = new JButton("添加节点");
private JButton btnRemove = new JButton("删除节点");
private JButton btnInsert = new JButton("插入数据");
private JLabel label = new JLabel("输入节点名:");
private JButton btnSaveJTreetoXml = new JButton("保存JTree");
private JTextField txtAdd = new JTextField(20);
private static DefaultMutableTreeNode root = null;
private DefaultTreeModel treeModel = null;
private JTree tree = null;
private DefaultMutableTreeNode selectedNode = null;
private static JTextArea messageLabel = null;
private String imageMessage = " 图像特征值 ";
private XMLEditor xmlEditor = new XMLEditor();
private static JPanel textContainer = new JPanel();
private static JPanel xmlContainer = new JPanel();
private static String xmlfname = null;
private String moment = null;
private String barycenterx = null;
private String barycentery = null;
private String barycentervalue = null;
private String energy = null;
private String entropy = null;
private String inertia = null;
private String placidity = null;
private String colorhistogram = null;
private String name = null;
private String url = null;
private String size = null;
public JTreeFrame() {
super("JTree演示");
this.setLayout(new BorderLayout());
JPanel bottompanel = new JPanel();
bottompanel.add(btnAdd);
bottompanel.add(btnRemove);
bottompanel.add(label);
bottompanel.add(txtAdd);
bottompanel.add(btnInsert);
bottompanel.add(btnSaveJTreetoXml);
DomParserForPathSet dfs = new DomParserForPathSet();
Map map = dfs.ParserXML("src/ParameterSet.xml");
xmlfname = (String) map.get("TreeXmlPath");
// File xmlfile = new File(xmlfname);
//
// if (xmlfile != null) {
// xmlEditor.loadXmlToJTree(tree, treeModel, xmlContainer, xmlfname,
// root);
// }
try {
// XML解码器.
XMLDecoder xe = new XMLDecoder(new FileInputStream(xmlfname));
root = (DefaultMutableTreeNode) xe.readObject();
xe.close();
} catch (Exception ex) {
ex.printStackTrace();
}
// root = new DefaultMutableTreeNode("root");
treeModel = new DefaultTreeModel(root);
tree = new JTree(treeModel);
tree.addTreeSelectionListener(this);
tree.setBackground(Color.gray);
tree.setShowsRootHandles(true);
btnAdd.addActionListener(this);
btnRemove.addActionListener(this);
btnInsert.addActionListener(this);
btnSaveJTreetoXml.addActionListener(this);
File file = new File("src//c.jpg");
try {
FileInputStream fi = new FileInputStream(file);
size = fi.getChannel().size() + "B";
} catch (IOException e) {
e.printStackTrace();
}
this.url = file.getAbsolutePath();
String imageURL = "src/c.jpg";
GetImageFeature gif = new GetImageFeature();
Map imageinfo = new HashMap();
imageinfo = gif.getImageFeature(imageURL);
this.moment = (String) imageinfo.get("moment");
this.barycenterx = (String) imageinfo.get("barycenterx");
this.barycentery = (String) imageinfo.get("barycentery");
this.barycentervalue = (String) imageinfo.get("barycentervalue");
this.energy = (String) imageinfo.get("energy");
this.entropy = (String) imageinfo.get("entropy");
this.inertia = (String) imageinfo.get("inertia");
this.placidity = (String) imageinfo.get("placidity");
this.colorhistogram = (String) imageinfo.get("colorhistogram");
// System.out.println(this.colorhistogram);
String turn = "\n";
imageMessage += turn + "moment:" + (String) imageinfo.get("moment")
+ turn;
imageMessage += "barycenterx:" + (String) imageinfo.get("barycenterx")
+ turn;
imageMessage += "barycentery:" + (String) imageinfo.get("barycentery")
+ turn;
imageMessage += "barycentervalue:"
+ (String) imageinfo.get("barycentervalue") + turn;
imageMessage += "energy:" + (String) imageinfo.get("energy") + turn;
imageMessage += "entropy:" + (String) imageinfo.get("entropy") + turn;
imageMessage += "inertia:" + (String) imageinfo.get("inertia") + turn;
imageMessage += "placidity:" + (String) imageinfo.get("placidity")
+ turn;
imageMessage += "colorhistogram:"
+ (String) imageinfo.get("colorhistogram") + turn;
messageLabel = new JTextArea(imageMessage);
messageLabel.setPreferredSize(new Dimension(150, 500));
messageLabel.setEditable(false);
textContainer.add(messageLabel);
xmlContainer.setLayout(new GridLayout(1, 0));
xmlContainer.add(new JScrollPane(tree));
this.add(new JScrollPane(textContainer), BorderLayout.WEST);
this.add(xmlContainer, BorderLayout.CENTER);
this.add(bottompanel, BorderLayout.PAGE_END);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation((d.width - this.getWidth()) / 4, 30);
this.setSize(d.width / 2 + 50, d.height - 150);
this.setVisible(true);
this.setBackground(Color.black);
this.setResizable(false);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
xmlEditor.saveJTreeToXml(xmlfname, root);
//JTreeFrame.this.setDefaultCloseOperation(JTreeFrame.EXIT_ON_CLOSE);
JTreeFrame.this.setVisible(false);
}
});
}
// @Override
public void valueChanged(TreeSelectionEvent e) {
if (null!=(DefaultMutableTreeNode) e.getNewLeadSelectionPath()
.getLastPathComponent() ) {
selectedNode = (DefaultMutableTreeNode) e.getNewLeadSelectionPath()
.getLastPathComponent(); // 标记选中的节点
} else {
selectedNode = root;
System.out.println(selectedNode.getUserObject().toString());
}
}
// @Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnAdd && !txtAdd.getText().equals("")
&& selectedNode != null) {
// 添加节点
DefaultMutableTreeNode addNode = new DefaultMutableTreeNode(txtAdd
.getText());
selectedNode.add(addNode);
tree.updateUI();
} else if (e.getSource() == btnRemove && selectedNode != null
&& selectedNode != root) {
selectedNode.removeFromParent(); // 移除节点
selectedNode = null;
tree.updateUI();
} else if (e.getSource() == btnInsert) {
if (selectedNode == null) {
javax.swing.JOptionPane.showMessageDialog(null, "您还没有选择节点",
"提醒", javax.swing.JOptionPane.INFORMATION_MESSAGE);
return;
} else {
if (this.url == null) {
javax.swing.JOptionPane.showMessageDialog(null,
"您还没有采样,采样信息不存在", "提醒",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
} else {
int configType = javax.swing.JOptionPane.showConfirmDialog(
ImageFrame.frame, "是不要将右边的信息插入数据库", "提示",
JOptionPane.YES_NO_CANCEL_OPTION);
if (configType == JOptionPane.CANCEL_OPTION) {
return;
} else {
DBConnect dbconn = new DBConnect();
String treeName = selectedNode.getUserObject()
.toString();
String imageURL = "src/c.jpg";
GetImageFeature gif = new GetImageFeature();
Map imageinfo = new HashMap();
imageinfo = gif.getImageFeature(imageURL);
this.moment = (String) imageinfo.get("moment");
this.barycenterx = (String) imageinfo.get("barycenterx");
this.barycentery = (String) imageinfo.get("barycentery");
this.barycentervalue = (String) imageinfo.get("barycentervalue");
this.energy = (String) imageinfo.get("energy");
this.entropy = (String) imageinfo.get("entropy");
this.inertia = (String) imageinfo.get("inertia");
this.placidity = (String) imageinfo.get("placidity");
this.colorhistogram = (String) imageinfo.get("colorhistogram");
System.out.println(colorhistogram);
String sql = "insert into img(moment,barycenterx,barycentery,barycentervalue,energy,entropy,inertia,placidity,colorhistogram,name) values('"
+ this.moment
+ "','"
+ this.barycenterx
+ "','"
+ this.barycentery
+ "','"
+ this.barycentervalue
+ "','"
+ this.energy
+ "','"
+ this.entropy
+ "','"
+ this.inertia
+ "','"
+ this.placidity
+ "','"
+ this.colorhistogram + "','" + treeName + "')";
boolean b = dbconn.executeUpdate(sql);
if (b) {
javax.swing.JOptionPane
.showMessageDialog(
null,
"图片信息保存成功",
"提醒",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
// 将图片信息清空.
this.url = null;
this.name = null;
this.size = null;
} else {
javax.swing.JOptionPane.showMessageDialog(null,
"图片信息失败成功", "提醒",
javax.swing.JOptionPane.ERROR_MESSAGE);
}
}
}
}
} else if (e.getSource() == btnSaveJTreetoXml) {
xmlEditor.saveJTreeToXml(xmlfname, root);
javax.swing.JOptionPane.showMessageDialog(null, "JTree保存成功", "提醒",
javax.swing.JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String args[]) {
JTreeFrame f = new JTreeFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -