package birthdaytree
import java.awt.BorderLayout
import java.awt.Font
import java.awt.event.MouseAdapter
import java.awt.event.MouseEvent
import javax.swing.JFrame
import javax.swing.JScrollPane
import javax.swing.JTextArea
import javax.swing.JTree
import javax.swing.tree.DefaultMutableTreeNode
import javax.swing.tree.TreeNode
import javax.swing.tree.TreePath
public class BirthdayTree extends JFrame {
private static final long serialVersionUID = -1908556835892773567L
private JTextArea jt
public JTextArea getJt() {
return jt
}
public BirthdayTree() {
JScrollPane jtPane = new JScrollPane(jt = new JTextArea())
jt.setFont(new Font("Serif", Font.PLAIN, 16))
jt.setLineWrap(true)
jt.setWrapStyleWord(true)
DefaultMutableTreeNode root = new DefaultMutableTreeNode("365天生日性格大全!")
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("蔬菜")
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("水果")
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("礼品")
DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("家用小物件")
root.add(node1)
root.add(node2)
root.add(node3)
root.add(node4)
DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("白菜")
node1.add(leafnode)
leafnode = new DefaultMutableTreeNode("大蒜")
node1.add(leafnode)
leafnode = new DefaultMutableTreeNode("土豆")
node1.add(leafnode)
leafnode = new DefaultMutableTreeNode("苹果")
node2.add(leafnode)
leafnode = new DefaultMutableTreeNode("香蕉")
node2.add(leafnode)
leafnode = new DefaultMutableTreeNode("西瓜")
node1.add(leafnode)
leafnode = new DefaultMutableTreeNode("礼品")
node3.add(leafnode)
leafnode = new DefaultMutableTreeNode("茅台酒")
node3.add(leafnode)
leafnode = new DefaultMutableTreeNode("营养麦片")
node3.add(leafnode)
leafnode = new DefaultMutableTreeNode("保健食品")
node3.add(leafnode)
leafnode = new DefaultMutableTreeNode("味精")
node4.add(leafnode)
leafnode = new DefaultMutableTreeNode("酱油")
node4.add(leafnode)
leafnode = new DefaultMutableTreeNode("洗洁精")
node4.add(leafnode)
leafnode = new DefaultMutableTreeNode("保鲜袋")
node4.add(leafnode)
JTree tree = new JTree(root)
tree.setEditable(false)// 设置JTree为不可编辑
tree.addMouseListener(new MouseHandle(this))// 在JTree中加入检测Mouse事件,以便取得节点名称
JScrollPane js = new JScrollPane()
js.setViewportView(tree)// 将树组件添加到滚动条组件中
setLayout(new BorderLayout())
getContentPane().add(jtPane, BorderLayout.CENTER)
getContentPane().add(js, BorderLayout.WEST)
this.setSize(900, 500)
this.setVisible(true)
}
public static void main(String[] args) {
new BirthdayTree()
}
}
class MouseHandle extends MouseAdapter {
private BirthdayTree birthdayTree
// 处理Mouse点选事件
@Override
public void mouseClicked(MouseEvent e) {
String nodeName
JTree tree = (JTree) e.getSource()// 当单击鼠标,在右边会显示出相应的数据信息
int rowLocation = tree.getRowForLocation(e.getX(), e.getY())
TreePath treepath = tree.getPathForRow(rowLocation)
if (null == treepath) {
return
}
TreeNode treenode = (TreeNode) treepath.getLastPathComponent()
nodeName = treenode.toString()
getBirthdayTree().getJt().setText(nodeName)
}
public BirthdayTree getBirthdayTree() {
return birthdayTree
}
public MouseHandle(BirthdayTree birthdayTree) {
this.birthdayTree = birthdayTree
}
}
你ArrayList<String>subj = new ArrayList<String>()的话不需要转换类型的
推荐你用HashMap<String, String>储存
把节点-详细内容映射到HashMap里面,这样比用index判断好看点,也不用验证subj和cont是一一对应的
0. JavaSwing 简介Java Swing 图形界面开发简介
1. JavaSwing 布局管理器
JavaSwing_1.1: FlowLayout(流式布局)
JavaSwing_1.2: GridLayout(网格布局)
JavaSwing_1.3: GridBagLayout(网格袋布局)
JavaSwing_1.4: BoxLayout(箱式布局)
JavaSwing_1.5: GroupLayout(分组布局)
JavaSwing_1.6: CardLayout(卡片布局)
JavaSwing_1.7: BorderLayout(边界布局)
JavaSwing_1.8: SpringLayout(d性布局)
JavaSwing_1.9: null(绝对布局)
2. JavaSwing 基本组件
JavaSwing_2.1: JLabel(标签)
JavaSwing_2.2: JButton(按钮)
JavaSwing_2.3: JRadioButton(单选按钮)
JavaSwing_2.4: JCheckBox(复选框)
JavaSwing_2.5: JToggleButton(开关按钮)
JavaSwing_2.6: JTextField(文本框)
JavaSwing_2.7: JPasswordField(密码框)
JavaSwing_2.8: JTextArea(文本区域)
JavaSwing_2.9: JComboBox(下拉列表框)
JavaSwing_2.10: JList(列表框)
JavaSwing_2.11: JProgressBar(进度条)
JavaSwing_2.12: JSlider(滑块)
3. JavaSwing 面板组件
JavaSwing_3.1: JPanel(面板)
JavaSwing_3.2: JScrollPane(滚动面板)
JavaSwing_3.3: JSplitPane(分隔面板)
JavaSwing_3.4: JTabbedPane(选项卡面板)
JavaSwing_3.5: JLayeredPane(层级面板)
4. JavaSwing 其他组件
JavaSwing_4.1: JFrame(窗口)
JavaSwing_4.2: JDialog、JOptionPane(对话框)
JavaSwing_4.3: JFileChooser(文件选择器)
JavaSwing_4.4: JColorChooser(颜色选择器)
JavaSwing_4.5: JMenuBar(菜单栏)
JavaSwing_4.6: JToolBar(工具栏)
JavaSwing_4.7: JPopupMenu(d出菜单)
JavaSwing_4.8: JTable(表格)
JavaSwing_4.9: JTree(树)
JavaSwing_4.10: JInternalFrame(内部窗口)
5. JavaSwing 相关特性
JavaSwing_5.1: 组件的位置和尺寸
JavaSwing_5.2: 组件边框(Border)
JavaSwing_5.3: 事件处理
JavaSwing_5.4: 多线程并发与线程安全
JavaSwing_5.5: 拖拽功能
JavaSwing_5.6: 系统托盘(System Tray)
JavaSwing_5.7: 闪屏(Splash Screen)
扩展:Java AWT/Swing 其他相关
Java绘图: 使用Graphics类绘制线段、矩形、椭圆/圆弧/扇形、图片、文本
Java图片 *** 作 — 图片的读取、绘制、缩放、裁剪、保存
Java代码截屏:使用 Java 代码截取电脑屏幕并保存
Java模拟鼠标键盘输入事件 — Robot 类
Java *** 作桌面应用 — Desktop 类
Java *** 作系统剪贴板(Clipboard)实现复制和粘贴
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)