编程语言
首页 > 编程语言> > Java 窗口 小马图像窗口

Java 窗口 小马图像窗口

作者:互联网

写在前面:

  eclipse接着爽到

  全是借鉴的,东改西改,而且记不住原网址了

  两个月前写的,忘了思路,嗯,It just works

 

  运行效果:

图像随便选(放到*jar所在目录*\\pictures\\)

能选择永远显示在前

鼠标滚轮放大缩小

可以隐藏UI

 

Java代码如下:

  1 package ponyimg;
  2 
  3 import java.awt.*;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.awt.event.MouseAdapter;
  7 import java.awt.event.MouseEvent;
  8 import java.awt.event.MouseMotionAdapter;
  9 import java.awt.event.MouseWheelEvent;
 10 import java.awt.event.MouseWheelListener;
 11 import java.io.*; 
 12 import javax.swing.ImageIcon;
 13 import javax.swing.JButton;
 14 import javax.swing.JFileChooser;
 15 import javax.swing.JFrame;
 16 import javax.swing.JLabel;
 17 
 18 public class PonyIMG extends JFrame
 19 {
 20     static int minx=192,miny=108;
 21     static boolean ishide=false;
 22     static int pframexOri,pframeyOri,pframex,pframey,ponyresult;
 23     static int ori_pict_x,ori_pict_y,pict_x,pict_y,ponyscale=20;
 24     static String ponySname;
 25     static ImageIcon ponyimgtmp;
 26     
 27     public static void main(String[] args) throws Exception
 28     {
 29         JFrame ponyFrame=new JFrame("PonyIMG");
 30         File directory = new File("");
 31         ponyFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\32.png"));
 32         ImageIcon ponydefaultimg=new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\pictures\\default.png"));
 33         ori_pict_x=ponydefaultimg.getIconWidth();
 34         ori_pict_y=ponydefaultimg.getIconHeight();
 35         JButton ponyb_front = new JButton("");
 36         JButton ponyb_exit = new JButton("");
 37         JButton ponyb_open = new JButton("");
 38         JButton ponyb_hide = new JButton("");
 39         ImageIcon ponyb_fronti = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\front.png"));
 40         ImageIcon ponyb_nfronti = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\nfront.png"));
 41         ImageIcon ponyb_exiti = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\exit.png"));
 42         ImageIcon ponyb_openi = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\open.png"));
 43         ImageIcon ponyb_hidei = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\hide.png"));
 44         ImageIcon ponyb_uhidei = new ImageIcon(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\uhide.png"));
 45         JFileChooser ponychooser = new JFileChooser(directory.getCanonicalPath()+"\\pictures");
 46         ponychooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
 47         ponychooser.setDialogTitle("Get an image...");
 48         
 49         /*Whole*/
 50         ponyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 51         ponyFrame.setLayout(null);
 52         ponyFrame.setResizable(false);
 53         ponyFrame.setLocationRelativeTo(null);
 54         ponyFrame.setSize(ponydefaultimg.getIconWidth(),ponydefaultimg.getIconHeight());
 55         ponyFrame.setUndecorated(true);
 56         ponyFrame.setAlwaysOnTop(false);
 57         ponyFrame.setBackground(new Color(0,0,0,0));
 58         
 59         /*BUTTON_FRONT*/
 60         ponyb_front.setIcon(ponyb_nfronti);
 61         ponyb_front.setSize(27, 27);
 62         ponyb_front.setLocation(ponydefaultimg.getIconWidth()-64, 5);
 63         ponyb_front.addActionListener(new ActionListener(){
 64             public void actionPerformed(ActionEvent e)
 65             {
 66                 if(ponyFrame.isAlwaysOnTop()==true)
 67                 {
 68                     ponyFrame.setAlwaysOnTop(false);
 69                     ponyb_front.setIcon(ponyb_nfronti);
 70                 }
 71                 else
 72                 {
 73                     ponyFrame.setAlwaysOnTop(true);
 74                     ponyb_front.setIcon(ponyb_fronti);
 75                 }
 76             }
 77         });
 78         ponyFrame.add(ponyb_front);
 79         
 80         /*BUTTON_EXIT*/
 81         ponyb_exit.setIcon(ponyb_exiti);
 82         ponyb_exit.setSize(27, 27);
 83         ponyb_exit.setLocation(ponydefaultimg.getIconWidth()-32, 5);
 84         ponyb_exit.addActionListener(new ActionListener(){
 85             public void actionPerformed(ActionEvent e)
 86             {
 87                 System.exit(0);
 88             }
 89         });
 90         ponyFrame.add(ponyb_exit);
 91         
 92         /*BUTTON_OPEN*/
 93         ponyb_open.setIcon(ponyb_openi);
 94         ponyb_open.setSize(27, 27);
 95         ponyb_open.setLocation(5, ponydefaultimg.getIconHeight()-32);
 96         ponyFrame.add(ponyb_open);
 97         
 98         /*BUTTON_HIDE*/
 99         ponyb_hide.setIcon(ponyb_uhidei);
100         ponyb_hide.setSize(27, 27);
101         ponyb_hide.setLocation(5, 5);
102         ponyb_hide.addActionListener(new ActionListener(){
103             public void actionPerformed(ActionEvent e)
104             {
105                 if(ishide==false) {
106                     ishide=true;
107                     ponyb_hide.setIcon(ponyb_hidei);
108                     ponyb_front.setVisible(false);
109                     ponyb_exit.setVisible(false);
110                     ponyb_open.setVisible(false);
111                 }
112                 else {
113                     ishide=false;
114                     ponyb_hide.setIcon(ponyb_uhidei);
115                     ponyb_front.setVisible(true);
116                     ponyb_exit.setVisible(true);
117                     ponyb_open.setVisible(true);
118                 }
119 
120             }
121         });
122         ponyFrame.add(ponyb_hide);
123         
124         /*IMG*/
125         JLabel Label00 = new JLabel(ponydefaultimg);
126         Label00.setSize(ponydefaultimg.getIconWidth(),ponydefaultimg.getIconHeight());
127         ponyFrame.add(Label00);
128         
129         /*BUTTON_OPEN*/
130         ponyb_open.addActionListener(new ActionListener(){
131             public void actionPerformed(ActionEvent e)
132             {
133                 ponyresult=ponychooser.showOpenDialog(ponyFrame);
134                 if(ponychooser.APPROVE_OPTION == ponyresult)
135                 {
136                     ponySname=ponychooser.getSelectedFile().getPath();
137                     ponyimgtmp=new ImageIcon(Toolkit.getDefaultToolkit().getImage(ponySname));
138                     
139                     if(!(ponyimgtmp.getIconWidth()>=minx && ponyimgtmp.getIconHeight()>=miny))
140                     {
141                         ponyimgtmp=ponydefaultimg;
142                     }
143                     ori_pict_x=ponyimgtmp.getIconWidth();
144                     ori_pict_y=ponyimgtmp.getIconHeight();
145                     Label00.setIcon(ponyimgtmp);
146                     ponyFrame.setSize(ponyimgtmp.getIconWidth(),ponyimgtmp.getIconHeight());
147                     Label00.setSize(ponyimgtmp.getIconWidth(),ponyimgtmp.getIconHeight());
148                     ponyb_front.setLocation(ponyimgtmp.getIconWidth()-64, 5);
149                     ponyb_exit.setLocation(ponyimgtmp.getIconWidth()-32, 5);
150                     ponyb_open.setLocation(5, ponyimgtmp.getIconHeight()-32);
151                     ponyscale=20;
152                 }
153             }
154         });
155         
156         /*MOUSE*/
157         ponyFrame.addMouseListener(new MouseAdapter() {
158             @Override
159             public void mousePressed(MouseEvent e) {
160                 pframexOri=e.getX();
161                 pframeyOri=e.getY();
162             }
163         });
164         ponyFrame.addMouseMotionListener(new MouseMotionAdapter() {
165             @Override
166             public void mouseDragged(MouseEvent e) {
167                 ponyFrame.setLocation(e.getXOnScreen()-pframexOri,e.getYOnScreen()-pframeyOri);
168             }
169         });
170         ponyFrame.addMouseWheelListener(new MouseWheelListener() {
171             @Override
172             public void mouseWheelMoved(MouseWheelEvent e) {
173                 if(e.getWheelRotation()==-1){
174                     if(ponyscale<=19) {
175                         ++ponyscale;
176                         pict_x=(ori_pict_x/20)*ponyscale;
177                         pict_y=(ori_pict_y/20)*ponyscale;
178                         if(ponyscale==20) {
179                             pict_x=ori_pict_x;
180                             pict_y=ori_pict_y;
181                         }
182                         ImageIcon ponytmp=new ImageIcon(ponyimgtmp.getImage().getScaledInstance(pict_x, pict_y, Image.SCALE_DEFAULT));
183                         Label00.setIcon(ponytmp);
184                         ponyFrame.setSize(pict_x,pict_y);
185                         Label00.setSize(pict_x,pict_y);
186                         ponyb_front.setLocation(pict_x-64, 5);
187                         ponyb_exit.setLocation(pict_x-32, 5);
188                         ponyb_open.setLocation(5, pict_y-32);
189                       }
190                   }
191                 if(e.getWheelRotation()==1){
192                     if(ponyscale>=1 && ((ori_pict_x/20)*(ponyscale-1)>=minx) && ((ori_pict_y/20)*(ponyscale-1)>=miny)) {
193                         --ponyscale;
194                         pict_x=(ori_pict_x/20)*ponyscale;
195                         pict_y=(ori_pict_y/20)*ponyscale;
196                         ImageIcon ponytmp=new ImageIcon(ponyimgtmp.getImage().getScaledInstance(pict_x, pict_y, Image.SCALE_DEFAULT));
197                         Label00.setIcon(ponytmp);                        
198                         ponyFrame.setSize(pict_x,pict_y);
199                         Label00.setSize(pict_x,pict_y);
200                         ponyb_front.setLocation(pict_x-64, 5);
201                         ponyb_exit.setLocation(pict_x-32, 5);
202                         ponyb_open.setLocation(5, pict_y-32);
203                       }
204                   }
205             }
206         });
207         
208         ponyFrame.setVisible(true);
209     }
210 }

没注释,摸了

 

附件:

*jar所在目录*\\icons\\

32.png

exit.png

front.png

hide.png

nfront.png

open.png

uhide.png

*jar所在目录*\\pictures\\

default.png


标签:ImageIcon,小马,Java,ponyFrame,ponyb,pict,ponyimgtmp,new,窗口
来源: https://www.cnblogs.com/Antigonae/p/12659972.html