Swing程序设计实践与练习1

public class asd extends JFrame{
    public asd() {
        // TODO Auto-generated constructor stub
        Container c = getContentPane();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
        
        
        JComboBox<String> jc = new JComboBox<>(new MyComboBox());
        jp1.add(jc);
        
        
        JRadioButton jr1 = new JRadioButton("男");
        JRadioButton jr2 = new JRadioButton("女");
        ButtonGroup group = new ButtonGroup();
        group.add(jr1);
        group.add(jr2);
        jp2.add(jr1);
        jp2.add(jr2);
        
        
        JButton jb1 = new JButton("确定");
        JButton jb2 = new JButton("取消");
        jp3.add(jb1);
        jp3.add(jb2);
        
        c.add(jp1);
        c.add(jp2);
        c.add(jp3);
        setSize(300,300);
        setLayout(new GridLayout(3,1,5,5));
        setVisible(true);
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new asd();
    }
}
public class MyComboBox extends AbstractListModel<String> implements ComboBoxModel<String>{

    private static final long serialVersionUID = 1L;
    String selceteditem = null;
    String[] test = {"红","蓝","白","黑"};
    public int getSize() {
        // TODO Auto-generated method stub
        return test.length;
    }

    @Override
    public String getElementAt(int index) {
        // TODO Auto-generated method stub
        return test[index];
    }

    @Override
    public void setSelectedItem(Object anItem) {
        // TODO Auto-generated method stub
        selceteditem = (String)anItem;
    }

    @Override
    public Object getSelectedItem() {
        // TODO Auto-generated method stub
        return selceteditem;
    }
    
    public int getInex() {
        // TODO Auto-generated method stub
        for(int i=0;i<test.length;i++){
            if(test[i].equals(getSelectedItem()))
                return i;
        }
        return 0;
    }
    
}

猜你喜欢

转载自www.cnblogs.com/dulute/p/10587940.html
今日推荐