前几天看了一些大牛的博客,顿时觉得自己的博客写的不好,不够详细,不够深刻,今天我就要写一些有力量的东西,顺便说一句胡哥说:“只要博客写的好,就能找到妹子”,我先试一下。言归正传,先传一下我的仿XP画板的图片
至于整个画图板的功能,实现了一些选颜色与工具的功能,没有涉及到文件的部分功能,这篇文章主要就是通过画图板这个实例讲一下当一个项目的类的数量过多的时候怎样传值,以及在那种情况下使用内部类和外部类。
写画图板的时候,刚开始必须要先将画图板的布局,以及组件加上。请看下面的代码
package Draw; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JSeparator; /** * 此类主要是显示面板,并向其上面添加组件 * @author 李伟 * */ public class DrawBord extends JFrame{ private JMenuBar jmenubar; private JPanel panelWest; private JPanel panelSouth; private JPanel panelCentral; private Graphics g; private JButton but01; private ButColorListener bl; private MenuItemListener ml; //添加标志 public static String biaozhi="huabi"; //定义按钮 private JButton but; //定义显示的方法 public void showUI(){ panelCentral = creatCentral(); //添加菜单条 jmenubar = creatMenuBar(); this.setJMenuBar(jmenubar); //添加工具栏 panelWest = creatWest(); //添加颜色选择栏 panelSouth = creatSouth(); //设置画板标题 this.setTitle("仿XP画板"); //设置画板大小尺寸 this.setSize(800,600); //设置画板显示在窗体中央 this.setLocationRelativeTo(null); this.add(panelCentral,BorderLayout.CENTER); this.add(panelWest,BorderLayout.WEST); this.add(panelSouth,BorderLayout.SOUTH); this.setDefaultCloseOperation(3); //设置窗体为不可改变大小的 this.setResizable(false); //设置窗体可见 this.setVisible(true); g = panelCentral.getGraphics(); bl.setGraphics(g); ml.setGraphics(g); ml.setPanel(panelCentral); FrameListener fl = new FrameListener(g); //将画布及鼠标监听器传过去 panelCentral.addMouseListener(fl); FrameMouseMotion mm= new FrameMouseMotion(g); panelCentral.addMouseMotionListener(mm); } /*********设置画板上的菜单栏***********/ private JMenuBar creatMenuBar(){ ml = new MenuItemListener(this); //创建菜单栏 JMenuBar menubar = new JMenuBar(); //设置菜单 JMenu menu01 = new JMenu("文件(F)"); JMenu menu02 = new JMenu("编辑(E)"); JMenu menu03 = new JMenu("查看(V)"); JMenu menu04 = new JMenu("图像(I)"); JMenu menu05 = new JMenu("颜色(C)"); JMenu menu06 = new JMenu("帮助(H)"); JMenu menu07 = new JMenu("自选图形"); //创建文件菜单项 JMenuItem FItem01 = new JMenuItem("新建(N)"); JMenuItem FItem02 = new JMenuItem("打开(O)"); JMenuItem FItem03 = new JMenuItem("保存(S)"); JMenuItem FItem04 = new JMenuItem("另存为(A)"); JSeparator Separator01 = new JSeparator(); JMenuItem FItem05 = new JMenuItem("退出(X)"); FItem05.addActionListener(ml); menu01.add(FItem01); menu01.add(FItem02); menu01.add(FItem03); menu01.add(FItem04); menu01.add(Separator01); menu01.add(FItem05); //创建编辑菜单项 JMenuItem EItem01 = new JMenuItem("撤销(U)"); JMenuItem EItem02 = new JMenuItem("重复(R)"); JSeparator Separator02 = new JSeparator(); JMenuItem EItem03 = new JMenuItem("剪切(X)"); JMenuItem EItem04 = new JMenuItem("复制(C)"); JMenuItem EItem05= new JMenuItem("粘贴(V)"); menu02.add(EItem01); menu02.add(EItem02); menu02.add(Separator02); menu02.add(EItem03); menu02.add(EItem04); menu02.add(EItem05); //创建查看菜单项 JMenuItem VItem01 = new JMenuItem("没有东西"); menu03.add(VItem01); //创建图像菜单项 JMenuItem IItem01 = new JMenuItem("清除图像"); IItem01.addActionListener(ml); menu04.add(IItem01); //创建颜色菜单项 JMenuItem CItem01 = new JMenuItem("颜色选择"); CItem01.addActionListener(ml); menu05.add(CItem01); //创建帮助菜单项 JMenuItem HItem01 = new JMenuItem("关于画图"); HItem01.addActionListener(ml); menu06.add(HItem01); //创建自选图形菜单项 JMenuItem ZItem01 = new JMenuItem("科赫曲线"); JMenuItem ZItem02 = new JMenuItem("科赫雪花"); JMenuItem ZItem03 = new JMenuItem("毕达哥拉斯树"); JMenuItem ZItem04 = new JMenuItem("树"); JMenuItem ZItem05= new JMenuItem("科赫曲线"); JMenuItem ZItem06= new JMenuItem("科赫曲线"); menu07.add(ZItem01); menu07.add(ZItem02); menu07.add(ZItem03); menu07.add(ZItem04); menu07.add(ZItem05); menu07.add(ZItem06); //想菜单条上加上菜单 menubar.add(menu01); menubar.add(menu02); menubar.add(menu03); menubar.add(menu04); menubar.add(menu05); menubar.add(menu07); menubar.add(menu06); return menubar; } /***********创建左边工具选择面板***********/ private JPanel creatWest(){ JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(70,500)); //创建一个监听器类 ToolButListener tb = new ToolButListener(biaozhi); String[] arr = { "duobianxing", "Xjuxing", "ca", "tianchong", "dianbi", "fangdajing", "huabi", "shuazi", "youqitong", "bianji", "zhixian", "quxian", "juxing", "shouwei", "tuoyuan", "yuanji" }; for (int i = 0; i < arr.length; i++) { ImageIcon img = new ImageIcon("DrawImages/" + arr[i] + ".jpg"); but = new JButton(img); but.setActionCommand(arr[i]); but.setPreferredSize(new Dimension(24, 22)); but.addActionListener(tb); panel.add(but); } return panel; } /***********创建南面的颜色框*****************/ private JPanel creatSouth(){ JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(800,60)); JPanel panel01 = new JPanel(); panel.setLayout(null); but01 = new JButton(); but01.setBackground(Color.BLACK); but01.setBounds(70, 5, 50, 50); panel.add(but01); panel01.setPreferredSize(new Dimension(160,60)); //添加颜色按钮监听器 bl = new ButColorListener(but01); String[] arr01 = {"Color.black","Color.gray","Color.darkGray","Color.green","Color.LIGHT_GRAY","Color.magenta", "Color.orange","Color.pink","Color.red","Color.white"}; Color [] arr02={Color.black,Color.gray,Color.darkGray,Color.green,Color.LIGHT_GRAY,Color.magenta, Color.orange,Color.pink,Color.red,Color.white}; for(int i=0;i<arr01.length;i++){ JButton but = new JButton(); but.setBackground(arr02[i]); but.setActionCommand(arr01[i]); but.setPreferredSize(new Dimension(22, 22)); but.addActionListener(bl); panel01.add(but); } panel01.setBounds(110, 0, 160, 60); panel.add(panel01); return panel; } /*********创建中间的画布****************/ private JPanel creatCentral(){ JPanel panel = new JPanel(); panel.setBackground(Color.WHITE); g = panel.getGraphics(); return panel; } }
当你看到这些代码,可能已经晕了,不用急,让我一步一步解释一下,在刚开始的时候就是添加一些组件的代码,应该没有什么难以理解,如果无法理解就看注释吧,应该很清晰的,我想讲一下就是我将添加按钮写在一个方法里,如下部分代码:
/***********创建左边工具选择面板***********/ private JPanel creatWest(){ JPanel panel = new JPanel(); panel.setPreferredSize(new Dimension(70,500)); //创建一个监听器类 ToolButListener tb = new ToolButListener(biaozhi); String[] arr = { "duobianxing", "Xjuxing", "ca", "tianchong", "dianbi", "fangdajing", "huabi", "shuazi", "youqitong", "bianji", "zhixian", "quxian", "juxing", "shouwei", "tuoyuan", "yuanji" }; for (int i = 0; i < arr.length; i++) { ImageIcon img = new ImageIcon("DrawImages/" + arr[i] + ".jpg"); but = new JButton(img); but.setActionCommand(arr[i]); but.setPreferredSize(new Dimension(24, 22)); but.addActionListener(tb); panel.add(but); } return panel; }
通过for循环添加按钮,那么此时也必须实例化按钮监听器类,这样问题就来了,我们画图的时候,必须要在窗体可见后才能获画布对象 Graphics ,这样就会出现怎样将Graphics对象传到监听器类中,可以看如下代码:
//设置窗体可见 this.setVisible(true); g = panelCentral.getGraphics(); bl.setGraphics(g); ml.setGraphics(g); ml.setPanel(panelCentral);
通过在监听器类里设置set和get方法就能解决问题了,当然还能在监听器类实例化是传入,这种方法在这里不适合。接下类看一下监听器中的具体代码:
package Draw; import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JPanel; public class ButColorListener implements ActionListener{ private JButton but01; private Graphics g; public ButColorListener(JButton but01) { // TODO Auto-generated constructor stub this.but01=but01; } public void setGraphics(Graphics g){ this.g = g; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub // Graphics g = this.panelCentral.getGraphics(); if(e.getActionCommand().equals("Color.black")){ this.but01.setBackground(Color.BLACK); g.setColor(Color.BLACK); }if(e.getActionCommand().equals("Color.gray")){ this.but01.setBackground(Color.gray); g.setColor(Color.gray); }if(e.getActionCommand().equals("Color.darkGray")){ this.but01.setBackground(Color.darkGray); g.setColor(Color.darkGray); }if(e.getActionCommand().equals("Color.green")){ this.but01.setBackground(Color.green); g.setColor(Color.green); }if(e.getActionCommand().equals("Color.LIGHT_GRAY")){ this.but01.setBackground(Color.LIGHT_GRAY); g.setColor(Color.LIGHT_GRAY); }if(e.getActionCommand().equals("Color.magenta")){ this.but01.setBackground(Color.magenta); g.setColor(Color.magenta); }if(e.getActionCommand().equals("Color.orange")){ this.but01.setBackground(Color.orange); g.setColor(Color.orange); }if(e.getActionCommand().equals("Color.pink")){ this.but01.setBackground(Color.pink); g.setColor(Color.pink); }if(e.getActionCommand().equals("Color.red")){ this.but01.setBackground(Color.red); g.setColor(Color.red); }if(e.getActionCommand().equals("Color.white")){ this.but01.setBackground(Color.white); g.setColor(Color.white); } } }
从以上代码可以看看到如下:
public void setGraphics(Graphics g){ this.g = g; }
这样我们就获取了画布对象。那么我们就可调用这个g了;
以上就是通过set和get方法将对象传过去的,我们再将一下通过构造方法将对象传过去的方法,看如下代码:
FrameListener fl = new FrameListener(g); //将画布及鼠标监听器传过去 panelCentral.addMouseListener(fl); FrameMouseMotion mm= new FrameMouseMotion(g); panelCentral.addMouseMotionListener(mm);
我们直接通过构造函数将对象传过去了,那我们在看看实例化的对象的那边的代码:
package Draw; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class FrameListener implements MouseListener { private Graphics g; //定义鼠标的获取的点的坐标 // private int x1; // private int y1; public static int x1; public static int y1; private int x2; private int y2; public FrameListener(Graphics g) { this.g = g; } // public int getX1(){ // return this.x1; // } // public int gety1(){ // return this.y1; // } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub x1 = e.getX(); y1 = e.getY(); } @Override public void mouseReleased(MouseEvent e) { x2 = e.getX(); y2 = e.getY(); //具体画的方法 if(DrawBord.biaozhi.equals("zhixian")){ g.drawLine(x1, y1, x2, y2); }else if(DrawBord.biaozhi.equals("juxing")){ g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2)); }else if(DrawBord.biaozhi.equals("yuanji")){ g.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2),25,25); }else if(DrawBord.biaozhi.equals("tuoyuan")){ g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2)); } FrameMouseMotion.state=1; } }
就这样。我们也将画布对象传过去了。至此我们已将传对象的方法说了,下面继续发一下画图板的其他代码,一下的是画图板鼠标监听器类的代码:
package Draw; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class FrameListener implements MouseListener { private Graphics g; //定义鼠标的获取的点的坐标 // private int x1; // private int y1; public static int x1; public static int y1; private int x2; private int y2; public FrameListener(Graphics g) { this.g = g; } // public int getX1(){ // return this.x1; // } // public int gety1(){ // return this.y1; // } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub x1 = e.getX(); y1 = e.getY(); } @Override public void mouseReleased(MouseEvent e) { x2 = e.getX(); y2 = e.getY(); //具体画的方法 if(DrawBord.biaozhi.equals("zhixian")){ g.drawLine(x1, y1, x2, y2); }else if(DrawBord.biaozhi.equals("juxing")){ g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2)); }else if(DrawBord.biaozhi.equals("yuanji")){ g.drawRoundRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2),25,25); }else if(DrawBord.biaozhi.equals("tuoyuan")){ g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1-x2), Math.abs(y1-y2)); } FrameMouseMotion.state=1; } }
接下来是鼠标移动时的鼠标监听器类的代码:
package Draw; import java.awt.Color; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; public class FrameMouseMotion implements MouseMotionListener { //将画布对象传过来 private Graphics g; private int x1; private int y1; private int x2; private int y2; public static int state =1; //构造方法 public FrameMouseMotion(Graphics g) { // TODO Auto-generated constructor stub this.g = g; } @Override public void mouseDragged(MouseEvent e) { // TODO Auto-generated method stub x2 = e.getX(); y2 = e.getY(); if(DrawBord.biaozhi.equals("huabi")){ if(state==1){ g.drawLine(x2, y2, x2, y2); state =2; }else if(state==2){ g.drawLine(x2, y2, x1, y1); } }else if (DrawBord.biaozhi.equals("ca")) { g.setColor(Color.white); g.fillRect(x1, y1, 10, 20); g.setColor(Color.black); } else if (DrawBord.biaozhi.equals("shuazi")) { g.fillRect(x1, y1, 5, 5); } else if (DrawBord.biaozhi.equals("youqitong")) { for (int i = 0; i < 20; i++) { int j = (int) (Math.random() * 10); int k = (int) (Math.random() * 10); g.drawLine(x2+ j, y2 + k, x2 + j, y2 + k); } } x1 = x2; y1 = y2; } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub } }
然后就是菜单栏的监听器类了:
package Draw; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class MenuItemListener implements ActionListener { private Graphics g; private DrawBord drawBord; private JPanel panel; public MenuItemListener(DrawBord drawBord) { this.drawBord=drawBord; } public void setPanel(JPanel panel){ this.panel = panel; } public void setGraphics(Graphics g){ this.g =g; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("退出(X)")){ this.drawBord.setVisible(false); }else if(e.getActionCommand().equals("清除图像")){ this.panel.paint(g); }else if(e.getActionCommand().equals("颜色选择")){ JFrame frame = new JFrame(); frame.setTitle("颜色选择"); frame.setSize(300,300); JColorChooser color = new JColorChooser(); frame.add(color); frame.setDefaultCloseOperation(3); JButton but = new JButton("确定"); ColorConfirm cf = new ColorConfirm(frame,g,color); but.addActionListener(cf); but.setPreferredSize(new Dimension(100,20)); frame.add(but,BorderLayout.SOUTH); frame.setVisible(true); }else if(e.getActionCommand().equals("关于画图")){ JFrame frame = new JFrame(); frame.setTitle("声明"); AnounceConfirm ac = new AnounceConfirm(frame); String str= "haha"; JLabel label = new JLabel(str); JButton but = new JButton("确定"); but.addActionListener(ac); label.setPreferredSize(new Dimension(300,300)); frame.add(but,BorderLayout.SOUTH); frame.setSize(400,300); frame.setDefaultCloseOperation(3); frame.setVisible(true); } } }
接下来是菜单项的颜色选择器的代码
package Draw; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JColorChooser; import javax.swing.JFrame; public class ColorConfirm implements ActionListener { private JFrame frame; private Graphics g; private JColorChooser color; public ColorConfirm(JFrame frame, Graphics g, JColorChooser color) { // TODO Auto-generated constructor stub this.frame = frame; this.g = g; this.color = color; } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub g.setColor(this.color.getColor()); this.frame.setVisible(false); } }
接下来是工具栏的监听器:
package Draw; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ToolButListener implements ActionListener { //添加一个标志 private String biaozhi; public ToolButListener(String biaozhi) { // TODO Auto-generated constructor stub this.biaozhi =biaozhi; } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals("duobianxing")){ DrawBord.biaozhi = "duobianxing"; }else if(e.getActionCommand().equals("Xjuxing")){ DrawBord.biaozhi = "Xjuxing"; }else if(e.getActionCommand().equals("ca")){ DrawBord.biaozhi = "ca"; }else if(e.getActionCommand().equals("tianchong")){ DrawBord.biaozhi = "tianchong"; }else if(e.getActionCommand().equals("dianbi")){ DrawBord.biaozhi = "dianbi"; }else if(e.getActionCommand().equals("fangdajing")){ DrawBord.biaozhi = "fangdajing"; }else if(e.getActionCommand().equals("huabi")){ DrawBord.biaozhi = "huabi"; }else if(e.getActionCommand().equals("shuazi")){ DrawBord.biaozhi = "shuazi"; }else if(e.getActionCommand().equals("youqitong")){ DrawBord.biaozhi = "youqitong"; }else if(e.getActionCommand().equals("bianji")){ DrawBord.biaozhi = "bianji"; }else if(e.getActionCommand().equals("zhixian")){ DrawBord.biaozhi = "zhixian"; }else if(e.getActionCommand().equals("quxian")){ DrawBord.biaozhi = "quxian"; }else if(e.getActionCommand().equals("juxing")){ DrawBord.biaozhi = "juxing"; }else if(e.getActionCommand().equals("shouwei")){ DrawBord.biaozhi = "shouwei"; }else if(e.getActionCommand().equals("tuoyuan")){ DrawBord.biaozhi = "tuoyuan"; }else if(e.getActionCommand().equals("yuanji")){ DrawBord.biaozhi = "yuanji"; } } }
最后就是整个程序运行的manager类了:
package Draw; //设置主程序入口类 public class Manager { public static void main(String[]args){ DrawBord draw = new DrawBord(); draw.showUI(); } }
通过以上代码,我们能写出一个画图板,但是你可能回觉的太过麻烦了,接下来请看一下我通过内部类写的仿XP画图板,功能一样,,但是就用了一个类就搞定了:
package netjava0626; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseMotionListener; //定义一个画板类 public class DrawingBoard { private String biaozhi = "huabi"; private String color; private Graphics g; private int x1, y1, x2, y2, x3, y3, w, h; private JLabel lab; // 初始化面板并实现组建 public void show() { JFrame frame = new JFrame(); // 创建一个工具栏 JButton but01 = new JButton("文件(F)"); JButton but02 = new JButton("编辑(E)"); JButton but03 = new JButton("查看(V)"); JButton but04 = new JButton("图像(I)"); JButton but05 = new JButton("颜色(C)"); JButton but06 = new JButton("帮助(H)"); JToolBar bar = new JToolBar(); bar.add(but01); bar.add(but02); bar.add(but03); bar.add(but04); bar.add(but05); bar.add(but06); frame.add(bar, BorderLayout.NORTH); //创建颜色框的监听器 ActionListener a3 = new ActionListener(){ public void actionPerformed(ActionEvent e) { //设置颜色 color = e.getActionCommand(); if(color.equals("Color.black") ){ g.setColor(Color.black); lab.setBackground(Color.black); }else if(color.equals("Color.gray")){ g.setColor(Color.gray); lab.setBackground(Color.gray); }else if(color.equals("Color.darkGray")){ g.setColor(Color.darkGray); lab.setBackground(Color.darkGray); }else if(color.equals("Color.green")){ g.setColor(Color.green); lab.setBackground(Color.green); }else if(color.equals("Color.LIGHT_GRAY")){ g.setColor(Color.LIGHT_GRAY); lab.setBackground(Color.LIGHT_GRAY); }else if(color.equals("Color.magenta")){ g.setColor(Color.magenta); lab.setBackground(Color.magenta); }else if(color.equals("Color.orange")){ g.setColor(Color.orange); lab.setBackground(Color.orange); }else if(color.equals("Color.pink")){ g.setColor(Color.pink); lab.setBackground(Color.pink); }else if(color.equals("Color.red")){ g.setColor(Color.red); lab.setBackground(Color.red); }else if(color.equals("Color.white")){ g.setColor(Color.white); lab.setBackground(Color.white); } } }; // 创建监听器 ActionListener al = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { biaozhi = e.getActionCommand(); } }; // 创建工具面板 JPanel panel01 = new JPanel(); panel01.setPreferredSize(new Dimension(60, 104)); String[] arr = { "duobianxing", "Xjuxing", "ca", "tianchong", "dianbi", "fangdajing", "huabi", "shuazi", "youqitong", "bianji", "zhixian", "quxian", "juxing", "shouwei", "tuoyuan", "yuanji" }; for (int i = 0; i < arr.length; i++) { ImageIcon img = new ImageIcon("DrawImages/" + arr[i] + ".jpg"); JButton but = new JButton(img); but.setActionCommand(arr[i]); but.addActionListener(al); but.setPreferredSize(new Dimension(24, 22)); panel01.add(but); } frame.add(panel01, BorderLayout.WEST); // 颜色选项栏 JPanel panel02 = new JPanel(); panel02.setPreferredSize(new Dimension(100, 50)); panel02.setLayout(null); JPanel panel04 = new JPanel(); panel04.setPreferredSize(new Dimension(100, 40)); // panel04.setBackground(Color.white ); panel04.setLayout(new GridLayout(2, 5)); Color [] arr02={Color.black,Color.gray,Color.darkGray,Color.green,Color.LIGHT_GRAY,Color.magenta, Color.orange,Color.pink,Color.red,Color.white}; String[] arr01 = {"Color.black","Color.gray","Color.darkGray","Color.green","Color.LIGHT_GRAY","Color.magenta", "Color.orange","Color.pink","Color.red","Color.white"}; lab = new JLabel(); lab.setPreferredSize(new Dimension()); lab.setOpaque(true); lab.setBackground(Color.black); lab.setBounds(60, 0, 47, 50); for (int i = 0; i < arr02.length; i++) { JButton but = new JButton(); but.setBackground(arr02[i]); but.setActionCommand(arr01[i]); but.setPreferredSize(new Dimension(16, 16)); but.addActionListener(a3); panel04.add(but); } panel02.add(lab); panel04.setBounds(107, 0, 100, 50); panel02.add(panel04); frame.add(panel02, BorderLayout.SOUTH); // 画布 JPanel panel03 = new JPanel(); panel03.setPreferredSize(new Dimension(694, 400)); panel03.setBackground(Color.WHITE); frame.add(panel03, BorderLayout.CENTER); frame.setTitle("画图板"); frame.setSize(754, 508); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(3); MouseListener a1 = new MouseAdapter() { // 鼠標按下時獲得坐标 public void mousePressed(MouseEvent e) { x1 = e.getX(); y1 = e.getY(); } // 鼠标松开时获取坐标 public void mouseReleased(MouseEvent e) { x2 = e.getX(); y2 = e.getY(); if (biaozhi.equals("shouwei")) { g.drawLine(x1, y1, x1, y1); } else if (biaozhi.equals("dianbi")) { } else if (biaozhi.equals("zhixian")) { g.drawLine(x1, y1, x2, y2); } else if (biaozhi.equals("juxing")) { if (x2 > x1 && y2 > y1) { x3 = x1; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRect(x3, y3, w, h); } else if (x2 > x1 && y2 < y1) { x3 = x1; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRect(x3, y3, w, h); } else if (x2 < x1 && y2 < y1) { x3 = x2; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRect(x3, y3, w, h); } else { x3 = x2; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRect(x3, y3, w, h); } } else if (biaozhi.equals("tuoyuan")) { if (x2 > x1 && y2 > y1) { x3 = x1; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawOval(x3, y3, w, h); } else if (x2 > x1 && y2 < y1) { x3 = x1; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawOval(x3, y3, w, h); } else if (x2 < x1 && y2 < y1) { x3 = x2; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawOval(x3, y3, w, h); } else { x3 = x2; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawOval(x3, y3, w, h); } } else if (biaozhi.equals("yuanji")) { if (x2 > x1 && y2 > y1) { x3 = x1; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRoundRect(x3, y3, w, h, 20, 20); } else if (x2 > x1 && y2 < y1) { x3 = x1; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRoundRect(x3, y3, w, h, 20, 20); } else if (x2 < x1 && y2 < y1) { x3 = x2; y3 = y2; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRoundRect(x3, y3, w, h, 20, 20); } else { x3 = x2; y3 = y1; w = Math.abs(x2 - x1); h = Math.abs(y2 - y1); g.drawRoundRect(x3, y3, w, h, 20, 20); } } } }; // 添加拖動時的方法 MouseMotionListener a2 = new MouseMotionAdapter() { public void mouseDragged(MouseEvent e) { x3 = e.getX(); y3 = e.getY(); // 当鼠标在拖动时也要注意对坐标的赋值 if (biaozhi.equals("huabi")) { g.drawLine(x1, y1, x3, y3); x1 = x3; y1 = y3; } else if (biaozhi.equals("ca")) { g.setColor(Color.white); g.fillRect(x1, y1, 10, 20); x1 = x3; y1 = y3; g.setColor(Color.black); } else if (biaozhi.equals("shuazi")) { g.fillRect(x1, y1, 5, 5); x1 = x3; y1 = y3; } else if (biaozhi.equals("youqitong")) { for (int i = 0; i < 20; i++) { int j = (int) (Math.random() * 10); int k = (int) (Math.random() * 10); g.drawLine(x3 + j, y3 + k, x3 + j, y3 + k); } } } }; panel03.addMouseListener(a1); panel03.addMouseMotionListener(a2); frame.setVisible(true); g = panel03.getGraphics(); } public static void main(String[] args) { DrawingBoard draw = new DrawingBoard(); draw.show(); } }
至此我们总结一下:
传值方法:1.通过set和get方法传值;
2.通过构造函数传值;
内部类和外部类的优劣:
1.使用内部类可以很轻松的写,但是这个框架是很不清晰的,我们要知道以后做项目的时候并不是一个人完成的,而是整个团队合作写的。
2.使用外部类可能会很清晰,但是传值问题真的很是蛋痛,但是我们看可以很清晰,以后写代码是给别人看的,不是只有你自己的。
你可能会问,那么我们是否就不适用内部类了呢,这个问题我将在下一篇博客写到,到时候会结合一下贪吃蛇项目具体讲解一下的。
相关推荐
1、文件内容:ibus-table-chinese-erbi-1.4.6-3.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/ibus-table-chinese-erbi-1.4.6-3.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、更多资源/技术支持:公众号禅静编程坊
选择Java后台技术和MySQL数据库,在前台界面为提升用户体验,使用Jquery、Ajax、CSS等技术进行布局。 系统包括两类用户:学生、管理员。 学生用户只要实现了前台信息的查看,打开首页,查看网站介绍、自习室信息、在线留言、轮播图信息公告等,通过点击首页的菜单跳转到对应的功能页面菜单,包括网站首页、自习室信息、注册登录、个人中心、后台登录。 学生用户通过账户账号登录,登录后具有所有的操作权限,如果没有登录,不能在线预约。学生用户退出系统将注销个人的登录信息。 管理员通过后台的登录页面,选择管理员权限后进行登录,管理员的权限包括轮播公告管理、老师学生信息管理和信息审核管理,管理员管理后点击退出,注销登录信息。 管理员用户具有在线交流的管理,自习室信息管理、自习室预约管理。 在线交流是对前台用户留言内容进行管理,删除留言信息,查看留言信息。
面向基层就业个性化大学生服务平台(源码+数据库+论文+ppt)java开发springboot框架javaweb,可做计算机毕业设计或课程设计 【功能需求】 面向基层就业个性化大学生服务平台(源码+数据库+论文+ppt)java开发springboot框架javaweb,可做计算机毕业设计或课程设计 面向基层就业个性化大学生服务平台中的管理员角色主要负责了如下功能操作。 (1)职业分类管理功能需求:对职业进行划分分类管理等。 (2)用户管理功能需求:对用户信息进行维护管理等。 (3)职业信息管理功能需求:对职业信息进行发布等。 (4)问卷信息管理功能需求:可以发布学生的问卷调查操作。 (5)个性化测试管理功能需求:可以发布个性化测试试题。 (6)试题管理功能需求:对测试试题进行增删改查操作。 (7)社区交流管理功能需求:对用户的交流论坛信息进行维护管理。 面向基层就业个性化大学生服务平台中的用户角色主要负责了如下功能操作。 (1)注册登录功能需求:没有账号的用户,可以输入账号,密码,昵称,邮箱等信息进行注册操作,注册后可以输入账号和密码进行登录。 (2)职业信息功能需求:用户可以对职业信息进行查看。 (3)问卷信息功能需求:可以在线进行问卷调查答卷操作。 (4)社区交流功能需求:可以在线进行社区交流。 (5)个性化测试功能需求:可以在线进行个性化测试。 (6)公告资讯功能需求:可以查看浏览系统发布的公告资讯信息。 【环境需要】 1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。 3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.数据库:MySql 5.7/8.0等版本均可; 【购买须知】 本源码项目经过严格的调试,项目已确保无误,可直接用于课程实训或毕业设计提交。里面都有配套的运行环境软件,讲解视频,部署视频教程,一应俱全,可以自己按照教程导入运行。附有论文参考,使学习者能够快速掌握系统设计和实现的核心技术。
三菱Fx3u程序:自动检测包装机电机控制模板,PLC脉冲与伺服定位,手自动切换功能,三菱Fx3u程序:自动检测包装机电机控制模板——涵盖伺服定位与手自动切换功能,三菱Fx3u程序,自动检测包装机。 该程序六个电机,plc本体脉冲控制3个轴,3个1pg控制。 程序内包括伺服定位,手自动切,功能快的使用,可作为模板程序,很适合新手。 ,三菱Fx3u程序; 自动检测包装机; 六个电机; PLC脉冲控制; 伺服定位; 手自动切换; 功能快捷键; 模板程序。,三菱Fx3u PLC控制下的自动包装机程序:六电机伺服定位与手自动切换模板程序
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
计及信息间隙决策与多能转换的综合能源系统优化调度模型:实现碳经济最大化与源荷不确定性考量,基于信息间隙决策与多能转换的综合能源系统优化调度模型:源荷不确定性下的高效碳经济调度策略,计及信息间隙决策及多能转的综合能源系统优化调度 本代码构建了含风电、光伏、光热发电系统、燃气轮机、燃气锅炉、电锅炉、储气、储电、储碳、碳捕集装置的综合能源系统优化调度模型,并考虑P2G装置与碳捕集装置联合运行,从而实现碳经济的最大化,最重要的是本文引入了信息间隙决策理论考虑了源荷的不确定性(本代码的重点)与店铺的47代码形成鲜明的对比,注意擦亮眼睛,认准原创,该代码非常适合修改创新,,提供相关的模型资料 ,计及信息间隙决策; 综合能源系统; 优化调度; 多能转换; 碳经济最大化; 风电; 光伏; 燃气轮机; 储气; 储电; 储碳; 碳捕集装置; P2G装置联合运行; 模型资料,综合能源系统优化调度模型:基于信息间隙决策和多能转换的原创方案
IPG QCW激光模块电源驱动电路设计与实现:包含安全回路、紧急放电回路及光纤互锁功能的多版本原理图解析,IPG QCW激光模块电源驱动电路设计与实现:含安全回路、紧急放电及光纤互锁等多重保护功能的原理图解析,IPG QCW激光模块电源驱动电路, 包含安全回路,紧急放电回路,光纤互锁回路等, 元件参数请根据实际设计适当调整,此电路仅供参考,不提供pcb文件 原理图提供PDF和KICAD两个版本。 ,IPG激光模块; QCW激光电源驱动; 安全回路; 紧急放电回路; 光纤互锁回路; 原理图PDF和KICAD版本。,IPG激光模块电源驱动电路图解:含安全与紧急放电回路
基于LSSVM的短期电力负荷预测模型及其性能评估:结果揭露精确度与误差分析,LSSVM在短期电力负荷预测中的结果分析:基于均方根误差、平均绝对误差及平均相对百分误差的评估。,LSSVM最小二乘支持向量机做短期电力负荷预测。 结果分析 均方根误差(RMSE):0.79172 平均绝对误差(MAE):0.4871 平均相对百分误差(MAPE):13.079% ,LSSVM(最小二乘支持向量机);短期电力负荷预测;均方根误差(RMSE);平均绝对误差(MAE);平均相对百分误差(MAPE),LSSVM在电力负荷短期预测中的应用及性能分析
1、文件内容:libmtp-examples-1.1.14-1.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/libmtp-examples-1.1.14-1.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、更多资源/技术支持:公众号禅静编程坊
资源内项目源码是均来自个人的课程设计、毕业设计或者具体项目,代码都测试ok,都是运行成功后才上传资源,答辩评审绝对信服的,拿来就能用。放心下载使用!源码、说明、论文、数据集一站式服务,拿来就能用的绝对好资源!!! 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、大作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 4、如有侵权请私信博主,感谢支持
2023-04-06-项目笔记-第四百一十六阶段-课前小分享_小分享1.坚持提交gitee 小分享2.作业中提交代码 小分享3.写代码注意代码风格 4.3.1变量的使用 4.4变量的作用域与生命周期 4.4.1局部变量的作用域 4.4.2全局变量的作用域 4.4.2.1全局变量的作用域_1 4.4.2.414局变量的作用域_414- 2025-02-21
MINIST数据集和春风机器学习框架
1、文件内容:ibus-table-chinese-wu-1.4.6-3.el7.rpm以及相关依赖 2、文件形式:tar.gz压缩包 3、安装指令: #Step1、解压 tar -zxvf /mnt/data/output/ibus-table-chinese-wu-1.4.6-3.el7.tar.gz #Step2、进入解压后的目录,执行安装 sudo rpm -ivh *.rpm 4、更多资源/技术支持:公众号禅静编程坊
宿舍管理系统(源码+数据库+论文+ppt)java开发springboot框架javaweb,可做计算机毕业设计或课程设计 【功能需求】 系统拥有管理员和学生两个角色,主要具备系统首页、个人中心、学生管理、宿舍信息管理、宿舍分配管理、水电费管理、进入宿舍管理、出入宿舍管理、维修信息管理、卫生信息管理、考勤信息管理、留言板、交流论坛、系统管理等功能模块。 【环境需要】 1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。 3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.数据库:MySql 5.7/8.0等版本均可; 【购买须知】 本源码项目经过严格的调试,项目已确保无误,可直接用于课程实训或毕业设计提交。里面都有配套的运行环境软件,讲解视频,部署视频教程,一应俱全,可以自己按照教程导入运行。附有论文参考,使学习者能够快速掌握系统设计和实现的核心技术。
1.版本:matlab2014/2019a/2024a 2.附赠案例数据可直接运行matlab程序。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。
人凤飞飞凤飞飞是粉色丰富
2024蓝桥杯嵌入式学习资料
image_download_1740129191509.jpg
基于Multisim仿真的带优先病房呼叫系统设计(仿真图) 设计一个病房呼叫系统。 功能 (1)当有病人紧急呼叫时,产生声,光提示,并显示病人的编号; (2)根据病人的病情设计优先级别,当有多人呼叫时,病情严重者优先; (3)医护人员处理完当前最高级别的呼叫后,系统按优先级别显示其他呼叫病人的病号。
基于STM32F103的3.6kW全桥逆变器资料:并网充电放电、智能切换与全方位保护方案,基于STM32F103的3.6kW全桥逆变器资料:并网充电放电、智能控制与全方位保护方案,逆变器光伏逆变器,3.6kw储能逆变器全套资料 STM32储能逆变器 BOOST 全桥 基于STM32F103设计,具有并网充电、放电;并网离网自动切;485通讯,在线升级;风扇智能控制,提供过流、过压、短路、过温等全方位保护。 基于arm的方案区别于dsp。 有PCB、原理图及代码ad文件。 ,逆变器; 储能逆变器; STM32F103; 3.6kw; 485通讯; 全方位保护; 智能控制; 方案区别; PCB文件; 原理图文件; ad文件。,基于STM32F103的3.6kw储能逆变器:全方位保护与智能控制