实验8 javaFX程序设计.doc
山西大学计算机与信息技术学院实验报告姓 名苏文杰学 号201601001026专业班级计算机科学与技术三班 课程名称 Java实验实验日期2017/12/4 成 绩指导教师杨陟卓 批改日期实验名称实验 8 JAVAFX程序设计一、实验目的掌握常用GUI控制组件及其事件处理。 二、实验内容1编程包含一个标签和一个按钮,单击按钮时,标签的内容在“你好”和“再见”之间切换。分别用本类(该类自身),外部类,内部类,匿名内部类,以及lambda表达式完成。程序代码:package sd8;import java.awt.GridLayout;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;public class Test extends JFrame implements MouseListener private JLabel label = new JLabel("你好");private JButton btn = new JButton("OK");public Test()setLayout(new GridLayout(2, 1);setBounds(200, 200, 250, 250);add(label);add(btn);btn.addMouseListener(this);super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);pack();public static void main(String args) new Test();new Thread() -> new Test().start();public void mouseClicked(MouseEvent e) if(label.getText().equals("你好")label.setText("再见");elselabel.setText("你好");public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) 运行结果贴图:2编程包含一个文本框和一个文本区域,文本框内容改变时,将文本框中的内容显示在文本区域中;在文本框中按回车键时,清空文本区域的内容。程序代码:package sd2;import java.awt.Color; import java.awt.GridLayout; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.border.TitledBorder;public class ShowText extends JFrame private static final long serialVersionUID = 1L; private JTextField text1; private JTextArea text2; public ShowText() super("Tetx Show"); JPanel p1 = new JPanel(); p1.setBackground(Color.WHITE); p1.setBorder(new TitledBorder("文本框"); text1 = new JTextField(10); text1.addKeyListener(new TextListener(); p1.add(text1); JPanel p2 = new JPanel(); p2.setBackground(Color.WHITE); p2.setBorder(new TitledBorder("文本区域"); text2 = new JTextArea("原文本", 10, 10); text2.setLineWrap(true); text2.setEditable(false); p2.add(text2); setLayout(new GridLayout(2, 1, 0, 5); add(p1); add(p2); setSize(200, 200); setVisible(true); this.setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); private class TextListener implements KeyListener public void keyPressed(KeyEvent e) public void keyReleased(KeyEvent e) if (e.getKeyChar() != KeyEvent.VK_ENTER) text2.setText(text1.getText(); public void keyTyped(KeyEvent e) if (e.getKeyChar() = KeyEvent.VK_ENTER) text2.setText(null); public static void main(String args) JFrame frame = new ShowText(); 运行结果贴图:3编程包含一个复选按钮和一个普通按钮,复选按钮选中时,普通按钮的背景色为青色,未选中时为灰色。程序代码:package sd3;import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; public class ChangeButtonColor extends JFrame private static final long serialVersionUID = 1L; private JButton button; private JCheckBox checkBox; public ChangeButtonColor() super("改变按钮颜色"); JPanel p1 = new JPanel(); p1.setBackground(Color.WHITE); setLayout(new GridLayout(2, 1); button = new JButton("Hello"); button.setSize(20, 20); button.setBackground(Color.GRAY); p1.add(button); JPanel p2 = new JPanel(); p2.setBackground(Color.BLUE); checkBox = new JCheckBox(); checkBox.addItemListener(new checkBoxListener(); p2.add(checkBox); add(p1); add(p2); setSize(200, 200); setVisible(true); this.setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); private class checkBoxListener implements ItemListener public void itemStateChanged(ItemEvent e) if (checkBox.isSelected() button.setBackground(Color.CYAN); else button.setBackground(Color.GRAY); public static void main(String args) ChangeButtonColor b = new ChangeButtonColor(); 运行结果贴图:4编程显示当前鼠标的位置坐标。程序代码:package sd4;import java.awt.Color; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.JButton; import javax.swing.JFrame; public class LocateMouse extends JFrame private JButton location; public LocateMouse() super("寻找鼠标位置"); location = new JButton("显示鼠标位置"); location.setSize(20, 10); add(location); location.addMouseMotionListener(new MouseMotionListener() public void mouseDragged(MouseEvent e) public void mouseMoved(MouseEvent e) location.setText("鼠标在(" + e.getX() + "," + e.getY() + ")"); ); setSize(300, 200); setLocationRelativeTo(null); setVisible(true); location.setBackground(Color.WHITE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public static void main(String args) LocateMouse mouse = new LocateMouse(); 运行结果贴图:5. 编写程序,实现使用键盘上的上下左右箭头控制界面上图片的移动。移动到边界时从界面另一侧出现。移动过程中显示另一个图片,停止时恢复原来的图片。程序代码:package sd5;import java.awt.Color; import java.awt.event.KeyEvent;import java.awt.event.KeyListener; import javax.swing.ImageIcon;import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MoveImage extends JFrame private ImageIcon oneIcon = new ImageIcon("C:Users潘Desktoptimg.jpg"); private ImageIcon twoIcon = new ImageIcon("C:Users潘PicturesCamera Roll1.jpg"); private JLabel label; JPanel p; public MoveImage() super("Image移动"); setSize(500, 500); setLocationRelativeTo(null); label = new JLabel(oneIcon); p = new JPanel(); setContentPane(p); p.setLayout(null); this.addKeyListener(new PanelListener(); label.setBounds(0, 0, 100, 100); p.add(label); p.setBackground(Color.WHITE); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); private class PanelListener implements KeyListener public void keyPressed(KeyEvent e) label.setIcon(twoIcon); int x = label.getX(); int y = label.getY(); int w = p.getWidth(); int h = p.getHeight(); if (e.getKeyCode() = KeyEvent.VK_UP) -y; if (y <= 0) y = h; label.setBounds(x, y, 100, 100); else if (e.getKeyCode() = KeyEvent.VK_DOWN) +y; if (y >= h) y = 0; label.setBounds(x, y, 100, 100); else if (e.getKeyCode() = KeyEvent.VK_LEFT) x-; if (x <= 0) x = w; label.setBounds(x, y, 100, 100); else if (e.getKeyCode() = KeyEvent.VK_RIGHT) x+; if (x >= w) x = 0; label.setBounds(x, y, 100, 100); public void keyReleased(KeyEvent e) if (e.getKeyCode() = KeyEvent.VK_UP | e.getKeyCode() = KeyEvent.VK_DOWN | e.getKeyCode() = KeyEvent.VK_LEFT | e.getKeyCode() = KeyEvent.VK_RIGHT) label.setIcon(oneIcon); public void keyTyped(KeyEvent e) public static void main(String args) MoveImage m = new MoveImage(); 运行结果贴图:移动前移动中移动后移动末尾6 绘制如下形式的一个二叉树。程序代码:package tree; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.List; import javax.swing.JPanel; SuppressWarnings("serial")public class TreePanel extends JPanel private Node tree; /保存整棵树 private int nodeWidth = 20; /每个结点的宽度 private int nodeHeight = 20; /每个结点的高度 private int vDistance = 50; /每2个结点的垂直距离 private int hDIstance = 30; /每2个结点的水平距离 private int startY = 200; /根结点的Y,默认距离顶部200像素 private int startX = 0; /根结点的X,默认水平居中对齐 private int childAlignWays; /子对齐方式 public static int CHILD_ALIGN_ABSOLUTE = 0; /相对Panel居中 public static int CHILD_ALIGN_RELATIVE = 1; /相对父结点居中 private Font font = new Font("微软雅黑",Font.BOLD,20); /描述结点的字体 private Color nodeColor = Color.WHITE; /结点背景颜色 private Color linkLineColor = Color.BLACK; /结点连线颜色 private Color stringColor = Color.BLACK; /结点描述文字的颜色 / 默认构造 public TreePanel() this(null,CHILD_ALIGN_ABSOLUTE); /根据传入的Node绘制树,以绝对居中的方式绘制 / param n 要绘制的树 public TreePanel(Node n) this(n,CHILD_ALIGN_ABSOLUTE); /* 1 设置要绘制时候的对齐策略 2 param childAlignWays 对齐策略 3 see tree.TreePanel#CHILD_ALIGN_RELATIVE 4 see tree.TreePanel#CHILD_ALIGN_ABSOLUTE */ public TreePanel(int childAlignWays) this(null,childAlignWays); /* 1根据子对齐策略childAlignWays绘制的树的根结点n 2 param n 要绘制的树的根结点 3 param childAlignWays 对齐策略 */ public TreePanel(Node n, int childAlignWays) super(); /setTree(n); this.childAlignWays = childAlignWays; /* 1 设置用于绘制的树 2 param n 用于绘制的树的 */ public void setTree(Node n) tree = n; /重写,调用自己的绘制方法 public void paintComponent(Graphics g) startX = (getWidth()-nodeWidth)/2; super.paintComponent(g); g.setFont(font); drawAllNode(tree, startX, g); /* 1 递归绘制整棵树 2 param n 被绘制的Node 3 param xPos 根节点的绘制X位置 4 param g 绘图上下文环境 */ public String toString() return "a" public void drawAllNode(Node n, int x, Graphics g) int y = n.getLayer()*(vDistance+nodeHeight)+startY; int fontY = y + nodeHeight - 4; g.setColor(nodeColor); g.fillRect(x, y, nodeWidth, nodeHeight); /画结点的格子 g.setColor(stringColor); g.drawString(n.getName(), x, fontY); /画结点的名字 if(n.hasChild() List<Node> c = n.getChilds(); int size = n.getChilds().size(); int tempPosx = childAlignWays = CHILD_ALIGN_RELATIVE ? x+nodeWidth/2 - (size*(nodeWidth+hDIstance)-hDIstance)/2 : (getWidth() - size*(nodeWidth+hDIstance)+hDIstance)/2; int i = 0; for(Node node : c) int newX = tempPosx+(nodeWidth+hDIstance)*i; /孩子结点起始X g.setColor(linkLineColor); g.drawLine(x+nodeWidth/2, y+nodeHeight, newX+nodeWidth/2, y+nodeHeight+vDistance); /画连接结点的线 drawAllNode(node, newX, g); i+; public Color getnodeColor() return nodeColor; /* 1 设置结点背景颜色 2 param nodeColor 结点背景颜色 */ public void setnodeColor(Color nodeColor) this.nodeColor = nodeColor; public Color getLinkLineColor() return linkLineColor; /* * 设置结点连接线的颜色 * param nodeLinkLine 结点连接线的颜色 */ public void setLinkLineColor(Color nodeLinkLine) this.linkLineColor = nodeLinkLine; public Color getStringColor() return stringColor; /* * 设置结点描述的颜色 * param stringColor 结点描述的颜色 */ public void setStringColor(Color stringColor) this.stringColor = stringColor; public int getStartY() return startY; /* * 设置根结点的Y位置 * param startY 根结点的Y位置 */ public void setStartY(int startY) this.startY = startY; public int getStartX() return startX; /* * 设置根结点的X位置 * param startX 根结点的X位置 */ public void setStartX(int startX) this.startX = startX; package tree.demo; import java.awt.BorderLayout; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JPanel; import tree.Node; import tree.TreePanel; public class TestDrawTree extends JFrame public TestDrawTree() super("MYTest Draw Tree"); initComponents(); public void initComponents() /初始化树的数据 Node floor0 = new Node("A"); Node floor11 = new Node("B"); Node floor12 = new Node("C"); floor0.add(floor11); floor0.add(floor12); Node floor21 = new Node("D"); Node floor22 = new Node("E"); floor11.add(floor21); floor11.add(floor22); Node floor31 = new Node("F"); Node floor32 = new Node("G"); floor21.add(floor31); floor21.add(floor32); Node floor41 = new Node("H"); Node floor42 = new Node("I"); floor32.add(floor41); floor32.add(floor42); floor0.printAllNode(floor0); /输出树 / 创建一个用于绘制树的面板并将树传入,使用相对对齐方式 TreePanel panel1 = new TreePanel(TreePanel.CHILD_ALIGN_RELATIVE); panel1.setTree(floor0); JPanel contentPane = new JPanel(); contentPane.setLayout(new GridLayout(1,1); contentPane.add(panel1); add(contentPane,BorderLayout.CENTER); public static void main(String args) TestDrawTree frame = new TestDrawTree(); frame.setSize(800, 800); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 运行结果贴图: