> 文档中心 > 《Java 核心技术 卷1》 笔记 第11章 异常、日志、断言和调试(11) 调试器的使用(IDEA环境)

《Java 核心技术 卷1》 笔记 第11章 异常、日志、断言和调试(11) 调试器的使用(IDEA环境)


 11.7 使用调试器

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener; public class BuggyButtonTest {    public static void main(String[] args) { EventQueue.invokeLater(new Runnable() {     @Override     public void run() {  BuggyButtonFrame frame = new BuggyButtonFrame();  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  frame.setVisible(true);     } });    }} class BuggyButtonFrame extends JFrame {    private static final int W = 300;    private static final int H = 200;    public BuggyButtonFrame(){ setTitle("BuggyButtonTest"); setSize(W,H);  BuggyButtonPanel panel = new BuggyButtonPanel(); add(panel);    }} class BuggyButtonPanel extends JPanel{    public BuggyButtonPanel(){ ActionListener listener = new ButtonListener();  String [] colors = new String[]{"Yellow","Blue","Red"}; for(int i = 0; i < colors.length; i++){     JButton b = new JButton(colors[i]);     add(b);     b.addActionListener(listener); }    }     private class ButtonListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) {     String arg = e.getActionCommand();      String [] colors = new String[]{"yellow","blue","red"};     Color[] colorObjs = new Color[]{Color.YELLOW,Color.BLUE,Color.RED};      for(int i = 0;i < colors.length; i++){  if(arg.equals(colors[i])){      setBackground(colorObjs[i]);  }     } }    }}

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

🍓 断点:希望在某一句执行前查看的位置,点击行号左侧,出现红色小圆圈即可(需要在执行前或执行到这一句前加断点)

🍓 IDEA 调试方式1:右键+DEBUG 按钮

🍓 IDEA 调试方式2:点击 main 左侧小图标 + DEBUG 按钮

比如点击这个位置

🍓 查看参数方式 1:下方控制台 Debug->Debugger->右下角查看

🍓 查看参数方式 2:鼠标光标移动到变量上查看

此处发现起的名称不同,把公共名称改为在外侧定义数组,防止重复书写字符串出错

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener; public class BuggyButtonTest {    public static void main(String[] args) { EventQueue.invokeLater(new Runnable() {     @Override     public void run() {  BuggyButtonFrame frame = new BuggyButtonFrame();  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  frame.setVisible(true);     } });    }} class BuggyButtonFrame extends JFrame {    private static final int W = 300;    private static final int H = 200;    public BuggyButtonFrame(){ setTitle("BuggyButtonTest"); setSize(W,H);  BuggyButtonPanel panel = new BuggyButtonPanel(); add(panel);    }} class BuggyButtonPanel extends JPanel{    public BuggyButtonPanel(){ ActionListener listener = new ButtonListener();  for(int i = 0; i < colors.length; i++){     JButton b = new JButton(colors[i]);     add(b);     b.addActionListener(listener); }    }     final String [] colors = new String[]{"Yellow","Blue","Red"};    private class ButtonListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) {     String arg = e.getActionCommand();      final Color[] colorObjs = new Color[]{Color.YELLOW,Color.BLUE,Color.RED};      for(int i = 0;i < colors.length; i++){  if(arg.equals(colors[i])){      setBackground(colorObjs[i]);  }     } }    }}

❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤❤🧡💛💚💙💜🤎🖤

🍉 小贴士:相似的代码尽量用循环处理,公共的变量使用全局变量(成员变量)定义

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

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

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