第二次作业

JComboBox的使用方法:
JComboBox是swing种的下拉菜单控件。最常使用的函数应该是addItem()方法,可以为该控件添加一个个下拉选项。

然后要处理选中某个选项的事件,可以添加ItemListener监听器。

[java] view plain copy
comboBox1.addItemListener(new java.awt.event.ItemListener(){
public void itemStateChanged(java.awt.event.ItemEvent e){
func(e);//处理事件的函数
}
});

但是有可能会对一个选择事件处理两次。假设func(e)函数中输出a,则在一次选择后,会发现输出了aa,这是因为itemStateChanged()函数与itemState有关,而itemState状态有两个:Selected和Unselected。即在一次选择的过程中,实际出发了两个事件,unselect上一次选中的事件,select本次选中的事件。

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class log {
public JFrame frame;
private JTextField log_text;
private JTextField name_text;
public log() {
makeframe();
}
public void makeframe(){
frame =new JFrame("登录程序");
Container contentpane=frame.getContentPane();

 JPanel northPanel = new JPanel(new GridLayout(7, 1));
 JPanel north1=new JPanel(new FlowLayout());
 
 JPanel north2=new JPanel(new FlowLayout());
 JPanel north3=new JPanel(new FlowLayout());
JTextField Name_text = new JTextField(16);
	JTextField Log_text = new JTextField(16);
  JLabel Name=new JLabel("用户名");
//  contentpane.add(Name,BorderLayout.NORTH);
 JLabel  Log=new JLabel("登录 密码");
 north1.add(Name);
 north2.add(Log);
 north1.add(Name_text);
 north2.add(Log_text);
 JButton yes=new JButton("确定");
 JButton no=new JButton("取消");
 north3.add(yes);
 north3.add(no);
 //contentpane.add(Log,BorderLayout.SOUTH);
  contentpane.add(northPanel);
 northPanel.add(north1);
 northPanel.add(north2);
  northPanel.add(north3);
  
  String[] list={"尹鑫磊","S卞","B琛"};
  JComboBox ComboBox=new JComboBox(list);
  ComboBox.addActionListener(new ActionListener(){

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		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
	}});
  yes.addActionListener(new ActionListener(){

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		JOptionPane.showMessageDialog(frame, "正确", "登陆成功", JOptionPane.INFORMATION_MESSAGE);
    
	}});
  no.addActionListener(new ActionListener(){

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		System.exit(0);
	}});
  
  north1.add(ComboBox);
  
  
 frame.setVisible(true);
 Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
 frame.setLocation(d.width/2 - frame.getWidth()/2, d.height/2 - 
 		frame.getHeight()/2);
 frame.pack();

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new log();
}

}

posted on 2016-03-18 22:35  尹鑫磊  阅读(139)  评论(1编辑  收藏  举报