




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、用java編寫的我的電腦的界面 基本實現(xiàn)“我的電腦”的事件,多為調用系統(tǒng)內部路徑的事件注:代碼中設計到的圖片導入 可以自己進行設置package ex;import javax.swing.*;import javax.swing.event.TreeSelectionEvent;import javax.swing.event.TreeSelectionListener;import javax.swing.plaf.IconUIResource;import javax.swing.tree.DefaultMutableTreeNode;import javax.swing.tree.De
2、faultTreeModel;import javax.swing.tree.TreeNode;import javax.swing.tree.TreePath;import javax.swing.tree.TreeSelectionModel;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.even
3、t.MouseAdapter;import java.awt.event.MouseEvent;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.nio.file.Path;import java.util.Enumeration;import java.util.Scanner;public class ComputerTest extends JFrame implements ActionListener private JPanel header
4、, body, left, right, bottom;/ 頭部庫JPanel address;JLabel tadd;JComboBox massage, search;JButton turn;/ 頭部的兩個菜單JMenuBar menubar1;JToolBar menubar2;JPanel system, others, information;/ 左側信息JCheckBox lookfor, addfile, internet, myfile, control;/ 左側信息JTextArea mginformation;/ 左側信息JPanel upper, middle, low
5、er;/ 右側整體信息JPanel keepfile, harddisk, mobiledisk;/ 右側信息JCheckBox file1, file2, localdisk1, localdisk2, localdisk3, localdisk4,dvd;/ 右側信息ComputerTest() getContentPane().setBackground(Color.RED);/ 定義大致框架header = new JPanel(); / 含有地址欄 兩個菜單欄body = new JPanel();left = new JPanel();right = new JPanel();bo
6、ttom = new JPanel();bottom.setLayout(new FlowLayout();/ 設置框架布局管理this.setLayout(new BorderLayout();/ (框架地址欄)定義頭部信息address = new JPanel();tadd = new JLabel();massage = new JComboBox();massage.addItem(new ImageIcon("image/磁盤.png");massage.addItem("本地磁盤C");massage.addItem("本地磁盤D
7、");massage.addItem("本地磁盤E");massage.addItem("計算機");massage.setFont(new Font("宋體", Font.BOLD, 18);/ 此處定義的應該是一個下拉復選框turn = new JButton("轉到");address.setLayout(new BorderLayout();massage.addItemListener(new ItemListener() Overridepublic void itemStateChanged
8、(ItemEvent e) / TODO 自動生成的方法存根Object source = e.getSource();if (source = massage) Object newpick = e.getItem();if ("本地磁盤C".equals(newpick.toString() try Runtime.getRuntime().exec("cmd /c start C:"); catch (IOException e1) e1.printStackTrace(); else if ("本地磁盤D".equals(ne
9、wpick.toString() try Runtime.getRuntime().exec("cmd /c start D:"); catch (IOException e1) e1.printStackTrace();else if ("計算機".equals(newpick.toString() new ComputerTest(););address.add(massage, BorderLayout.CENTER);address.add(turn, BorderLayout.EAST);/ 添加菜單menubar1 = new JMenuBa
10、r();/ 文件JMenu mFile = new JMenu("文件");JMenuItem mOpen = new JMenuItem("打開");JMenuItem mSave = new JMenuItem("保存");JMenuItem mQuit = new JMenuItem("退出");mFile.add(mOpen);mFile.add(mSave);mFile.addSeparator();mFile.add(mQuit);/ 打開和保存的菜單欄設置mOpen.addActionListener
11、(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO 自動生成的方法存根File file = null;JFileChooser jfc = new JFileChooser();jfc.setApproveButtonText("確定");jfc.setDialogTitle("打開文件");int val = jfc.showOpenDialog(ComputerTest.this);if (val = JFileChooser.APPROVE
12、_OPTION) file = jfc.getSelectedFile();if (val = JFileChooser.CANCEL_OPTION) );mSave.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO 自動生成的方法存根JFileChooser jfc = new JFileChooser();jfc.setApproveButtonText("確定");jfc.setDialogTitle("保存文件&q
13、uot;);int val = jfc.showOpenDialog(ComputerTest.this);if (val = JFileChooser.APPROVE_OPTION) if (val = JFileChooser.CANCEL_OPTION) );mQuit.addActionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e) / TODO 自動生成的方法存根System.exit(0););JMenu mEdit = new JMenu("編輯"
14、);JMenuItem mchexiao = new JMenuItem("撤銷");JMenuItem mhuifu = new JMenuItem("恢復");JMenuItem mfuzhi = new JMenuItem("復制");JMenuItem mjianqie = new JMenuItem("剪切");JMenuItem mzhantie = new JMenuItem("粘貼");JMenuItem mquanxuan = new JMenuItem("全選&qu
15、ot;);JMenuItem mfanxiang = new JMenuItem("反向選擇");mEdit.add(mchexiao);mEdit.add(mhuifu);mEdit.addSeparator();mEdit.add(mfuzhi);mEdit.add(mjianqie);mEdit.add(mzhantie);mEdit.addSeparator();mEdit.add(mquanxuan);mEdit.add(mfanxiang);JMenu mSearch = new JMenu("查看");JMenuItem mzhuangta
16、i = new JMenuItem("狀態(tài)欄");JMenuItem mxiangxi = new JMenuItem("詳細信息");JMenuItem paixu = new JMenuItem("排序方式");JMenuItem mzidingyi = new JMenuItem("自定義文件夾");JMenuItem mzhuanzhi = new JMenuItem("轉至");JMenuItem mshuaxin = new JMenuItem("刷新");mSe
17、arch.add(mzhuangtai);mSearch.addSeparator();mSearch.add(mxiangxi);mSearch.add(paixu);mSearch.add(mzidingyi);mSearch.addSeparator();mSearch.add(mzhuanzhi);mSearch.add(mshuaxin);JMenu mTool = new JMenu("工具");JMenuItem mying = new JMenuItem("映射網(wǎng)絡驅動器");JMenuItem mduan = new JMenuItem
18、("斷開網(wǎng)絡驅動器");JMenuItem tongbu = new JMenuItem("打開同步中心");JMenuItem wenjianjia = new JMenuItem("文件夾選項");mTool.add(mying);mTool.add(mduan);mTool.add(tongbu);mTool.addSeparator();mTool.add(wenjianjia);JMenu mHelp = new JMenu("幫助");JMenuItem mchab = new JMenuItem(&q
19、uot;查看幫助");JMenuItem mguanyu = new JMenuItem("關于");mHelp.add(mchab);mHelp.add(mguanyu);mguanyu.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) / 顯示對話框JOptionPane.showOptionDialog(null, "程序名稱:我的電腦n" + "設計人::楊龍n"+ "簡介:基于Swing設計我
20、的電腦窗口n", "關于:",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE, null, null, null););menubar1.add(mFile);menubar1.add(mEdit);menubar1.add(mSearch);menubar1.add(mTool);menubar1.add(mHelp);menubar2 = new JToolBar();JButton gbar1 = new JButton("卸載或更改程序");JButton gbar2
21、 = new JButton("系統(tǒng)屬性");JButton gbar3 = new JButton("打開控制面板");gbar1.setFont(new Font("宋體", Font.BOLD, 18);gbar2.setFont(new Font("宋體", Font.BOLD, 18);gbar3.setFont(new Font("宋體", Font.BOLD, 18);menubar2.add(gbar2);menubar2.add(gbar1);menubar2.add(gbar
22、3);/ 給工具面板添加事件gbar3.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 1) try Runtime.getRuntime().exec("cmd /c start C:windowssystem32control.exe"); catch (IOException e1) e1.printStackTrace(););gbar2.addMouseListener(new MouseAdapter() publ
23、ic void mouseClicked(MouseEvent e) if (e.getClickCount() = 1) try Runtime.getRuntime().exec("cmd /c start C:WINDOWSSystem32sysdm.cpl"); catch (IOException e1) e1.printStackTrace(););gbar1.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 1)
24、try Runtime.getRuntime().exec("cmd /c start C:WINDOWSSystem32appwiz.cpl"); catch (IOException e1) e1.printStackTrace(););header.setLayout(new GridLayout(3, 1);header.add(address);header.add(menubar1);header.add(menubar2);/ 設置菜單事件監(jiān)聽/ (框架內容)設置內容布局管理body.setLayout(new BorderLayout();/ 添加內容信息
25、左中下三個面板進行劃分body.add(left, BorderLayout.WEST);body.add(right, BorderLayout.CENTER);/ body.add(bottom,BorderLayout.SOUTH);/ 設置內容的左側布局管理left.setLayout(new GridLayout(2, 1, 10, 40);/ 定義內容的左側信息system = new JPanel();others = new JPanel();information = new JPanel();/ ttree.setLayout(new GridLayout(2,1);/ 設
26、置樹結構final JTree trr = new JTree();trr.setRootVisible(false);trr.setFont(new Font("仿宋", Font.BOLD, 15);trr.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);trr.setRowHeight(20);DefaultMutableTreeNode root = new DefaultMutableTreeNode("根");DefaultMutab
27、leTreeNode root2 = new DefaultMutableTreeNode("收藏夾");DefaultMutableTreeNode Yc2 = new DefaultMutableTreeNode("下載");DefaultMutableTreeNode Yd2 = new DefaultMutableTreeNode("桌面");DefaultMutableTreeNode Ye2 = new DefaultMutableTreeNode("新建文件夾");DefaultMutableTree
28、Node Yy2 = new DefaultMutableTreeNode("最近訪問的位置");root2.add(Yc2);root2.add(Yd2);root2.add(Ye2);root2.add(Yy2);DefaultMutableTreeNode root1 = new DefaultMutableTreeNode("我的電腦");DefaultMutableTreeNode Yc = new DefaultMutableTreeNode("本地磁盤(C:)");DefaultMutableTreeNode Yd =
29、new DefaultMutableTreeNode("本地磁盤(D:)");DefaultMutableTreeNode Ye = new DefaultMutableTreeNode("本地磁盤(E:)");DefaultMutableTreeNode Yy = new DefaultMutableTreeNode("DVD驅動器(G:)");root1.add(Yc);root1.add(Yd);root1.add(Ye);root1.add(Yy);root.add(root2);root.add(root1);(Defaul
30、tTreeModel)trr.getModel().setRoot(root);left.add(new JScrollPane(trr);/ 設置其他位置信息布局管理others.setLayout(new GridLayout(3, 1, 1, 1);/ 定義其他位置信息others.setBorder(BorderFactory.createTitledBorder("其他位置:");internet = new JCheckBox("網(wǎng)上鄰居", new ImageIcon("image/網(wǎng)上鄰居.png");myfile =
31、 new JCheckBox("我的文檔", new ImageIcon("image/我的文檔.png");control = new JCheckBox("控制面板", new ImageIcon("image/控制面板.png");internet.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 1) /new HomeTest(););/ 添加指定的項偵聽器,
32、接收來自此復選框的項事件myfile.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 1) try Runtime.getRuntime().exec("cmd /c start c:Users"); catch (IOException e1) e1.printStackTrace(););control.addMouseListener(new MouseAdapter() public void mouseClicked
33、(MouseEvent e) if (e.getClickCount() = 1) try Runtime.getRuntime().exec("cmd /c start C:windowssystem32control.exe"); catch (IOException e1) e1.printStackTrace(););/ 添加其他位置信息others.add(internet);others.add(myfile);others.add(control);left.add(others);/ 設置詳細信息布局管理information.setLayout(new B
34、orderLayout();/ mginformation = new JTextArea(5,10);/ information.add(mginformation);/ 定義詳細信息JLabel s0 = new JLabel(new ImageIcon("image/大圖標.png");JLabel s1 = new JLabel(new ImageIcon("image/詳細信息.png");/ 添加詳細信息information.add(s0, BorderLayout.WEST);information.add(s1, BorderLayou
35、t.CENTER);bottom.add(information, BorderLayout.WEST);/ 設置內容的右側布局管理right.setLayout(new GridLayout(3, 1);/ 定義內容的右側整體信息upper = new JPanel();middle = new JPanel();lower = new JPanel();harddisk = new JPanel();keepfile = new JPanel();mobiledisk = new JPanel();/ 設置內容的右側"本地磁盤"布局管理harddisk.setLayou
36、t(new GridLayout(1, 3);/ 定義內容的右側"本地磁盤"信息harddisk.setBorder(BorderFactory.createTitledBorder("硬盤:");localdisk1 = new JCheckBox("本地磁盤(C:)", new ImageIcon("image/系統(tǒng)圖標.png");localdisk2 = new JCheckBox("本地磁盤(D:)", new ImageIcon("image/磁.png");lo
37、caldisk3 = new JCheckBox("本地磁盤(E:)", new ImageIcon("image/磁.png");localdisk1.setFont(new Font("宋體", Font.BOLD, 18);localdisk2.setFont(new Font("宋體", Font.BOLD, 18);localdisk3.setFont(new Font("宋體", Font.BOLD, 18);/ localdisk1.addmou(this);/添加指定的項偵聽器,
38、接收來自此復選框的項事件localdisk2.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 2) try Runtime.getRuntime().exec("cmd /c start D:"); catch (IOException e1) e1.printStackTrace(););localdisk1.addMouseListener(new MouseAdapter() public void mouseClick
39、ed(MouseEvent e) if (e.getClickCount() = 2) try Runtime.getRuntime().exec("cmd /c start C:"); catch (IOException e1) e1.printStackTrace(););localdisk3.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 2) setTitle("本地磁盤(E:)");right.
40、removeAll();right.setLayout(new GridLayout(1, 1);JPanel ee = new JPanel();JPanel ee1 = new JPanel();JCheckBox pic = new JCheckBox("圖片", new ImageIcon("image/文件夾1.png");JCheckBox mus = new JCheckBox("音樂", new ImageIcon("image/文件夾1.png");pic.setFont(new Font(&qu
41、ot;宋體", Font.BOLD, 18);mus.setFont(new Font("宋體", Font.BOLD, 18);ee.setLayout(new BorderLayout();ee.setBorder(BorderFactory.createTitledBorder("");ee1.add(pic);ee1.add(mus);ee.add(ee1, BorderLayout.WEST);right.add(ee, FlowLayout.LEFT);repaint();pic.addMouseListener(new Mouse
42、Adapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 2) /new Diske(););mus.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 2) /new Diske1();););localdisk1.mouseMove(null, getState(), getState();localdisk2.mouseMove(null, getState
43、(), getState();/ 添加內容的右側"本地磁盤"信息right.add(harddisk, BorderLayout.WEST);/ middle.add();harddisk.add(localdisk1);harddisk.add(localdisk2);harddisk.add(localdisk3);/ 設置內容的右側"移動磁盤"布局管理mobiledisk.setLayout(new GridLayout(1, 4);/ 定義內容的右側"移動磁盤"信息mobiledisk.setBorder(BorderFact
44、ory.createTitledBorder("有可移動存儲的設備:");dvd = new JCheckBox("DVD驅動器(G:)", new ImageIcon("image/dvd1.png");JCheckBox bd = new JCheckBox("BD驅動器(H:)", new ImageIcon("image/bd.png");dvd.setFont(new Font("宋體", Font.BOLD, 18);bd.setFont(new Font(&qu
45、ot;宋體", Font.BOLD, 18);dvd.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 2) JOptionPane.showOptionDialog(null, "erro提示:n"+ "檢查不到光盤,無法訪問!n" + "請確認光盤是否插入!n", "錯誤!",JOptionPane.DEFAULT_OPTION,JOptionPane.I
46、NFORMATION_MESSAGE, null, null, null););bd.addMouseListener(new MouseAdapter() public void mouseClicked(MouseEvent e) if (e.getClickCount() = 2) JOptionPane.showOptionDialog(null, "erro提示:n"+ "檢查不到光盤,無法訪問!n" + "請確認光盤是否插入!n", "錯誤!",JOptionPane.DEFAULT_OPTION,JO
47、ptionPane.INFORMATION_MESSAGE, null, null, null););/ 添加指定的項偵聽器,接收來自此復選框的項事件/ 添加內容的右側"移動磁盤"信息right.add(mobiledisk);/ lower.add();mobiledisk.add(dvd);mobiledisk.add(bd);/ 設置內容右側"存儲文件"布局管理keepfile.setLayout(new GridLayout(1, 3);/ 定義內容的右側"存儲文件"信息keepfile.setBorder(BorderFac
48、tory.createTitledBorder("其他:");/ 為p5設置邊界添加邊界名file1 = new JCheckBox("Disc Viewer", new ImageIcon("image/view.png");file1.setFont(new Font("宋體", Font.BOLD, 18);/ 添加內容的右側"存儲文件"信息right.add(keepfile);keepfile.add(file1);trr.addTreeSelectionListener(new Tr
49、eeSelectionListener() Overridepublic void valueChanged(TreeSelectionEvent e) / TODO 自動生成的方法存根TreePath path = trr.getSelectionPath();if (path = null) return;DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();if (node.toString().equals("本地磁盤(C:)") try Runtime.getRuntime().exec("cmd /c start C:"); catch (IOException e1) e1.printStackTrace(); else if (node.toString().equals("本地磁盤(D:)") try Runtime.getRuntime().exec("cmd /c start D:"); catch (IOException e1) e1.printSt
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年國內保理業(yè)務協(xié)議應收賬款池融資版
- 一年級下數(shù)學教案-退位減法-西師大版
- 2024-2025學年一年級下學期數(shù)學第二單元位置《左和右》(教案)
- 2025年公司和個人簽訂的勞務合同模板
- 六年級上冊數(shù)學教案-4.1 比的基本性質 ︳青島版
- 一年級下冊數(shù)學教案-小兔請客1 北師大版
- 2025年倉儲保管合同樣本常用版
- 學習2025年雷鋒精神62周年主題活動方案 (3份)
- 2025年合肥經(jīng)濟技術職業(yè)學院單招職業(yè)適應性測試題庫完整
- 期中(試題)-外研版(三起)英語三年級下冊-(含答案)
- 《魅力教師的修煉》讀書心得體會4篇
- 2016年百貨商城商場超市企劃全年活動策劃方案模板
- 民航法規(guī)與實務PPT全套教學課件
- 15 分章專項練習-整本書閱讀系列《經(jīng)典常談》名著閱讀與練習
- 幼兒園衛(wèi)生保健人員任命書(保健醫(yī)生)
- 一課一練┃二年級下冊:1古詩二首
- 財務報表2019新版-已執(zhí)行新金融和收入準則(財會〔2019〕6號)
- 2023年湖南食品藥品職業(yè)學院高職單招(英語)試題庫含答案解析
- GB/T 39096-2020石油天然氣工業(yè)油氣井油管用鋁合金管
- 爐外精煉說課
- 紅色喜慶大氣軍令狀2022頒獎誓師大會動態(tài)PPT模板
評論
0/150
提交評論