JButton在每次点击时将图像尺寸像素扩展到10%?

时间:2016-04-23 16:09:08

标签: java

这是我的TextEditor项目,当用户上传图像时,默认图像大小为400x400像素。我添加了两个名为ExpandImageShrinkImage的按钮。每次点击ExpandImage按钮图像都应该扩展到50像素。

      image = new ImageIcon(filename);
         icon = new ImageIcon(image.getImage().getScaledInstance(400, 400, Image.SCALE_DEFAULT));
         editorText.insertIcon(icon);
         editorText.setEditable(true); 
         editorText.setFocusable(true);



 ExpandImage.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent e) {
                     // do stuff here

                    // 400 += 50;
                    // 400 += 50;

                     System.out.println("You clicked me!");  
                 }        
             });

1 个答案:

答案 0 :(得分:0)

     image = new ImageIcon(filename);
     icon = new ImageIcon(image.getImage().getScaledInstance(400, 400, Image.SCALE_DEFAULT));
     editorText.insertIcon(icon);
     editorText.setEditable(true); 
     editorText.setFocusable(true); 


   int point1 = 400;
   int point2 = 400;              

   ExpandImage.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e) {
         point1 +=50;
         point2 +=50;
              image = new ImageIcon(filename);
 icon = new ImageIcon(image.getImage().getScaledInstance(point1,point2,  Image.SCALE_DEFAULT));
 //
 editorText.setIcon(null) 
 //
 editorText.insertIcon(icon);
 editorText.setEditable(true); 
 editorText.setFocusable(true);


             System.out.println("You clicked me!");  
         }        
     });

你应该得到之前的值并将它增加你想要的值,在这种情况下,我将前一个值增加50。然后调用方法以使用新值更改图标。感谢您发布输出