Java基础

类与对象
1)什么是类?
类是一些具有相同属性和行为的事物的集合
2)什么是对象?
对象是生活中的一个一个的实体、客观的。
3)面向对象编程思想(OOP)
基本语法:
A.一个类对应一个java文件
B.属性的定义格式:public 类型 属性名
C.方法的定义格式:public 返回类型 方法名(参数列表){}
D.创建对象你给的格式:类名 对象名 = new 类名();
E.调用属性:对象名.属性名
F.调用方法:对象名.方法名(具体内容)
实例:计算机软件调用工具实现(开关机操作)
这里写图片描述
主方法:
public class Main {
public static void main(String[] args) {
UI test=new UI();
}
}
UI界面
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 UI extends JFrame {
/**
* @author xhh
*/
private JButton btn_shutdown;
private JButton btn_reboot;
private JButton btn_openBrowser;
//构造方法
public UI() {
super(“电脑操作界面”);
this.setSize(400, 500); // 设置窗体大小
this.setDefaultCloseOperation(3);
this.setLocationRelativeTo(null); // 设置窗体居中
this.setLayout();
this.addListener();
this.setVisible(true); // 设置窗体可见
}
//设计UI界面
private void setLayout(){
btn_shutdown=new JButton(“关机”);
btn_reboot=new JButton(“打开QQ”);
btn_openBrowser=new JButton(“打开浏览器”);
this.setLayout(new FlowLayout());
this.add(btn_shutdown);
this.add(btn_reboot);
this.add(btn_openBrowser);

}
//为组件添加监听
private void addListener(){
    ActionListener actionListener=new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            String event=e.getActionCommand();
            Runtime run=Runtime.getRuntime();
            if("关机".equals(event)){
                try{
                run.exec("shutdown -s -t 10000");
                }catch (IOException e1){
                    e1.printStackTrace();
                }
            }else if("打开QQ".equals(event)){
                try{
                    System.out.println("点击了打开QQ");
                    run.exec("D:\\Tencent\\QQ\\Bin\\QQ.exe");
                    }catch (IOException e1){
                        e1.printStackTrace();
                    }
            }else if("打开浏览器".equals(event)){
                try{
                    run.exec("explorer http://www.baidu.com");
                    }catch (IOException e1){
                        e1.printStackTrace();
                    }


            }
        }
    };

    btn_shutdown.addActionListener(actionListener);
    btn_reboot.addActionListener(actionListener);
    btn_openBrowser.addActionListener(actionListener);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值