package example;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
public class Main {
private JPanel right;
private JTree tree;
public Main() {
initLeft();
initRight();
}
private void initLeft() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode("我的电脑");
DefaultMutableTreeNode c = new DefaultMutableTreeNode("C:");
DefaultMutableTreeNode d = new DefaultMutableTreeNode("D:");
DefaultMutableTreeNode e = new DefaultMutableTreeNode("E:");
DefaultMutableTreeNode c1 = new DefaultMutableTreeNode("file1");
DefaultMutableTreeNode c2 = new DefaultMutableTreeNode("file2");
DefaultMutableTreeNode c3 = new DefaultMutableTreeNode("file3");
DefaultMutableTreeNode c4 = new DefaultMutableTreeNode("file4");
DefaultTreeModel treeModel = new DefaultTreeModel(root);
treeModel.insertNodeInto(c, root, root.getChildCount());
treeModel.insertNodeInto(d, root, root.getChildCount());
treeModel.insertNodeInto(e, root, root.getChildCount());
treeModel.insertNodeInto(c1, c, c.getChildCount());
treeModel.insertNodeInto(c2, c, c.getChildCount());
treeModel.insertNodeInto(c3, c, c.getChildCount());
treeModel.insertNodeInto(c4, c, c.getChildCount());
tree = new JTree();
tree.setModel(treeModel);
}
private void initRight() {
final Object[] columnNames = { "档案号", "姓名", "年龄", "性别", "婚姻状况", "职业", "联系电话" };
Object[][] rowData = { { "010110", "张三", "28", "男", "已婚", "教师", "13686562936" },
{ "010110", "李四", "28", "男", "已婚", "教师", "13686562936" } };
final JTable tb = new JTable(rowData, columnNames) {
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column) {
return false;
}
};
tb.setPreferredScrollableViewportSize(new Dimension(639, 232));
tb.setRowHeight(20);
tb.setRowSelectionAllowed(true);
tb.setSelectionBackground(Color.lightGray);
tb.setSelectionForeground(Color.white);
tb.setGridColor(Color.black);
tb.setShowGrid(true);
tb.setShowHorizontalLines(true);
tb.setShowVerticalLines(true);
tb.setBackground(Color.white);
// 添加部分2
tb.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {// 双击鼠标
if (e.getClickCount() == 2) {
int colummCount = tb.getModel().getColumnCount();
// 列数
for (int i = 0; i < colummCount; i++) {
System.out.print(tb.getModel().getColumnName(i) + ":");
System.out.print(tb.getModel().getValueAt(tb.getSelectedRow(), i).toString() + " ");
}
System.out.println();
}
}
}
});
JScrollPane pane = new JScrollPane(tb);
right = new JPanel(new BorderLayout());
right.add(pane, BorderLayout.CENTER);
right.add(new JButton("OK"), BorderLayout.SOUTH);
}
public static void main(String[] args)
{
Main m = new Main();
JSplitPane splitPane = new JSplitPane();// 创建一个分割容器类
splitPane.setOneTouchExpandable(true);// 让分割线显示出箭头
splitPane.setContinuousLayout(true);// 操作箭头,重绘图形
splitPane.setPreferredSize(new Dimension(800, 300));// 设置splitPane的尺寸
splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);// 设置分割线方向
splitPane.setLeftComponent(m.tree);
splitPane.setRightComponent(m.right);
splitPane.setDividerSize(10);// 分割线的宽度
splitPane.setDividerLocation(100);// 设置分割线的位置,即与左边框的距离
JFrame frame = new JFrame("JSplitPane demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(splitPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}