




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、山西大學(xué)計算機與信息技術(shù)學(xué)院實驗報告姓 名蘇文杰學(xué) 號201601001026專業(yè)班級計算機科學(xué)與技術(shù)三班 課程名稱 Java實驗實驗日期2017/12/4 成 績指導(dǎo)教師楊陟卓 批改日期實驗名稱實驗 8 JAVAFX程序設(shè)計一、實驗?zāi)康恼莆粘S肎UI控制組件及其事件處理。 二、實驗內(nèi)容1編程包含一個標(biāo)簽和一個按鈕,單擊按鈕時,標(biāo)簽的內(nèi)容在“你好”和“再見”之間切換。分別用本類(該類自身),外部類,內(nèi)部類,匿名內(nèi)部類,以及l(fā)ambda表達式完成。程序代碼:package sd8;import java.awt.GridLayout;import java.awt.event.MouseEven
2、t;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);setBo
3、unds(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(你好)l
4、abel.setText(再見);elselabel.setText(你好);public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) public void mousePressed(MouseEvent e) public void mouseReleased(MouseEvent e) 運行結(jié)果貼圖:2編程包含一個文本框和一個文本區(qū)域,文本框內(nèi)容改變時,將文本框中的內(nèi)容顯示在文本區(qū)域中;在文本框中按回車鍵時,清空文本區(qū)域的內(nèi)容。程序代碼:package sd2;import java.awt.
5、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
6、 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(te
7、xt1); JPanel p2 = new JPanel(); p2.setBackground(Color.WHITE); p2.setBorder(new TitledBorder(文本區(qū)域); 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.
8、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
9、e) if (e.getKeyChar() = KeyEvent.VK_ENTER) text2.setText(null); public static void main(String args) JFrame frame = new ShowText(); 運行結(jié)果貼圖:3編程包含一個復(fù)選按鈕和一個普通按鈕,復(fù)選按鈕選中時,普通按鈕的背景色為青色,未選中時為灰色。程序代碼:package sd3;import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ItemEvent; import java.a
10、wt.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 ChangeButto
11、nColor() 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();
12、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)
13、 if (checkBox.isSelected() button.setBackground(Color.CYAN); else button.setBackground(Color.GRAY); public static void main(String args) ChangeButtonColor b = new ChangeButtonColor(); 運行結(jié)果貼圖:4編程顯示當(dāng)前鼠標(biāo)的位置坐標(biāo)。程序代碼:package sd4;import java.awt.Color; import java.awt.event.MouseEvent; import java.awt.even
14、t.MouseMotionListener; import javax.swing.JButton; import javax.swing.JFrame; public class LocateMouse extends JFrame private JButton location; public LocateMouse() super(尋找鼠標(biāo)位置); location = new JButton(顯示鼠標(biāo)位置); location.setSize(20, 10); add(location); location.addMouseMotionListener(new MouseMotion
15、Listener() public void mouseDragged(MouseEvent e) public void mouseMoved(MouseEvent e) location.setText(鼠標(biāo)在( + e.getX() + , + e.getY() + ); ); setSize(300, 200); setLocationRelativeTo(null); setVisible(true); location.setBackground(Color.WHITE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); public
16、 static void main(String args) LocateMouse mouse = new LocateMouse(); 運行結(jié)果貼圖:5. 編寫程序,實現(xiàn)使用鍵盤上的上下左右箭頭控制界面上圖片的移動。移動到邊界時從界面另一側(cè)出現(xiàn)。移動過程中顯示另一個圖片,停止時恢復(fù)原來的圖片。程序代碼:package sd5;import java.awt.Color; import java.awt.event.KeyEvent;import java.awt.event.KeyListener; import javax.swing.ImageIcon;import javax.swi
17、ng.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() s
18、uper(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); setDefaultCloseO
19、peration(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 = h) y = 0; label.setB
20、ounds(x, y, 100, 100); else if (e.getKeyCode() = KeyEvent.VK_LEFT) 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_RIG
21、HT) label.setIcon(oneIcon); public void keyTyped(KeyEvent e) public static void main(String args) MoveImage m = new MoveImage(); 運行結(jié)果貼圖:移動前移動中移動后移動末尾6 繪制如下形式的一個二叉樹。程序代碼:package tree; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.List; import javax.swing.JPan
22、el; SuppressWarnings(serial)public class TreePanel extends JPanel private Node tree; /保存整棵樹 private int nodeWidth = 20; /每個結(jié)點的寬度 private int nodeHeight = 20; /每個結(jié)點的高度 private int vDistance = 50; /每2個結(jié)點的垂直距離 private int hDIstance = 30; /每2個結(jié)點的水平距離 private int startY = 200; /根結(jié)點的Y,默認(rèn)距離頂部200像素 private
23、int startX = 0; /根結(jié)點的X,默認(rèn)水平居中對齊 private int childAlignWays; /子對齊方式 public static int CHILD_ALIGN_ABSOLUTE = 0; /相對Panel居中 public static int CHILD_ALIGN_RELATIVE = 1; /相對父結(jié)點居中 private Font font = new Font(微軟雅黑,Font.BOLD,20); /描述結(jié)點的字體 private Color nodeColor = Color.WHITE; /結(jié)點背景顏色 private Color linkLi
24、neColor = Color.BLACK; /結(jié)點連線顏色 private Color stringColor = Color.BLACK; /結(jié)點描述文字的顏色 / 默認(rèn)構(gòu)造 public TreePanel() this(null,CHILD_ALIGN_ABSOLUTE); /根據(jù)傳入的Node繪制樹,以絕對居中的方式繪制 / param n 要繪制的樹 public TreePanel(Node n) this(n,CHILD_ALIGN_ABSOLUTE); /* 1 設(shè)置要繪制時候的對齊策略 2 param childAlignWays 對齊策略 3 see tree.TreeP
25、anel#CHILD_ALIGN_RELATIVE 4 see tree.TreePanel#CHILD_ALIGN_ABSOLUTE */ public TreePanel(int childAlignWays) this(null,childAlignWays); /* 1根據(jù)子對齊策略childAlignWays繪制的樹的根結(jié)點n 2 param n 要繪制的樹的根結(jié)點 3 param childAlignWays 對齊策略 */ public TreePanel(Node n, int childAlignWays) super(); /setTree(n); this.childAl
26、ignWays = childAlignWays; /* 1 設(shè)置用于繪制的樹 2 param n 用于繪制的樹的 */ public void setTree(Node n) tree = n; /重寫,調(diào)用自己的繪制方法 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 x
27、Pos 根節(jié)點的繪制X位置 4 param g 繪圖上下文環(huán)境 */ 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); /畫結(jié)點的格子 g.setColor(stringColor)
28、; g.drawString(n.getName(), x, fontY); /畫結(jié)點的名字 if(n.hasChild() List 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;
29、for(Node node : c) int newX = tempPosx+(nodeWidth+hDIstance)*i; /孩子結(jié)點起始X g.setColor(linkLineColor); g.drawLine(x+nodeWidth/2, y+nodeHeight, newX+nodeWidth/2, y+nodeHeight+vDistance); /畫連接結(jié)點的線 drawAllNode(node, newX, g); i+; public Color getnodeColor() return nodeColor; /* 1 設(shè)置結(jié)點背景顏色 2 param nodeColo
30、r 結(jié)點背景顏色 */ public void setnodeColor(Color nodeColor) this.nodeColor = nodeColor; public Color getLinkLineColor() return linkLineColor; /* * 設(shè)置結(jié)點連接線的顏色 * param nodeLinkLine 結(jié)點連接線的顏色 */ public void setLinkLineColor(Color nodeLinkLine) this.linkLineColor = nodeLinkLine; public Color getStringColor() r
31、eturn stringColor; /* * 設(shè)置結(jié)點描述的顏色 * param stringColor 結(jié)點描述的顏色 */ public void setStringColor(Color stringColor) this.stringColor = stringColor; public int getStartY() return startY; /* * 設(shè)置根結(jié)點的Y位置 * param startY 根結(jié)點的Y位置 */ public void setStartY(int startY) this.startY = startY; public int getStartX() return startX; /* * 設(shè)置根結(jié)點的X位置 * param startX 根結(jié)點的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
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- GB/T 45710-2025聚對苯二甲酸乙二醇酯纖維及切片中低聚物的測定高效聚合物色譜法(APC)
- 2025年食品科學(xué)與工程專業(yè)綜合知識考核試題及答案
- Aromatase-IN-5-生命科學(xué)試劑-MCE
- 2025年人力資源管理政策與實務(wù)試題及答案
- 2025年監(jiān)會與財經(jīng)法規(guī)專業(yè)資格考試試題及答案
- 2025年家庭教育與兒童心理發(fā)展專業(yè)知識考試試卷及答案
- 2025年海洋科學(xué)專業(yè)研究生入學(xué)考試題及答案
- 2025年公共衛(wèi)生管理碩士考試試題及答案
- 愛的禮物我家的寵物狗寫物作文(7篇)
- 一年級寫人作文我的妹妹300字(12篇)
- 互聯(lián)網(wǎng)與營銷創(chuàng)新智慧樹知到期末考試答案章節(jié)答案2024年華東師范大學(xué)
- 云南開放大學(xué)實-用寫作離線作業(yè)1-5
- 四川省成都市溫江縣2023-2024學(xué)年八下物理期末監(jiān)測試題及答案解析
- 內(nèi)科學(xué)(腎臟-內(nèi)分泌-血液)智慧樹知到期末考試答案章節(jié)答案2024年溫州醫(yī)科大學(xué)
- 食品安全與日常飲食智慧樹知到期末考試答案章節(jié)答案2024年中國農(nóng)業(yè)大學(xué)
- 100以內(nèi)進退位加減法口算題每天60道
- MOOC 嵌入式軟件設(shè)計-大連理工大學(xué) 中國大學(xué)慕課答案
- 永久基本農(nóng)田儲備區(qū)劃定技術(shù)方案
- 醫(yī)療銷售經(jīng)驗技巧分享
- 大氣組成與垂直分層(簡潔版)
- 鋼鐵企業(yè)環(huán)保培訓(xùn)課件
評論
0/150
提交評論