首页 > Windows开发 > 详细

Swing程序设计实践与练习1

时间:2019-03-24 14:46:12      阅读:128      评论:0      收藏:0      [点我收藏+]
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;
    }
    
}

技术分享图片

Swing程序设计实践与练习1

原文:https://www.cnblogs.com/dulute/p/10587940.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!