会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133803个问题

# coding=utf-8
"""开发记事本软件的菜单"""

from tkinter.filedialog import *
from tkinter.colorchooser import *
from tkinter import *


class Application(Frame):

    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.textpad = None
        self.pack()
        self.createWidget()

    def createWidget(self):
        # 创建主菜单栏
        menubar = Menu(root)

        # 创建主菜单三个
        menuFile = Menu(menubar)
        menuEdit = Menu(menubar)
        menuHelp = Menu(menubar)

        # 将三个主菜单加入主菜单栏中
        menubar.add_cascade(text="文件[F]", menu=menuFile)
        menubar.add_cascade(text="编辑[E]", menu=menuEdit)
        menubar.add_cascade(text="帮助[H]", menu=menuHelp)

        # 为文件[F]增加子菜单项
        menuFile.add_command(label="新建", accelerator="ctrl+n", command=self.newfile)
        menuFile.add_command(label="打开", accelerator="ctrl+o", command=self.openfile)
        menuFile.add_command(label="保存", accelerator="ctrl+s", command=self.savefile)
        menuFile.add_separator()  # 添加分行符
        menuFile.add_command(label="退出", accelerator="ctrl+q", command=self.exit)

        # 将主菜单添加到窗口中
        root["menu"] = menubar

        # 增加快捷键的处理
        root.bind("<Control-n>", lambda event: self.newfile())
        root.bind("<Control-o>", lambda event: self.openfile())
        root.bind("<Control-s>", lambda event: self.savefile())
        root.bind("<Control-q>", lambda event: self.exit())

        # 文本编辑区
        self.textpad = Text(root, width=50, height=30)
        self.textpad.pack()

        # 创建上下菜单
        self.contextMenu = Menu(root)
        self.contextMenu.add_command(label="背景颜色", command=self.openAskColor)

        # 为右键绑定事件
        root.bind("Button-3", self.createContextMenu)

    def newfile(self):
        self.textpad.delete("1.0", "end")  # 把text中的所有的内容清空
        self.filename = asksaveasfilename(title="另存为", initialfile="未命名.txt",
                                          filetypes=[("文本文档", "*.txt")], defaultextension=".txt")
        self.savefile()

    def openfile(self):
        self.textpad.delete("1.0", "end")  # 把text中的所有的内容清空
        with askopenfile(title="打开文本文件") as f:
            self.textpad.insert(INSERT, f.read())
            self.filename = f.name

    def savefile(self):
        self.textpad.delete("1.0", "end")  # 把text中的所有的内容清空
        with open(self.filename, "w") as f:
            c = self.textpad.get(1.0, END)
            f.write(c)

    def exit(self):
        root.quit()

    def openAskColor(self):
        s1 = askcolor(color="red", title="选择背景色")
        self.textpad.config(bg=s1[1])
        pass

    def createContextMenu(self, event):
        # 菜单在鼠标右键单击的座标处显示
        self.contextMenu.post(event.x_root, event.y_root)


if __name__ == '__main__':
    root = Tk()
    root.geometry("500x400+200+300")
    root.title("私用记事本")
    app = Application(master=root)
    root.mainloop()

屏幕截图 2021-03-29 190337.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 11656楼
JAVA 全系列/第十二阶段:消息中间件与高并发处理/RabbitMQ 11658楼
JAVA 全系列/第六阶段:项目管理与SSM框架/RBAC实战 11659楼
JAVA 全系列/第一阶段:AI驱动的JAVA编程/面向对象详解和JVM底层内存分析 11660楼

public class TestDeadLock {
    public static void main(String[] args) {
        MakeUp mu1 = new MakeUp(0,"小丫");
        mu1.start();

        MakeUp mu2 = new MakeUp(1,"大丫");
        mu2.start();
    }
}

/**
 * 口红类
 */
class Lipstick{

}

/**
 * 镜子类
 */
class Mirror{

}

class MakeUp extends Thread{

    private int flag; //flag = 0:手上有口红 flag != 0:手上有镜子
    private String name;
    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();

    public MakeUp(int flag, String name) {
        this.flag = flag;
        this.name = name;
    }

    public MakeUp() {
    }

    @Override
    public void run() {
        this.doMakeUp();
    }

    /**
     * 开始化妆
     */
    public void doMakeUp(){
        if (this.flag == 0){
            synchronized (lipstick){
                System.out.println(this.name+" 拿着口红");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (mirror){
                    System.out.println(this.name +" 拿着镜子");
                }
            }
        }else {
            synchronized (mirror){
                System.out.println(this.name +" 拿着镜子");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            synchronized (lipstick){
                System.out.println(this.name +" 拿着口红");
            }
        }
    }
}

图片.png

我确定是跟着视频里一个一个敲的,但是没有出现死锁情况是怎么回事啊

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 11662楼
JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 11665楼

public class TestFileBufferStream {
    public static void main(String[] args) {
        long time1 = System.currentTimeMillis();
        copyFile("C:\\Users\\周则霖\\Desktop\\zzl.jpg","C:\\Users\\周则霖\\Desktop\\zzl2.jpg");
        long time2 = System.currentTimeMillis();
        System.out.println(time2 - time1);
    }

    /**
     *
     * @param source 源文件
     * @param destination  目的地文件
     */
    public static void copyFile(String source,String destination){
            //实例化节点流对象
        try(FileInputStream fis = new FileInputStream(source);
            FileOutputStream fos = new FileOutputStream(destination);
            //实例化处理流对象
            BufferedInputStream bis = new BufferedInputStream(fis);
            BufferedOutputStream bos = new BufferedOutputStream(fos)){

            //实例化缓存
            byte[] buffer = new byte[8192];

            int temp = 0;
            //temp = bis.read(buffer);
            //System.out.println(temp);
            while((temp = bis.read(buffer)) != -1){
                bos.write(buffer,0,temp);
                //System.out.println(temp);
            }

            bos.flush();

        }catch(IOException e){
            e.printStackTrace();
        }

    }
}
while((temp = bis.read(buffer)) != -1){
    bos.write(buffer,0,temp);
}

老师,在这段代码中,read实际上读取的的是buffer数组的长度,如果没有填满或者数组为空的时候,就会返回-1,我可以这样理解吗?那buffer数组的填充也是由read来完成的吗?


JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO流技术 11667楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 11669楼
JAVA 全系列/第九阶段:SpringBoot与MybatisPlus/Spring Boot旧 11670楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637