本文共 2842 字,大约阅读时间需要 9 分钟。
1. 实例介绍:
通过面向对象的思想,JAVA编写一个ComputerOperation,功能为:快捷的实现计算机一些常用的 操作指令:关机,打开网址,打开QQ, 打开计算器。 2. 实例运行效果运行主界面图
打开QQ
关机
点击关机后会变成取消关机
点击进入网址就进入预先设定好的网站
3. 实例源代码重点内容:
///MianFrame.java 源代码
“`
package com.lt.view;import java.awt.Dimension;
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException;import javax.swing.JButton;
import javax.swing.JFrame;public class MainFrame extends JFrame {
//定义控件 private JButton shutdownOrCancle = new JButton(“关机”); private JButton openQQ = new JButton(“打开QQ”); private JButton openMyBlog = new JButton(“进入网站”); private JButton openClac = new JButton(“打开计算器”); private Dimension frameSize = new Dimension(400, 300); //构造方法 public MainFrame() { //基本属性设置 this.setSize(frameSize); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLayout(new FlowLayout()); //添加控件 this.add(openClac); this.add(openMyBlog); this.add(openQQ); this.add(shutdownOrCancle);this.setVisible(true); openClac.addActionListener(actionListener); openQQ.addActionListener(actionListener); shutdownOrCancle.addActionListener(actionListener); openMyBlog.addActionListener(actionListener);}ActionListener actionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //获取按钮的内容 String even = e.getActionCommand(); //获取线程 Runtime runtime = Runtime.getRuntime(); //对时间进行分类操作 switch (even) { case "关机": try { runtime.exec("shutdown -s -t 1000"); } catch (IOException e1) { e1.printStackTrace(); } shutdownOrCancle.setText("取消关机"); break; case "打开QQ": try { runtime.exec("D:\\Program Files (x86)\\Tencent\\QQ\\Bin\\QQScLauncher.exe"); } catch (IOException e1) { e1.printStackTrace(); } break; case "进入网站": try { runtime.exec("explorer http://www.ltbetter.com"); } catch (IOException e1) { e1.printStackTrace(); } break; case "打开计算器": try { runtime.exec("calc"); } catch (IOException e1) { e1.printStackTrace(); } break; case "取消关机": try { runtime.exec("shutdown -a"); } catch (IOException e1) { e1.printStackTrace(); } shutdownOrCancle.setText("关机"); break; default: break; } }};
}
4. 上课笔记(个人理解):
1. 对象: 对象 a = b : 是把b对象的地址赋值给a。而不是赋值。 2.继承 子类可以继承到父类的所有的非私有的所有成员。 继承的作用: 动态的扩展类的功能。 3.自动转型 子类类型自动转换成父类类型 转型后的对象,调用方法,如果子类重写了父类的方法,则调用的是重写后的方法。 D obj = new A(); D obj2 = new D(); 二者不一样!