栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

《Java 核心技术 卷1》 笔记 第11章 异常、日志、断言和调试(9)自制控制台窗口与窗口事件抓取

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

《Java 核心技术 卷1》 笔记 第11章 异常、日志、断言和调试(9)自制控制台窗口与窗口事件抓取

 11.6.1 使用控制台窗口

调试时,因为信息较多,比起机器捕捉关键词,有时人眼直接捕捉会更有效率。此时就需要滚动的窗口,而非 cmd,terminal 之类的简易控制台。就可带滚动条继续打印 System.out/System.err的内容。

修订版(作者弄得白背景黑字感觉不像,自己调了一下):

 ConsoleWindow.java

李癩李癩李癩

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.Styleddocument;
import java.awt.*;
import java.io.OutputStream;
import java.io.PrintStream;
 
public class ConsoleWindow {
 
    private static final int W = 800;
    private static final int H = 500;
    private static final int L = 200;
    private static final int T = 200;
    public static void init(){
        Jframe frame = new Jframe();
        frame.setTitle("ConsoleWindow");
        final JTextPane output = new JTextPane();
        output.setEditable(false);
        frame.add(new JScrollPane(output));
        frame.setSize(W,H);
        frame.setLocation(L,T);
        frame.setFocusableWindowState(false);
        frame.setVisible(true);
        output.setBackground(Color.BLACK);
        output.setFont(new Font("DIALOG",Font.BOLD,20));
        Styleddocument d = output.getStyleddocument();
        SimpleAttributeSet attr = new SimpleAttributeSet();
        PrintStream consoleStream = new PrintStream(
                new OutputStream(){
                    @Override
                    public void write(int b) {}
 
                    public void write(byte[] b, int off, int len) {
                        StyleConstants.setForeground(attr,Color.WHITE);
                        try {
                            d.insertString(d.getLength(),new String(b,off,len),attr);
                        } catch (BadLocationException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
 
        new Font("",Font.BOLD,5);
 
        PrintStream consoleStream2 = new PrintStream(
                new OutputStream(){
                    @Override
                    public void write(int b) {}
 
                    public void write(byte[] b, int off, int len){
                        StyleConstants.setForeground(attr,Color.RED);
                        try {
                            d.insertString(d.getLength(),new String(b,off,len),attr);
                        } catch (BadLocationException e) {
                            e.printStackTrace();
                        }
 
                    }
                }
        );
 
        System.setOut(consoleStream);
        System.setErr(consoleStream2);
    }
}

李癩李癩李癩

 

 Main.java

李癩李癩李癩

public class Main {
    public static  void main(String[] args) {
        Main solution = new Main();
        ConsoleWindow.init();
        for(int i = 0; i < 10; i++){
            System.out.println("out "+i);
            System.err.println("err "+i);
        }
    }
}

李癩李癩李癩

 

 

 

 

 11.6.2 跟踪 AWT 事件

 

我们希望当 AWT 组件和用户交互过程中,能显示提示信息,应该捕获信息,并将状态信息进行输出。

这部分挺有意思的,大致就是作者通过事件描述符,拦截到事件,然后交给代理类去处理,然后代理类完成了输出工作。为了和前面的内容结合,又把刚刚的控制台利用上了。这个控制台不会自动换行,内容一长看的头疼。找了好久,终于让我找到一个设置自动换行的工具,MyHTMLEditorKit:

 

 MyHTMLEditorKit.class

李癩李癩李癩

import javax.swing.*;
import javax.swing.text.Element;
import javax.swing.text.ParagraphView;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.InlineView;
 
public class MyHTMLEditorKit extends HTMLEditorKit {
    private static final long serialVersionUID = 3632670469611941371L;
 
    @Override
    public ViewFactory getViewFactory() {
 
        return new HTMLFactory() {
            public View create(Element e) {
                View v = super.create(e);
                if (v instanceof InlineView) {
                    return new InlineView(e) {
                        public int getBreakWeight(int axis, float pos, float len) {
                            return GoodBreakWeight;
                        }
 
                        public View breakView(int axis, int p0, float pos, float len) {
                            if (axis == View.X_AXIS) {
                                checkPainter();
                                int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
                                if (p0 == getStartOffset() && p1 == getEndOffset()) {
                                    return this;
                                }
                                return createFragment(p0, p1);
                            }
                            return this;
                        }
                    };
                } else if (v instanceof ParagraphView) {
                    return new ParagraphView(e) {
                        protected SizeRequirements calculateMinorAxisRequirements(int axis,
                                                                                  SizeRequirements r) {
                            if (r == null) {
                                r = new SizeRequirements();
                            }
                            float pref = layoutPool.getPreferredSpan(axis);
                            float min = layoutPool.getMinimumSpan(axis);
                            r.minimum = (int) min;
                            r.preferred = Math.max(r.minimum, (int) pref);
                            r.maximum = Integer.MAX_VALUE;
                            return r;
                        }
 
                    };
                }
                return v;
            }
        };
    }
}

李癩李癩李癩

 

 ConsoleWindow.class

李癩李癩李癩

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.io.OutputStream;
import java.io.PrintStream;
 
public class ConsoleWindow {
 
    private static final int W = 800;
    private static final int H = 500;
    private static final int L = 200;
    private static final int T = 200;
    public static void init(){
        EventTracer tracer = new EventTracer();
        Jframe frame = new Jframe();
        tracer.add(frame);
        frame.setTitle("ConsoleWindow");
        final JTextPane output = new JTextPane();
        output.setEditorKit(new MyHTMLEditorKit());
        output.setEditable(false);
        output.setSize(W,H);
        frame.add(new JScrollPane(output));
        frame.setSize(W,H);
        frame.setLocation(L,T);
        frame.setFocusableWindowState(false);
        frame.setVisible(true);
        output.setBackground(Color.BLACK);
        output.setFont(new Font("DIALOG",Font.BOLD,20));
        Styleddocument d = output.getStyleddocument();
        SimpleAttributeSet attr = new SimpleAttributeSet();
        PrintStream consoleStream = new PrintStream(
                new OutputStream(){
                    @Override
                    public void write(int b) {}
 
                    public void write(byte[] b, int off, int len) {
                        StyleConstants.setForeground(attr,Color.WHITE);
                        try {
                            d.insertString(d.getLength(),new String(b,off,len),attr);
                        } catch (BadLocationException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
 
        new Font("",Font.BOLD,5);
 
        PrintStream consoleStream2 = new PrintStream(
                new OutputStream(){
                    @Override
                    public void write(int b) {}
 
                    public void write(byte[] b, int off, int len){
                        StyleConstants.setForeground(attr,Color.RED);
                        try {
                            d.insertString(d.getLength(),new String(b,off,len),attr);
                        } catch (BadLocationException e) {
                            e.printStackTrace();
                        }
 
                    }
                }
        );
 
        System.setOut(consoleStream);
        System.setErr(consoleStream2);
    }
}

李癩李癩李癩

 Main.java

李癩李癩李癩

 

public class Main {

    public static  void main(String[] args) {

        Main solution = new Main();

        ConsoleWindow.init();

    }

}

李癩李癩李癩

 

 

然后发现书的作者还有第二段代码。。。嗯~ o(* ̄▽ ̄*)o,跑偏了。那就再试下呗。

 

 

给了个滚动条,还有个按钮,咱把刚刚的控制台再加进来,来个三级运载火箭合体:

 

李癩李癩李癩

import javax.swing.*;
import java.awt.*;
 
public class EventTracerTest {
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                Jframe frame = new EventTracerframe();
                frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}
 
class EventTracerframe extends Jframe{
    private static final int W = 400;
    private static final int H = 400;
 
    public EventTracerframe(){
        setTitle("EventTracerTest");
        setSize(W,H);
        add(new JSlider(),BorderLayout.NORTH);
        add(new JButton("Test"),BorderLayout.SOUTH);
 
        ConsoleWindow.init();
        EventTracer tracer = new EventTracer();
        tracer.add(this);
    }
}

李癩李癩李癩

控制台代码的这两行屏蔽一下:

 

 

相关内容:选择 《Java核心技术 卷1》查找相关笔记

评论点赞收藏✨关注,是送给作者最好的礼物,愿我们共同学习,一起进步

如果对作者发布的内容感兴趣,可点击下方关注公众号 钰娘娘知识汇总 查看更多作者文章哦!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/780707.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号