Java swing

一.Swing概述html

二.建立窗體
三.經常使用組件java

            1.JLabel標籤組件安全

            2.JButton按鈕組件dom

            3.JRadioButton(單選按鈕)組件ide

            4.JCheckBox(複選框)組件佈局

            5.JComboBox(選擇框)組件.net

            6.JList(列表框)組件設計

            7.JTextField(文本框)組件orm

            8.JPasswordField(密碼框)組件htm

            9.JTextArea(文本域)組件

四.經常使用佈局管理器

            1.不使用佈局管理器

            2.FlowLayout佈局管理器

            3.BorderLayout佈局管理器

            4.GridLayout佈局管理器

五.經常使用面板

           1.JPanel面板

           2.JScrollPane面板

六.經常使用事件處理

          1.動做事件處理(ActionEvent類捕獲)

經常使用的狀況是:監聽鼠標單擊按鈕後將進行發生的動做。動做事件能夠經過實現接口ActionListener實現動做。

ActionEvent類中有兩個經常使用方法:

1》getSource():用來得到處罰這次事件的組件對象,返回值類型爲Object

2》getActionCommand():用來得到與當前動做相關的命令字符串,返回值類型爲String

public interface ActionListener extends EventListener{

     public void actionPerformed(ActionEvent e);

}

舉例:

package javaSwing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ActionEventExample extends JFrame {
    
    //定義動做事件的屬性
    private JLabel label;   //用來提示信息

    public ActionEventExample() {
        // TODO Auto-generated constructor stub
        
        //對窗體進行設置
        super();
        setTitle("動做事件示例");
        setBounds(100,100,500,375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    //設置關閉窗體時退出程序
        
        //對標籤進行設置
        label=new JLabel();
        label.setText("歡迎登陸!");
        label.setHorizontalAlignment(JLabel.CENTER);
        
        //對控制面板進行設置
        JPanel panel=new JPanel();
        getContentPane().add(label);
        
        //對按鈕進行設置
        final JButton submitButton=new JButton();
        submitButton.setText("登陸");
        submitButton.addActionListener(new buttonListener());
        getContentPane().add(submitButton,BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
          ActionEventExample frame=new ActionEventExample();
          frame.setVisible(true);
    }
    
    class  buttonListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            JButton button=(JButton) e.getSource();  //得到處罰這次事件的按鈕對象
            String buttonName=e.getActionCommand();   //h得到觸發這次事件的按鈕的標籤文本
            if(buttonName.equals("登陸")){
                
                  label.setText("您已經成功登陸");
                  button.setText("退出");
                
            }else{
                
                  label.setText("您已經安全退出");
                  button.setText("登陸");
            }
        }
        
    }

}

          2.焦點事件處理

          3.鼠標事件處理

          4.鍵盤事件處理

七.拼圖小遊戲源代碼

       步驟一:設計一個大窗體

       步驟二:在窗體添加兩個控制面板

       步驟三:在控制面板中添加按鈕和標籤

       步驟四:對按鈕進行監聽

注意:本遊戲是九宮格小遊戲,九個圖片中每個都是120(px)*120(px)像素,製做九宮格教程能夠參照微博:http://blog.sina.com.cn/s/blog_13a975b850102wyz3.html

同時注意圖片的放置位置,imgs和src在同一級目錄。對於java的目錄位置能夠參考博客:http://blog.csdn.net/slqslqshiliqiang/article/details/71435751

源代碼:

