标签:lis ref custom ini rar mit import explore 背景色
知识点:
Swing组件:
Swing事件监听:
Java技术:
1.创建项目和导入资源
在Package Explorer的Other Projects,右键-New-Other-Java Project-next-(输入项目名:一起来画画)Finish;
在项目名上,右键-New-Folder-(选择文件夹:一起来画画,在Folder name输入:lib)Finish;
将外部包DrawUtil.mr.jar复制到lib里,然后在项目名上,右键-Build Path-Configure Build Path-Libraries-Add JARs...-选中"一起来画画/lib/DrawUtil.mr.jar"-OK;
2.编码
1 package com.mr.draw; 2 3 import javax.swing.JFrame; 4 5 import java.awt.Color; 6 import java.awt.Graphics; 7 import java.awt.Graphics2D; 8 import java.awt.image.BufferedImage; 9 10 import java.awt.event.MouseAdapter; 11 import java.awt.event.MouseEvent; 12 import java.awt.event.MouseMotionAdapter; 13 14 import java.awt.BorderLayout; 15 import javax.swing.ButtonGroup; 16 import javax.swing.JButton;//按钮类 17 import javax.swing.JToggleButton;///画线类 18 import javax.swing.JToolBar;//工具栏类 19 20 import java.awt.BasicStroke; 21 import java.awt.event.ActionEvent; 22 import java.awt.event.ActionListener;//调整画笔粗细 23 24 import javax.swing.JColorChooser;//实现添加颜色功能 25 26 import java.awt.geom.Ellipse2D; 27 import java.awt.geom.Rectangle2D; 28 import com.mr.util.FrameGetShape; 29 import com.mr.util.ShapeWindow; 30 import com.mr.util.Shapes; ///实现绘制图形功能 31 32 import com.mr.util.DrawImageUtil;//实现图片保存功能 33 34 import javax.swing.JMenu; 35 import javax.swing.JMenuBar; 36 import javax.swing.JMenuItem;//实现 添加菜单栏 37 38 import java.awt.AlphaComposite; 39 import java.awt.Font; 40 import javax.swing.JOptionPane;//实现添加水印,为自己作品署名 41 42 import java.awt.Image; 43 import java.awt.Point; 44 import java.awt.Toolkit;//工具箱 45 import java.awt.Cursor;//光标,实现更改鼠标图标功能 46 import javax.swing.ImageIcon; 47 48 //画图主窗体 49 public class DrawPictureFrame extends JFrame implements FrameGetShape{//继承窗体类,实现绘制图形的接口 50 //创建一个8位BGR颜色分量的图像 51 BufferedImage image = new BufferedImage(570,390,BufferedImage.TYPE_3BYTE_BGR); 52 Graphics gs = image.getGraphics(); 53 Graphics2D g = (Graphics2D)gs; 54 DrawPictureCanvas canvas = new DrawPictureCanvas(); //创建画布对象 55 Color foreColor = Color.red; //前景色 56 Color backgroundColor = Color.green; //背景色 57 58 int x=-1; 59 int y=-1;//上一次鼠标绘制点的横纵坐标 60 boolean rubber=false;//橡皮识别变量 61 62 private JToolBar toolBar;//工具栏目 63 private JButton eraserButton; 64 private JToggleButton strokeButton1; 65 private JToggleButton strokeButton2; 66 private JToggleButton strokeButton3;//细中粗按钮 67 private JButton backgroundButton; 68 private JButton foregroundButton; 69 private JButton clearButton; 70 private JButton saveButton; 71 private JButton shapeButton;//图形按钮 72 boolean drawShape = false; 73 Shapes shape; 74 private JMenuItem strokeMenuItem1; 75 private JMenuItem strokeMenuItem2; 76 private JMenuItem strokeMenuItem3; 77 private JMenuItem clearMenuItem; 78 private JMenuItem foregroundMenuItem; 79 private JMenuItem backgroundMenuItem; 80 private JMenuItem eraserMenuItem; 81 private JMenuItem exitMenuItem;//退出菜单 82 private JMenuItem saveMenuItem; 83 private JMenuItem shuiyinMenuItem;//水印菜单 84 private String shuiyin = "";//水印字符内容 85 private PictureWindow picWindow;//简笔画展示窗体 86 private JButton showPicButton;//展开简笔画按钮 87 88 89 public DrawPictureFrame() 90 { 91 setResizable(false); // 窗体大小不能改变 92 //setTitle("画图程序"); 93 setTitle("画图程序(水印内容:[" + shuiyin + "] )" ); 94 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 窗体关闭则立即停止程序 95 setBounds(500, 100, 574, 460); // 设置窗口的位置和宽高 96 97 init();//组件初始化 98 addListener();//添加组件监听 99 } 100 101 private void init() 102 { 103 g.setColor(backgroundColor);//用前景色设置绘图对象的颜色 104 g.fillRect(0, 0, 570, 390);//用背景色填充整个画布,第一个0和570表示位置从0开始到57 105 g.setColor(foreColor);//用前景色设置绘图对象的颜色 106 canvas.setImage(image);//设置画布的图像 107 getContentPane().add(canvas);//将画布添加到窗体容器默认布局中的中部位置 108 109 toolBar = new JToolBar();//初始化工具栏 110 toolBar.setFloatable(false);//工具栏固定 111 getContentPane().add(toolBar,BorderLayout.NORTH);//将工具栏添加到窗体最北位置 112 113 //showPicButton = new JButton("展开简笔画"); 114 showPicButton = new JButton(); 115 showPicButton.setToolTipText("展开简笔画"); 116 showPicButton.setIcon( new ImageIcon("src/img/icon/展开.png") );//需要导入ImageIcon 117 toolBar.add(showPicButton); 118 119 saveButton = new JButton("保存"); 120 toolBar.add(saveButton); 121 toolBar.addSeparator();//添加工具栏 122 strokeButton1 = new JToggleButton("细线"); 123 strokeButton1.setSelected(true);//细线按钮处于被选中状态 124 toolBar.add(strokeButton1); 125 126 strokeButton2 = new JToggleButton("粗线"); 127 toolBar.add(strokeButton2); 128 129 strokeButton3 = new JToggleButton("较粗"); 130 toolBar.add(strokeButton3); 131 toolBar.addSeparator(); 132 ButtonGroup strokeGroup = new ButtonGroup();//画笔粗细按钮组,保证同时只有一个被选中 133 strokeGroup.add(strokeButton1); 134 strokeGroup.add(strokeButton2); 135 strokeGroup.add(strokeButton3); 136 137 backgroundButton = new JButton("背景颜色"); 138 toolBar.add(backgroundButton); 139 foregroundButton = new JButton("前景颜色"); 140 toolBar.add(foregroundButton); 141 toolBar.addSeparator(); 142 143 shapeButton = new JButton("图形"); 144 toolBar.add(shapeButton); 145 clearButton = new JButton("清除"); 146 toolBar.add(clearButton); 147 eraserButton = new JButton("橡皮"); 148 toolBar.add(eraserButton); 149 toolBar.addSeparator(); 150 151 JMenuBar menuBar = new JMenuBar(); 152 setJMenuBar(menuBar); 153 154 JMenu systemMenu = new JMenu("系统"); 155 menuBar.add(systemMenu); 156 shuiyinMenuItem = new JMenuItem("设置水印"); 157 systemMenu.add(shuiyinMenuItem); 158 saveMenuItem = new JMenuItem("保存"); 159 systemMenu.add(saveMenuItem); 160 systemMenu.addSeparator(); 161 exitMenuItem = new JMenuItem("退出"); 162 systemMenu.add(exitMenuItem); 163 JMenu strokeMenu = new JMenu("线型"); 164 menuBar.add(strokeMenu); 165 strokeMenuItem1 = new JMenuItem("细线"); 166 strokeMenu.add(strokeMenuItem1); 167 strokeMenuItem2 = new JMenuItem("粗线"); 168 strokeMenu.add(strokeMenuItem2); 169 strokeMenuItem3 = new JMenuItem("较线"); 170 strokeMenu.add(strokeMenuItem3); 171 172 JMenu colorMenu = new JMenu("颜色"); 173 menuBar.add(colorMenu); 174 foregroundMenuItem = new JMenuItem("前景颜色"); 175 colorMenu.add(foregroundMenuItem); 176 backgroundMenuItem = new JMenuItem("背景颜色"); 177 colorMenu.add(backgroundMenuItem); 178 179 JMenu editMenu = new JMenu("编辑"); 180 menuBar.add(editMenu); 181 clearMenuItem = new JMenuItem("清除"); 182 editMenu.add(clearMenuItem); 183 eraserMenuItem = new JMenuItem("橡皮"); 184 editMenu.add(eraserMenuItem); 185 186 picWindow = new PictureWindow(DrawPictureFrame.this); 187 } 188 189 //动作监听 190 private void addListener() 191 { 192 canvas.addMouseMotionListener(new MouseMotionAdapter(){ 193 194 public void mouseDragged( final MouseEvent e )//鼠标拖曳监听 195 { 196 if(x>0 && y>0)//如果x和y存在鼠标记录 197 { 198 if(rubber)//如果是橡皮则画出背景色,相当于擦掉前景色 199 { 200 g.setColor(backgroundColor); 201 g.fillRect(x, y, 10, 10); 202 } 203 else //否则画出 204 g.drawLine(x,y,e.getX(),e.getY()); 205 } 206 x=e.getX(); 207 y=e.getY();//上一次鼠标绘制点的横纵坐标 208 canvas.repaint();//更新画布 209 } 210 public void mouseMoved(final MouseEvent arg0) { 211 if(rubber)//如果使用橡皮 212 { 213 Toolkit kit = Toolkit.getDefaultToolkit();//获得系统默认的组件工具包 214 Image img = kit.createImage("src/img/icon/鼠标橡皮.png");//利用工具包获取图片 215 Cursor c = kit.createCustomCursor(img,new Point(0,0),"clear");//参数为图片,光标热点和光标描述的字符串 216 setCursor(c);//使用自定义光标 217 } 218 else 219 setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));//不是橡皮则是画笔,则设置光标为十字形 220 } 221 }); 222 canvas.addMouseListener(new MouseAdapter() {//鼠标点击监听 223 public void mouseReleased( final MouseEvent arg0 )//按键抬起时 224 { 225 x=-1; 226 y=-1;//将上一次鼠标绘制点的横纵坐标恢复成-1 227 //改成x=11,y=11??? 228 } 229 230 public void mousePressed(MouseEvent e)//当按键按下时 231 { 232 if( drawShape )//如果此时鼠标画的是 233 { 234 switch( shape.getType() )//判断图形种类 235 { 236 case Shapes.YUAN: 237 int yuanX = e.getX()-shape.getWidth()/2; 238 int yuanY = e.getY()-shape.getHeigth()/2; 239 /* 240 System.out.println(e.getX()+" "+e.getY()); 241 System.out.println(shape.getWidth()/2+" "+shape.getHeigth()/2); 242 System.out.println(yuanX+" "+yuanY);*/ 243 //创建圆形图形,根据长宽可能是椭圆,指定坐标和宽高 244 Ellipse2D yuan = new Ellipse2D.Double(yuanX,yuanY,shape.getWidth(),shape.getHeigth()); 245 g.draw(yuan); 246 break; 247 case Shapes.FANG: 248 int fangX=e.getX()-shape.getWidth()/2; 249 int fangY=e.getY()-shape.getHeigth()/2; 250 Rectangle2D fang = new Rectangle2D.Double(fangX,fangY,shape.getWidth(),shape.getHeigth()); 251 g.draw(fang); 252 break; 253 } 254 canvas.repaint(); 255 drawShape = false;//画一个就不能画了 256 } 257 } 258 }); 259 260 toolBar.addMouseMotionListener(new MouseMotionAdapter() {//工具栏添加鼠标移动监听 261 public void mouseMoved(final MouseEvent arg0) {//鼠标移动时,设置鼠标指针的形状为默认光标 262 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 263 } 264 }); 265 266 //调整画笔粗细 267 strokeButton1.addActionListener(new ActionListener() {//细线按钮添加监听 268 public void actionPerformed(final ActionEvent arg0)//单击时 269 { //声明画笔的属性,粗细为1像素,线条末端无修饰,折线处呈尖角 270 BasicStroke bs = new BasicStroke(1, BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER); 271 g.setStroke(bs);//画图工具使用此笔 272 } 273 }); 274 strokeButton2.addActionListener(new ActionListener() {//中线按钮添加监听 275 public void actionPerformed(final ActionEvent arg0)//单击时 276 { //声明画笔的属性,中线为2像素,线条末端无修饰,折线处呈尖角 277 BasicStroke bs = new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER); 278 g.setStroke(bs);//画图工具使用此笔 279 } 280 281 }); 282 strokeButton3.addActionListener(new ActionListener() {//粗线按钮添加监听 283 public void actionPerformed(final ActionEvent arg0)//单击时 284 { //声明画笔的属性,粗线为4像素,线条末端无修饰,折线处呈尖角 285 BasicStroke bs = new BasicStroke(4,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER); 286 g.setStroke(bs);//画图工具使用此笔 287 } 288 }); 289 290 //实现颜色添加功能 291 backgroundButton.addActionListener( new ActionListener() { 292 public void actionPerformed( final ActionEvent arg0 ) 293 { //打开选择颜色对话框,参数依次是:符窗体、标题、默认选中的颜色(青色) 294 Color bgColor = JColorChooser.showDialog(DrawPictureFrame.this, "选择颜色对话框", Color.CYAN); 295 if ( bgColor !=null )///选中的颜色不是空的 296 backgroundColor = bgColor;//则将选中的颜色赋给背景色 297 backgroundButton.setBackground(backgroundColor); 298 g.setColor(backgroundColor); 299 g.fillRect(0, 0, 570, 390);//画一个背景颜色的方形填满整个画布 300 g.setColor(foreColor); 301 canvas.repaint();//更新画布 302 } 303 }); 304 foregroundButton.addActionListener(new ActionListener() { 305 public void actionPerformed(final ActionEvent arg0) 306 { 307 Color fColor = JColorChooser.showDialog(DrawPictureFrame.this, "选择颜色对话框", Color.CYAN); 308 if(fColor != null) 309 foreColor = fColor; 310 foregroundButton.setForeground(foreColor); 311 g.setColor(foreColor); 312 } 313 314 }); 315 316 //清除按钮和橡皮按钮 317 clearButton.addActionListener(new ActionListener() { 318 public void actionPerformed( final ActionEvent arg0 ) { 319 g.setColor(backgroundColor); 320 g.fillRect(0, 0, 570, 390);//用背景色填充以达到清除效果,然后画笔换成前景色,继续作画 321 g.setColor(foreColor); 322 canvas.repaint();//更新画布 323 } 324 }); 325 eraserButton.addActionListener(new ActionListener() { 326 public void actionPerformed(final ActionEvent arg0) 327 { 328 if( eraserButton.getText().equals("橡皮") ) 329 { 330 rubber = true; 331 eraserButton.setText("画图"); 332 } 333 else 334 { 335 rubber = false; 336 eraserButton.setText("橡皮"); 337 g.setColor(foreColor); 338 339 } 340 } 341 }); 342 343 344 //图形绘制按钮 345 shapeButton.addActionListener(new ActionListener() { 346 public void actionPerformed(ActionEvent e)//单击时 347 { 348 //创建图形选择组件 349 ShapeWindow shapeWindow = new ShapeWindow(DrawPictureFrame.this); 350 int shapeButtonWidth = shapeButton.getWidth(); 351 int shapeWindowWidth = shapeWindow.getWidth(); 352 int shapeButtonX = shapeButton.getX(); 353 int shapeButtonY = shapeButton.getY(); 354 int shapeWindowX = getX() - shapeButtonX - ( shapeWindowWidth-shapeButtonWidth )/2; 355 int shapeWindowY = getY() - shapeButtonY + 80; 356 shapeWindow.setLocation(shapeWindowX,shapeWindowY); 357 shapeWindow.setVisible(true);//图形组件可见,如果是false点击图形啥都没有 358 } 359 360 }); 361 362 //保存按钮实现保存功能 363 saveButton.addActionListener(new ActionListener() { 364 public void actionPerformed(final ActionEvent arg0) {//单击时 365 addWatermark();//添加水印 366 DrawImageUtil.saveImage(DrawPictureFrame.this,image );//打印图片 367 } 368 369 }); 370 371 exitMenuItem.addActionListener(new ActionListener() {///退出菜单动作监听 372 public void actionPerformed(final ActionEvent e)///单击时程序退出 373 { 374 System.exit(0); 375 } 376 }); 377 378 eraserMenuItem.addActionListener(new ActionListener() { 379 public void actionPerformed(final ActionEvent e) { 380 if( eraserMenuItem.getText().contentEquals("橡皮") ) { 381 rubber = true; 382 eraserMenuItem.setText("画图"); 383 eraserButton.setText("画图"); 384 } 385 else 386 { 387 rubber = false; 388 eraserMenuItem.setText("橡皮"); 389 eraserButton.setText("橡皮"); 390 g.setColor(foreColor); 391 } 392 } 393 }); 394 395 clearMenuItem.addActionListener(new ActionListener() { 396 public void actionPerformed(final ActionEvent e) { 397 g.setColor(backgroundColor); 398 g.fillRect(0, 0, 570, 390); 399 g.setColor(foreColor); 400 canvas.repaint(); 401 402 } 403 }); 404 405 strokeMenuItem1.addActionListener(new ActionListener() {//细线菜单添加动作监听 406 public void actionPerformed(final ActionEvent e) { 407 //像素为1,线条末端无修饰,折线处呈尖角 408 BasicStroke bs = new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER); 409 g.setStroke(bs);//画图工具使用此笔 410 strokeButton1.setSelected(true);//细线按钮设为选中状态 411 } 412 }); 413 414 strokeMenuItem2.addActionListener(new ActionListener() { 415 public void actionPerformed(final ActionEvent e) { 416 BasicStroke bs = new BasicStroke(2,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER); 417 g.setStroke(bs); 418 strokeButton2.setSelected(true); 419 } 420 }); 421 422 strokeMenuItem3.addActionListener(new ActionListener() { 423 public void actionPerformed(final ActionEvent e) { 424 BasicStroke bs = new BasicStroke(4,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER); 425 g.setStroke(bs); 426 strokeButton3.setSelected(true); 427 } 428 }); 429 430 foregroundMenuItem.addActionListener(new ActionListener() { 431 public void actionPerformed(final ActionEvent e) { 432 Color fColor = JColorChooser.showDialog(DrawPictureFrame.this, "选择颜色对话框", Color.CYAN); 433 if( fColor != null ) 434 foreColor = fColor; 435 foregroundButton.setForeground(foreColor); 436 g.setColor(foreColor); 437 } 438 }); 439 440 backgroundMenuItem.addActionListener(new ActionListener() { 441 public void actionPerformed(final ActionEvent e) { 442 Color bgColor = JColorChooser.showDialog(DrawPictureFrame.this, "选择颜色对话框", Color.CYAN); 443 if( bgColor != null ) 444 backgroundColor = bgColor; 445 backgroundButton.setBackground(backgroundColor); 446 g.setColor(backgroundColor); 447 g.fillRect(0, 0, 570, 390); 448 g.setColor(foreColor); 449 canvas.repaint(); 450 } 451 }); 452 453 saveMenuItem.addActionListener(new ActionListener() { 454 public void actionPerformed(ActionEvent e) { 455 addWatermark();//添加水印 456 DrawImageUtil.saveImage(DrawPictureFrame.this,image);//打印图片 457 } 458 }); 459 460 shuiyinMenuItem.addActionListener(new ActionListener() { 461 public void actionPerformed(ActionEvent e) { 462 //输入对话框 463 System.out.println("facai1"); 464 shuiyin = JOptionPane.showInputDialog(DrawPictureFrame.this,"你想添加什么水印?"); 465 System.out.println("facai2"); 466 if ( null == shuiyin) 467 shuiyin=""; 468 else 469 setTitle("画图程序(水印内容:[ " + shuiyin + " ])");//修改窗体标题 470 System.out.println("facai3"); 471 } 472 }); 473 474 showPicButton.addActionListener(new ActionListener() { 475 public void actionPerformed(ActionEvent e) { 476 boolean isVisible = picWindow.isVisible(); 477 if(isVisible) { 478 showPicButton.setText("展开简笔画"); 479 picWindow.setVisible(false); 480 } 481 else 482 { 483 showPicButton.setText("隐藏简笔画"); 484 //重新指定简笔画展示窗体的显示位置 485 //横坐标 = 主窗体横坐标 - 简笔画窗体宽度 - 5; 纵坐标 = 主窗体纵坐标 486 picWindow.setLocation(getX()-picWindow.getWidth()-5,getY()); 487 picWindow.setVisible(true); 488 } 489 } 490 }); 491 492 493 494 } 495 496 //添加水印 497 private void addWatermark() { 498 if( !"".equals(shuiyin.trim()) )///shuiyin变量删除头尾空白字符后 不等于空 499 { 500 g.rotate(Math.toRadians(-30));//将图片旋转-30弧度 501 Font font = new Font("楷体",Font.BOLD,72);//设置字体 502 g.setFont(font); 503 g.setColor(Color.GRAY); 504 AlphaComposite alpha = AlphaComposite.SrcOver.derive(0.4f);//设置透明效果 505 g.setComposite(alpha); 506 g.drawString(shuiyin, 75, 400);//绘制文字 507 canvas.repaint(); 508 g.rotate(Math.toRadians(30));//将旋转的图片再旋转回来 509 alpha = AlphaComposite.SrcOver.derive(1f);//不透明效果 510 g.setComposite(alpha);//使用不透明效果 511 g.setColor(foreColor);//画笔恢复之前颜色 512 } 513 } 514 515 //恢复展开简笔画按钮的文本内容,此方法供简笔画面板的"隐藏"按钮调用 516 public void initShowPicButton() { 517 showPicButton.setText("展开简笔画"); 518 } 519 520 521 //FrameGetShape接口实现类,用于获得图形空间返回的被选中的图形 522 public void getShape(Shapes shape) 523 { 524 this.shape=shape;//将返回的图形对象赋给类的全局变量 525 drawShape = true;//画图形标识变量为true,说明现在鼠标画的是图形,而不是线条 526 } 527 528 529 530 531 532 public static void main(String[] args) 533 { 534 DrawPictureFrame frame = new DrawPictureFrame();//创建窗体对象 535 frame.setVisible(true);//让窗体可见 536 } 537 538 }
1 package com.mr.draw; 2 3 import java.awt.Canvas; 4 import java.awt.Graphics; 5 import java.awt.Image; 6 7 8 public class DrawPictureCanvas extends Canvas{ 9 private Image image = null;//创建画板中展示的图片对象 10 //设置画板中的图片 11 public void setImage( Image image) 12 { 13 this.image = image; 14 } 15 //重写,在画布上绘制图像 16 public void paint( Graphics g ) 17 { 18 g.drawImage( image,0,0,null ); 19 } 20 21 //重写,解决屏幕闪烁问题 22 public void update( Graphics g ) 23 { 24 paint(g); 25 } 26 }
1 package com.mr.draw; 2 3 import java.awt.BorderLayout; 4 import java.awt.Container; 5 import java.awt.FlowLayout; 6 import java.awt.Image; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 import java.io.File; 10 import javax.swing.ImageIcon; 11 import javax.swing.JButton; 12 import javax.swing.JPanel;//面板 13 import javax.swing.JWindow; 14 import com.mr.util.BackgroundPanel; 15 16 17 18 public class PictureWindow extends JWindow { 19 private JButton changeButton; 20 private JButton hiddenButton; 21 private BackgroundPanel centerPanel; 22 File list[]; 23 int index; 24 DrawPictureFrame frame; 25 26 //构造方法 27 public PictureWindow(DrawPictureFrame frame) { 28 this.frame = frame;//构造参数的值赋给父窗体 29 setSize(400,460); 30 init(); 31 addListener(); 32 } 33 34 private void init() { 35 Container c = getContentPane(); 36 File dir = new File("src/img/picture"); 37 list = dir.listFiles(); 38 centerPanel = new BackgroundPanel(getListImage()); 39 c.add(centerPanel,BorderLayout.CENTER);//背景面板放到主容器的中部 40 FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);//右对齐流布局 41 flow.setHgap(20);//水平间隔20像素 42 JPanel southPanel = new JPanel();//创建南部面板 43 southPanel.setLayout(flow);//使用刚才建好的流布局 44 changeButton = new JButton("更换图片"); 45 southPanel.add(changeButton); 46 hiddenButton = new JButton("隐藏"); 47 southPanel.add(hiddenButton); 48 c.add(southPanel,BorderLayout.SOUTH);//南部面板放在主容器的南部 49 } 50 51 //添加监听 52 private void addListener() { 53 hiddenButton.addActionListener(new ActionListener() { 54 public void actionPerformed(ActionEvent e) { 55 setVisible(false);//隐藏按钮点击时窗体不可见 56 frame.initShowPicButton(); 57 } 58 }); 59 changeButton.addActionListener(new ActionListener() { 60 public void actionPerformed(ActionEvent e) { 61 centerPanel.setImage(getListImage());//更换图片按钮点击时背景面板重新载入图片 62 } 63 }); 64 } 65 66 private Image getListImage() { 67 String imgPath = list[index].getAbsolutePath(); 68 ImageIcon image = new ImageIcon(imgPath); 69 index++; 70 if(index>=list.length) 71 index=0; 72 return image.getImage(); 73 } 74 75 }
3.项目界面
标签:lis ref custom ini rar mit import explore 背景色
原文地址:https://www.cnblogs.com/shoulinniao/p/11355287.html