JAVA之Swing程序设计

来源:互联网 发布:数据分析师考试大纲 编辑:程序博客网 时间:2024/05/21 06:40

一.Swing概述

二.创建窗体
三.常用组件

            1.JLabel标签组件

            2.JButton按钮组件

            3.JRadioButton(单选按钮)组件

            4.JCheckBox(复选框)组件

            5.JComboBox(选择框)组件

            6.JList(列表框)组件

            7.JTextField(文本框)组件

            8.JPasswordField(密码框)组件

            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;
                      }
                      
                 }
             }
            
         }
    }

}

















0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华为沾了海水打不开机怎么办 华为麦芒手机忘记锁屏密码怎么办 华为手机的方框键摁不了怎么办 笔记本自动更新到一半太慢了怎么办 华为麦芒5音量下键乱跑了怎么办 麦芒6手机QQ视频没声音怎么办 18:9看16:9黑边怎么办 华为畅享7s声音小怎么办 华为畅享8手机声音小怎么办 华为畅享8plus声音小怎么办 荣耀7x锁屏密码忘记怎么办 华为荣耀7x锁屏密码忘记了怎么办 苹果耳机进水后声音变了怎么办 华为荣耀开了数据用不了怎么办 华为手机高德地图信号弱怎么办? 手机QQ浏览器看视频有广告怎么办 手机显示网络连接但不可上网怎么办 华为手机关机后开不了机怎么办 华为畅享8p相机拍照模糊怎么办 手机触屏不准怎么办荣耀青春版九 华为手机锁屏手势密码忘了怎么办 荣耀手机锁屏密码忘了怎么办 华为p20隐私空间密码忘了怎么办 安全管家隐私保护的密码忘了怎么办 华为手机自带截图键删除了怎么办 飞科电吹风吹一会就断电怎么办 住酒店时电吹风吹坏了怎么办 把话费充到停机的号码上去了怎么办 电信手机卡充值了还停机怎么办 电信手机一直没用却欠费了怎么办 苹果se开起4g信号不好怎么办 触屏华为手机充不了电怎么办 华为手机自拍出来的字反向怎么办 华为微信隐藏了怎么弄出来怎么办 酷派手机酷管家密码忘了怎么办 手机磁盘目录不具有读写权限怎么办 魅族手机像素突然变模糊了怎么办 手机忽然所有软件都没了怎么办 魅族读书下架的书怎么办 我的电信大王卡激活了没信号怎么办 三星翻盖手机忘记锁屏密码怎么办