package pingTuYouXi; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.TitledBorder; public class MedleyGame extends JFrame{          //設置窗體屬性     private JPanel centerPanel;    //拼圖按鈕面板     private JButton emptyButton;   //空白按鈕對象          public MedleyGame() {         // TODO Auto-generated constructor stub         super();   //繼承JFrame類的構造方法         setResizable(false);   //設置窗體大小不可改變         setTitle("拼圖遊戲");     //設置窗體的標題         setBounds(100,100,370,525);  //設置窗體的顯示位置以及大小         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //設置關閉窗體時退出程序                           //爲窗體建立一個一個標籤組件和按鈕組件         final JPanel topPanel=new JPanel();        //建立面板對象         topPanel.setBorder(new TitledBorder(null,"",TitledBorder.DEFAULT_JUSTIFICATION,TitledBorder.DEFAULT_POSITION,null,null));         //爲控制面板添加邊框         topPanel.setLayout(new BorderLayout());              //控制面板採用邊界佈局         getContentPane().add(topPanel,BorderLayout.NORTH);   //將面板添加到窗體頂部         //設置標籤         final JLabel modelLabel=new JLabel();         modelLabel.setIcon(new ImageIcon("imgs/model.jpg"));         topPanel.add(modelLabel,BorderLayout.WEST);           //將標籤添加到面板的左側         //設置按鈕         final JButton startButton=new JButton();             //建立「下一局」按鈕對象         startButton.setText("下一局");         startButton.addActionListener(new StartButtonAction());         topPanel.add(startButton,BorderLayout.CENTER);                                    //爲窗體建立另外一個面板,該面板是圖片移動面板         centerPanel=new JPanel();             //建立拼圖按鈕面板對象         centerPanel.setBorder(new TitledBorder(null,"",TitledBorder.DEFAULT_JUSTIFICATION,TitledBorder.DEFAULT_POSITION,null,null));//爲面板添加邊框         centerPanel.setLayout(new GridLayout(0,3));    //將面板添加到窗體的中間         getContentPane().add(centerPanel,BorderLayout.CENTER);   //將面板添加到窗體的中間                 String[][] stochasticOrder=reorder();              //stochastic 隨機的         for(int row=0;row<3;row++){                            //遍歷行             for(int col=0;col<3;col++){                        //遍歷列                                  final JButton button=new JButton();      //建立拼圖按鈕對象                 button.setName(row+""+col);              //建立按鈕的名稱                 button.setIcon(new ImageIcon(stochasticOrder[row][col]));        //爲按鈕設置圖片                                  if(stochasticOrder[row][col].equals("imgs/00.jpg")){  //判斷是否爲空白按鈕                                                emptyButton=button;                 }                 button.addActionListener(new ImgButtonAction());     //爲按鈕設置監聽器                 centerPanel.add(button);                    //爲按鈕添加到拼圖按鈕面板中             }         }                                }               //用來生成網格圖片隨機擺放順序     private String[][] reorder(){    //用來獲取網格圖片的隨機擺放順序         String[][] exactnessOrder=new String[3][3];    //網格圖片的正確拜訪順序       exactness正確順序         for(int row=0;row<3;row++){                                         //遍歷行             for(int col=0;col<3;col++){                                     //遍歷列                  exactnessOrder[row][col]="imgs/"+row+col+".jpg";                  //exactnessOrder[row][col]=row+col+".jpg";             }         }                  String[][] stochasticOrder=new String[3][3];   //網格圖片的隨機拜訪順序         for(int row=0;row<3;row++)         //遍歷行             for(int col=0;col<3;col++){                          //遍歷列                 while(stochasticOrder[row][col]==null){                     /*int r=(int)(Math.random()*3);         //取隨機行                     int c=(int)(Math.random()*3);         //取隨機列                     if(exactnessOrder[r][c]!=null){                         stochasticOrder[row][col]=exactnessOrder[r][c];                         exactnessOrder[r][c]=null;                     }*/                     /*if(row<1&col<2){                         if(col==0)                           stochasticOrder[row][col]=exactnessOrder[0][1];                         if(col==1)                             stochasticOrder[row][col]=exactnessOrder[0][0];                     }else{                          stochasticOrder[row][col]=exactnessOrder[row][col];                     }*/                     stochasticOrder[row][col]=exactnessOrder[col][row];                 }             }         return stochasticOrder;     }               public static void main(String[] args) {         // TODO Auto-generated method stub          try{               MedleyGame frame=new MedleyGame();    //建立本類的對象               frame.setVisible(true);               //設置窗體可見          }catch(Exception e){               e.printStackTrace();          }     }          //拼圖監聽按鈕,用來監聽按鈕的狀況     class ImgButtonAction implements ActionListener{    //拼圖按鈕監聽器                   public void actionPerformed(ActionEvent e){                           String emptyName=emptyButton.getName();     //得到空白按鈕的名字              char emptyRow=emptyName.charAt(0);          //得到空白按鈕所在的行              char emptyCol=emptyName.charAt(1);          //得到空白按鈕的列                           JButton clickButton=(JButton) e.getSource();    //得到被單擊按鈕對象              String  clickName=clickButton.getName();        //得到被單擊按鈕的名稱                           char clickRow=clickName.charAt(0);  //得到被單擊按鈕所在的行              char clickCol=clickName.charAt(1);   //得到被單擊按鈕所在的列                           //判斷被單擊按鈕與空白按鈕是否相臨              if(Math.abs(clickRow-emptyRow)+Math.abs(clickCol-emptyCol)==1){                  //將被單擊按鈕的圖片移動到空白按鈕上                  emptyButton.setIcon(clickButton.getIcon());                                   //設置被單擊的按鈕顯示空白圖片                  clickButton.setIcon(new ImageIcon("imgs/00.jpg"));                  emptyButton=clickButton;    //將被單擊的按鈕設置爲空白按鈕                               }                       }              }          //編寫"下一句"按鈕的監聽器類     class StartButtonAction implements ActionListener{         public void actionPerformed(ActionEvent e){                           String[][]  stochasticOrder=reorder();   //得到網格圖片的隨機拜訪順序              int i=0;                                //拼圖按鈕在拼圖按鈕面板中的索引                           for(int row=0;row<3;row++){             //遍歷行                  for(int col=0;col<3;col++){         //遍歷列                                            JButton button=(JButton)centerPanel.getComponent(i++);                       button.setIcon(new ImageIcon(stochasticOrder[row][col]));                       if(stochasticOrder[row][col].equals("imgs/00.jpg")){                             emptyButton=button;                       }                                         }              }                       }     } }

相關文章
相關標籤/搜索