• java高级程序设计(第二周)


    第二学习了事件处理和Swing组件。并学习了给Swing组件添加事件监听器。我学习的是组合框JComboBox类。在创建组合框时可用构造方法JComboBox(Object[] items)直接初始化组合框包含的选项,还可以使用setModel(ComBoxModel aModel)初始化和addItem(Object item)和insertItemAt(Object item, int index)等方法添加或插入选项。用到的方法有:
    addItem(Object item);向选项列表尾部添加选项。
    insertItemAt(Object item, int index);向列表指定位置添加选项,索引值从0开始计数。
    removeItem(Object item, int index)/removeItem(int index);从选项列表中移除指定的选项,分别按对象名和索引值移除。
    removeAllItems();移除选项列表中所有选项。
    setSelectedItem(Object item)/setSelectedItem(int index);将指定的选项设置为组合框默认选项,分别按照对象和索引值移除。
    setMaximumRoxCount(int count);设置选项最大的显示行数,默认为八行。
    setEditable(Boolean isEdit);设置选泽框可否编辑,默认为不可编辑,即false.
    添加组合框后在其下添加事件监听器并有相应时间响应。
    package good;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JTextField;
    import javax.swing.border.EmptyBorder;

    public class good {

    public good()
    {
        makeFrame();
    }
    
     private void makeFrame()
        {
            
            
    
            
            
            final String username ="王伟华";
            final String username1 ="林妹妹";
            final String username2 ="111";
    		final String passwrod = "111";
    		final JFrame jFrame = new JFrame("信息匹配");
    		JDialog jd = new JDialog(jFrame, "登陆成功");
    		JPanel contentPane1 = (JPanel)jFrame.getContentPane();
    	    contentPane1.setBorder(new EmptyBorder(6, 6, 6, 6));
    		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    		jFrame.setBounds(((int)dimension.getWidth() - 200) / 2, ((int)dimension.getHeight() - 300) / 2, 250, 200);
    		jFrame.setResizable(false);
    		jFrame.setLayout(null);
    		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		
    		String[] likes = {"王伟华","林妹妹","111","222"};
    		JComboBox comboBox = new JComboBox(likes);
    		comboBox.setEditable(true);
    		comboBox.setMaximumRowCount(6);
    		comboBox.setBounds(80, 25, 130, 20);
    		jFrame.add(comboBox);
    		comboBox.addActionListener(new ActionListener(){
    
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				JComboBox comboBox = (JComboBox)e.getSource();
    		        String petName = (String)comboBox.getSelectedItem();
    		        String petName1 = (String)comboBox.getToolTipText();
    		        updateLabel(petName);
    				System.out.println(petName); 
    				System.out.println(petName1);
    			}
    
    			public void updateLabel(String petName) {
    				// TODO Auto-generated method stub
    				
    			}});
    		
    		String petName = (String)comboBox.getSelectedItem();
            updateLabel(petName);
    		
    		
    		
    		JLabel label1 = new JLabel("姓名");
    		label1.setBounds(30, 20, 100, 30);
    		jFrame.add(label1);
    		
    		JLabel label2 = new JLabel("密码");
    		label2.setBounds(30, 50, 100, 30);
    		jFrame.add(label2);
    		
    		
    		
    		final JPasswordField text2 = new JPasswordField("111");
    		text2.setBounds(80, 55, 130, 20);
    		jFrame.add(text2);
    		
    		JButton button = new JButton("登陆");
    		 button.setBackground(Color.gray);
    		button.setBounds(40, 95, 170, 50);
    		button.addActionListener(new ActionListener() {
    			
    			@SuppressWarnings("deprecation")
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				  if(       (username.equals(petName)&&passwrod.equals(text2.getText()))
    						  ||(username1.equals(petName)&&passwrod.equals(text2.getText()))
    						  ||(username2.equals(petName) &&passwrod.equals(text2.getText()))) {
    				
    				
    				
    						                                                              
    					JOptionPane.showMessageDialog(null, "正确", "提示", JOptionPane. INFORMATION_MESSAGE);
    				} else {
    					JOptionPane.showMessageDialog(null, "错误", "提示", JOptionPane.ERROR_MESSAGE);
    					
    					text2.setText("");
    				}
    			}
    		});
    		jFrame.add(button);
    		jFrame.setVisible(true);     
          
        }
    
    
    private void updateLabel(String petName) {
    	// TODO Auto-generated method stub
    	
    }
    
    public static void main(String[] args) {
    	// TODO Auto-generated method stub
        new good();
    }
    

    }

  • 相关阅读:
    JavaScript基础(13_宿主对象DOM)
    JavaScript基础(12_正则表达式)
    JavaScript基础(11_字符串的基本操作)
    JavaScript基础(10_call()方法与apply()方法)
    JavaScript基础(09_数组(Array))
    JavaScript基础(08_垃圾回收)
    c# tolist() 浅析
    技术人员,你的表达能力怎样?
    jquery中的$("#id")与document.getElementById("id")的区别
    MVC Filter验证登录
  • 原文地址:https://www.cnblogs.com/2014330122wwh/p/5292717.html
Copyright © 2020-2023  润新知