java练习7

第十三章

1.尝试开发一个窗体,如图13.20所示

public class exercise extends JFrame {	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox<String> my=new JComboBox<>(new MyComboBox());
	//private JPanel p1=new JPanel(new GridLayout(1,2,10,10));
	 JPanel p1=new JPanel();
	 JCheckBox jc1=new JCheckBox("男");
	 JCheckBox jc2=new JCheckBox("女");
	 JPanel p2=new JPanel();
	 JButton jb1=new JButton("确定");
	 JButton jb2=new JButton("取消");	 
    public  exercise() {
    	//JFrame jf=new JFrame(title);
    	Container co=getContentPane();
    	setVisible(true);
    	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    	setSize(new Dimension(400, 200));
    	//co.setLayout(new FlowLayout());
    	co.setLayout(new BorderLayout());
    	co.add(BorderLayout.NORTH,my);
    	p1.add(jc1);
    	p1.add(jc2);
    	p2.add(jb1);
    	p2.add(jb2);
    	co.add(BorderLayout.CENTER, p1);
    	co.add(BorderLayout.SOUTH, p2);
    }
	public static void main(String[] args) {
		new exercise();
		}
}

class MyComboBox extends AbstractListModel <String> implements ComboBoxModel <String>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    String test[]= {"红","白","黄","绿","蓝"};
    String item;
	@Override
	public String getElementAt(int arg0) {
		return test[arg0];
	}

	@Override
	public int getSize() {
		return test.length;
	}

	@Override
	public Object getSelectedItem() {

		return item;
	}

	@Override
	public void setSelectedItem(Object arg0) {
		item=(String) arg0;
	}
	
}

2.尝试创建一个窗体,选择合适的布局管理器,并在窗体中设置一个下拉列表框,初始状态下拉列表框中没有项目,并设一个按钮,为按钮设置动作事件监听器,单击时,添加数组内容。

public class exercise extends JFrame implements ActionListener{	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox<String> my=new JComboBox<>();
	//private JPanel p1=new JPanel(new GridLayout(1,2,10,10));
	 //JPanel jc =new JPanel();
	String[] tp={"111","222","333"};
	int tp1=0;
	JButton b1=new JButton("add");
    public  exercise() {
    	//JFrame jf=new JFrame(title);
    	Container co=getContentPane();
    	setVisible(true);
    	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    	setSize(new Dimension(400, 200));
    	//co.setLayout(new FlowLayout());
    	co.setLayout(new BorderLayout());
    	co.add(BorderLayout.NORTH,my);
    	co.add(BorderLayout.SOUTH,b1);
    	b1.addActionListener(this);
    }
    
	public static void main(String[] args) {
		new exercise();
		}

	@Override
	public void actionPerformed(ActionEvent e) {
		//b1.setText("111");
		//my.setSelectedItem("111")
		//my.addItem("111");
        if (tp1<tp.length) {
        	my.addItem(tp[tp1++]);
        }        
	}
}

3.开发一个登录窗体

public class exercise extends JFrame {	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JComboBox<String> my=new JComboBox<>();
	//private JPanel p1=new JPanel(new GridLayout(1,2,10,10));
	 //JPanel jc =new JPanel();
	JButton b1=new JButton("确定");
	JButton b2=new JButton("重置");
	JLabel jl1=new JLabel("用户名");
	JLabel jl2=new JLabel("密码");
	JTextField jt1=new JTextField();
	JPasswordField jt2=new JPasswordField();
    public  exercise() {
    	//JFrame jf=new JFrame(title);
    	Container co=getContentPane();
    	setVisible(true);
    	setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    	setSize(new Dimension(400, 200));
    	//co.setLayout(new FlowLayout());
    	co.setLayout(new GridLayout(3,3,5,5));
    	//co.add(BorderLayout.NORTH,my);
    	//co.add(BorderLayout.SOUTH,b1);
    	//b1.addActionListener(this);    	
    	co.add(jl1);
    	co.add(jl2);
    	co.add(jt1);
    	co.add(jt2);
    	co.add(b1);
    	co.add(b2);
    	
    	b1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				String tp1=jt1.getText();
				String r1="mr";
				//char[] tp2=jt2.getPassword();
				if((tp1.trim().equals(r1))&& new String(jt2.getPassword()).trim().equals("mrsoft")) {
					JOptionPane.showMessageDialog(null, "登录成功");
				}else {
					JOptionPane.showMessageDialog(null, "用户名或密码错误");
				}
			}   		
    	});
    }
    
	public static void main(String[] args) {
		new exercise();
		}


}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值