• java学习日记---------------------------------对JFrame的操作


    设置背景图片、添加音乐、监控键盘、 改变字体风格等等!

    import java.applet.Applet;
    import java.applet.AudioClip;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.File;
    import java.net.MalformedURLException;
    import java.net.URL;
    import javax.sound.sampled.*;
    
    import javax.swing.*;
    
    
    public class Password_Lock extends JFrame implements ActionListener{
        
        private JLabel label1,label2;
        private JTextField text_Login;
        private JPasswordField text_password;
        private JButton button_login,button_exit,button_q;
        private JPanel panel;
        
        //private Image image =  null ;
        private ImageIcon imageicon=new ImageIcon("D:/JAVA/Eclipse/Christmas_Eve/kulou.jpg");
        private JLabel label_background=new JLabel(imageicon);
        
        public Password_Lock(){
            super("密码锁");
            this.setLocation(0,0);
            this.setSize(imageicon.getIconWidth(), imageicon.getIconHeight());
            this.setUndecorated(true);//去windows修饰
            
           /* this.addKeyListener(new KeyAdapter(){//panel不识别,KeyListener需要焦点的, 如果你按了某个按钮之类的
            * (通常会使这个按钮获得焦点), 那他的父组件(例如这个panel)就无法捕获键盘事件了
                
                public void keyPressed(KeyEvent e){
                    if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
                        System.exit(0);
                }
            });*/
            
            init();
        }
        
        
    public void init(){
            
            panel=new JPanel();
            this.add(panel,"Center");
            //this.setLayout(null);
            
            panel.setLayout(null);
            
            label1=new JLabel("用户名:");
            label1.setBounds(new Rectangle(580,455,90,50));
            label1.setFont(new Font("宋体",Font.BOLD,16));
            label1.setForeground(Color.green);
            panel.add(label1);
            
            text_Login=new JTextField(" 计算机131");
            text_Login.setEditable(false);
            text_Login.setFont(new Font("宋体",Font.BOLD,16));
            text_Login.setForeground(Color.red);
            text_Login.setBounds(new Rectangle(650,465,110,30));
            panel.add(text_Login);
            
            label2=new JLabel("  密码:");
            label2.setBounds(new Rectangle(580,495,90,60));
            label2.setFont(new Font("宋体",Font.BOLD,16));
            label2.setForeground(Color.green);
            panel.add(label2);
            
            text_password=new JPasswordField();
            text_password.setFont(new Font("宋体",Font.BOLD,30));
            text_password.setBounds(new Rectangle(650,510,110,30));
            panel.add(text_password);
            
            button_login=new JButton("确 定");
            button_login.setBounds(new Rectangle(570,565,90,30));
            button_login.setFont(new Font("宋体",Font.BOLD,16));
            button_login.setForeground(Color.RED);
            button_login.addActionListener(this);
            
            //this.addKeyListener(this);
            
            panel.add(button_login);
            
            button_exit=new JButton("重 置");
            button_exit.setBounds(new Rectangle(695,565,90,30));
            button_exit.setFont(new Font("宋体",Font.BOLD,16));
            
            panel.add(button_exit);
            button_exit.addActionListener( this);
            
            button_q=new JButton("退出");
            button_q.setBounds(new Rectangle(0,0,2,5));
            button_q.setBackground(Color.BLACK);
            panel.add(button_q);
            button_q.addActionListener(this);
            
            
            Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener(){//我去,老费劲了,加个键盘全局监听
                public void eventDispatched(AWTEvent e){
                    if(((KeyEvent)e).getKeyChar()==KeyEvent.VK_ESCAPE){
                        System.exit(0);
                    }
                    if(((KeyEvent)e).getKeyChar()==KeyEvent.VK_ENTER){
                        test();
                    } 
                }
            },AWTEvent.KEY_EVENT_MASK);
            
            label_background.setBounds(new Rectangle(0,0,imageicon.getIconWidth(), imageicon.getIconHeight()));
            //this.getLayeredPane().add(label_background);
            
            this.getLayeredPane().add(label_background,new Integer(Integer.MIN_VALUE));//添加到JFrame的getLayeredPane层
            ((JPanel) this.getContentPane()).setOpaque(false);//透明
            panel.setOpaque(false);
            this.setVisible(true);
            
        }
        
    
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        
        
        if(e.getSource()==button_login){
            test();
        }
        else if(e.getSource()==button_q){
            System.exit(0);
        }
        else if(e.getSource()==button_exit){
            text_password.setText(null);
        }
        
    }
    public void test(){
        
        if(text_password.getText().equals("6472")){
            this.setVisible(false);
            new newFrame();
        }
        else {
            text_password.setText(null);
            JOptionPane.showMessageDialog(this, "密码输入有误,请重新输入");
        }
        
    }
     
    
    
    class newFrame extends JFrame implements ActionListener{
        
        
        private ImageIcon imageicon1=new ImageIcon("D:/JAVA/Eclipse/Christmas_Eve/平安夜.jpg");
        private JLabel label_background1=new JLabel(imageicon1);
        private JPanel panel_1=new JPanel();
        private JButton button_q=new JButton("退出");
        
        public newFrame(){
            
            super("你赢了!");
            this.setBounds(0, 0, imageicon1.getIconWidth(), imageicon1.getIconHeight());
            
            this.setUndecorated(true);
            panel_1.setLayout(null);
            this.getContentPane().add(panel_1);
            
            button_q.setBounds(new Rectangle(0,0,2,5));
            button_q.setBackground(Color.blue);
            panel_1.add(button_q);
            button_q.addActionListener(this);
            
            playMusic();//
        
            
            //this.getContentPane().add(label_background1);
            label_background1.setBounds(new Rectangle(0,0,imageicon.getIconWidth(), imageicon.getIconHeight()));
            this.getLayeredPane().add(label_background1,new Integer(Integer.MIN_VALUE));//添加到JFrame的getLayeredPane层
           ((JPanel) this.getContentPane()).setOpaque(false);//透明
            panel_1.setOpaque(false);
            
            
            this.setVisible(true);
        }
        
        
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            if(e.getSource()==button_q){
                System.exit(0);
            }
        }
    }
    
    public void playMusic(){
         
            URL cb = null; 
            File f = new File("C:\Users\xiehe\Music\WAV\12.wav"); //引号里面的是音乐文件所在的绝对鹿筋
            try {
                cb = f.toURL();
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            AudioClip aau; 
            aau = Applet.newAudioClip(cb); 
            //aau.play();
            aau.loop();
            //循环播放 aau.play() 单曲 aau.stop()停止播放     
    }
    public static void main(String[] args) {
            new Password_Lock();
        }
    }

  • 相关阅读:
    c++之运算符
    C++开源库,欢迎补充。
    C++ 图像处理类库
    C++开源代码项目汇总
    视频会议及流媒体十大开源项目
    多媒体的框架
    C++开发资源汇总
    Juce之旅-第一个例子(图形窗口)
    Juce-强大的开源类库
    图像处理库的比较:OpenCV,FreeImage,CImg,CxImage
  • 原文地址:https://www.cnblogs.com/he-shao/p/5116183.html
Copyright © 2020-2023  润新知