JAVA的swing編程筆記

來源:互聯網
上載者:User

標籤:tle   對象   重寫   next   component   clip   file   程式   設定   

工具:myeclipse

1.建立一個Frame類

   右鍵項目--->new--->class--->填寫包名,類名(視窗名),選擇父類JFrame--->finish.

   右鍵項目--->new--->other--->JFrame--->填寫類名(視窗名)--->finish.

 2.布局管理器

    我常用布局為absolute layout(絕對布局),flowlayout(流式布局),borderlayout(邊界布局),gridlayout(網格布局)cardlayout(卡片布局)

    具體看http://zhangjunhd.blog.51cto.com/113473/128174/

    寫寫我最愛的cardlayout

    CardLayout cl=new CardLayout ();

    JPanel jp=new JPanel();

    jp.setLayout(sl);

    添加卡片

    jp.add(JPanel1,"JPanel1");

    jpadd(JPanel2,"JPanel2");

                  *****

    跳轉

    cl.first(jp);

    cl.next(jp);

    cl.last(jp);

    cl.show(jp,"JPanel");這就任意跳轉了。

 3.按鈕事件----彈窗

    類JFileChooser

         JFileChooser.setFileSelectionMode(arg)--------參數arg可以使JFileChooser.File_ONLY、JFileChooser.Directories_ONLY和JFileChooser.File_and_Directories.以此控制視窗的選擇對象。

    提示小視窗(dialog)

         JOptionpane.showMessageDialog(null,"內容","title",JOptionpane.error_message)    注意:此視窗點擊後不中斷程式,繼續向下運行。

    其他http://blog.csdn.net/penjie0418/article/details/9257917

 4.背景設定

       JPanel背景

           new JPanel(){

                     protected void paintComponent(Graphics g){

                           此處省略

                     }

           }

       組件背景

           ImageIcon icon=new ImageIcon("BufferImage");

           組件名.setIcon(icon);

       利用border設定背景

            自訂myborder重寫paintBorder()

            new Border(){

                  public void paintBorder(Component c,Graphics g,int x,int y,int width,int height){

                            此處省略。。。

                  }

             }

           在為contentPane設定border

              contentPane.setBorder(myborder);如此可以直接在畫板上添加組件。

5.自訂frame視窗

     setUndecorated(true);去掉原來的邊框,自己在contentPane上添加一些功能組件(最小化、最大化、exit等),在邊框範圍添加滑鼠事件,時期可拖動視窗。這樣就比較炫了

 

JAVA的swing編程筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.