javaform小表單樣本用label和image

來源:互聯網
上載者:User
做的一個小程式,分享一下,給初學者一個樣本吧。。我就是初學的。功能:顯示滑鼠的位置。。。。有幾個圖片,點擊後會改變表情用label存文本資訊,用幾個label存圖片,把每個label定位在form上。只要把代碼考過去就好了。在根目錄下建立一個image檔案夾,放兩張小表情,happy.gif,unhappy.gif 或者自己修改路徑也行我把項目放到網上去
 
import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import java.awt.event.*;public class imageshow {public static void main(String[] args) { JFrame frame1 = new winform();frame1.setVisible(true);//顯示    } }class winform extends JFrame{public  JLabel label1;  //label標籤public  JLabel label2;  //用於顯示圖片 label2---7 public  JLabel label3;public  JLabel label4;public  JLabel label5;public  JLabel label6;public  JLabel label7;public  JLabel label8;//隱藏,因為它會出現在最左邊,很討厭public  String filename1 = "image/happy.gif";//檔案路徑public  String filename2 = "image/unhappy.gif";public  ImageIcon image1 = null;public  ImageIcon image2 = null;int x[] = new int[9];  //記錄第i個label現在的圖片public winform( ){for(int i=0;i<9;i++) x[i]= 0; setTitle("視窗小程式");setSize(500, 400); setLocationRelativeTo(null); // 初始在螢幕中央setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 使點擊紅X可以退出程式 label1 = new JLabel(); label1.setText("滑鼠指標的位置:     ");label1.setFont(new   java.awt.Font("Dialog",1,18)); //設定文子格式,//label1.setHorizontalAlignment(JLabel.LEFT);//label1.setVerticalAlignment(JLabel.TOP);label1.setBounds(10, 10, 300, 40);//x,y,width,height, 座標x,y,寬度,高度//label1.setLocation(0,0);getContentPane().add(label1); //加入form中                 image1 =new ImageIcon(filename1);//擷取圖片         image2 = new ImageIcon(filename2);                  label2 = new JLabel();         label2.setBounds(300, 100, 30, 30);         label2.setIcon(image1);//設定label的圖片         //label2.setLocation(100,100);         getContentPane().add(label2);                  label3 = new JLabel();         label3.setBounds(250, 150, 30, 30);         label3.setIcon(image1);         //label3.setLocation(100,100);         getContentPane().add(label3);                  label4 = new JLabel();         label4.setBounds(350, 150, 30, 30);         label4.setIcon(image1);         //label4.setLocation(100,100);         getContentPane().add(label4);                  label5 = new JLabel();         label5.setBounds(200, 200, 30, 30);         label5.setIcon(image1);         //label5.setLocation(100,100);         getContentPane().add(label5);                  label6 = new JLabel();         label6.setBounds(300, 200, 30, 30);         label6.setIcon(image1);         //label6.setLocation(100,100);         getContentPane().add(label6);                  label7 = new JLabel();         label7.setBounds(400, 200, 30, 30);         label7.setIcon(image1);         //label7.setLocation(100,100);         getContentPane().add(label7);                  label8 = new JLabel();         label8.setBounds(0, 0, 0, 0);         //label8.setIcon(image1);         label8.setLocation(000,000);                  getContentPane().add(label8);                         addMouseListener(           new MouseListener() {             //釋放事件           public void mouseReleased(MouseEvent e){           tt( e.getX(), e.getY());           }           //進入事件           public void mouseEntered(MouseEvent e){           tt( e.getX(), e.getY());        }           //離開           public void mouseExited(MouseEvent e){           tt( e.getX(), e.getY());                }           //點擊事件           public void mouseClicked(MouseEvent e) {                   tt( e.getX(), e.getY());       }           //按下事件           public void mousePressed(MouseEvent e){           tt( e.getX(), e.getY());       }           }            );}public void tt(int x,int y)   {label1.setText("滑鼠指標的位置:"+x+":"+y); if(300+5 < x && x < 300+30 && 100+30<y&&y<100+60)//定位用的    label2.setIcon(changeimage(2));if(250+5 < x && x < 250+30 && 150+30<y&&y<150+60)    label3.setIcon(changeimage(3));if(350+5 < x && x < 350+30 && 150+30<y&&y<150+60)    label4.setIcon(changeimage(4));if(200+5 < x && x < 200+30 && 200+30<y&&y<200+60)    label5.setIcon(changeimage(5));if(300+5 < x && x < 300+30 && 200+30<y&&y<200+60)    label6.setIcon(changeimage(6));if(400+5 < x && x < 400+30 && 200+30<y&&y<200+60)    label7.setIcon(changeimage(7));   }public ImageIcon changeimage(int y){//判斷現在的圖片,並且修改改變後的情況,並返回要改變成的圖片if(x[y] == 1){x[y] = 2;return image2;}else {x[y] = 1;return image1;}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.