博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

自考 课后题 第六章6

Posted on 2012-08-08 09:07  紫冰龙  阅读(123)  评论(0编辑  收藏  举报
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exec65 extends JFrame implements ItemListener{

    JComboBox box;
    JTextField text;
    static String[] lessons = {"语文","数学","化学","物理"};
    public static void main(String[] args) {
        new Exec65();

    }
    public Exec65(){
        this.setTitle("Test");
        this.setSize(500,500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        
        
        
        box = new JComboBox(lessons);
        box.setEditable(true);
        box.addItemListener(this);
        text = new JTextField(10);
        
        add(box,BorderLayout.NORTH);
        add(text,BorderLayout.CENTER);
        
        this.setVisible(true);
        pack();
    }
    @Override
    public void itemStateChanged(ItemEvent e) {
        text.setText(((JComboBox)(e.getSource())).getSelectedItem().toString());
        
    }

}