Java技术————创建窗口_第1页
Java技术————创建窗口_第2页
Java技术————创建窗口_第3页
Java技术————创建窗口_第4页
Java技术————创建窗口_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import javax.swing.AbstractListModel;import javax.swing.ButtonGroup;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.WindowConstants;public class Register extends JFrame implements ActionListener, FocusListener private static final String ADD = ADD; private static final String CANCEL = ADD; private static final long serialVersionUID = 4573735051740981321L; private JRadioButton man; private JRadioButton woman; private JTextField name_jf; private JPasswordField password1_jf; private JPasswordField password2_jf; private JComboBox education_cb; private JList hobby_jl; private JButton submit; private JButton cancel; private JPanel panel1; String str=大学,专科,高中; String str1=跑步,看书,打球; private ButtonGroup bg=new ButtonGroup(); public static void main(String args) new Register(); public Register() Container con=getContentPane(); setTitle(注册); setLayout(new GridLayout(10,8,8,8); con.add(new JLabel(用户名:,SwingConstants.CENTER); name_jf=new JTextField(8); con.add(name_jf); con.add(new JLabel(密码:,0); password1_jf=new JPasswordField(18); con.add(password1_jf); con.add(new JLabel(确认密码:,0); password2_jf=new JPasswordField(18); con.add(password2_jf); password2_jf.addFocusListener(this); con.add(new JLabel(性别:,0); woman = new JRadioButton(); man = new JRadioButton(); bg.add(man); bg.add(woman); woman.setSelected(true); woman.setText(女); man.setText(男); panel1=new JPanel(); panel1.add(woman); panel1.add(man); con.add(panel1); con.add(new JLabel(学历:,0); education_cb = new JComboBox(str); con.add(education_cb); con.add(new JLabel(爱好:,0); hobby_jl = new JList(str1); con.add(hobby_jl); con.add(getSubmit(); con.add(getCancel(); setSize(500, 600); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); private JButton getCancel() if (cancel = null) cancel = new JButton(); cancel.setText(取消); cancel.setActionCommand(CANCEL); cancel.addActionListener(this); return cancel; private JButton getSubmit() if (submit = null) submit = new JButton(); submit.setText(注册); submit.setActionCommand(ADD); submit.addActionListener(this); return submit; public void focusGained(FocusEvent e) /password2_jf.requestFocus(); SuppressWarnings(deprecation)public void focusLost(FocusEvent e) JPasswordField text=(JPasswordField)e.getSource(); if(!text.getText().equals(password1_jf.getText() JOptionPane.showMessageDialog(this, 密码不匹配); /password2_jf.requestFocus(); SuppressWarnings(deprecation)public void actionPerformed(ActionEvent e) String action = e.getActionCommand(); System.out.println(action); if (action != null & ADD.equals(action) StringBuffer sb=new StringBuffer(); sb.append(姓名: ).append(name_jf.getText().append(n) .append(密码: ).append(password1_jf.getText().append(n) .append(性别: ); if(woman.isSelected() sb.append(女n); else sb.append(男n); sb.append(学历: ).append(streducation_cb.getSelectedIndex(); sb.append(n); sb.append(爱好: ); int hobby=hobby_jl.getSelectedIndices(); for(int i=0;ihobby.length;i+) sb.append(str1hobbyi+ ); JOptionPane.showMessageDialog(this, sb.toString(); else if (action != null & action.equals(CANCEL) name_jf.setText(); password1_jf.setText(); password2_jf.setText(); woman.setSelected(true); man.setSelected(false); SuppressWarnings(serial)class JListTest extends JFrame public JListTest() Container cp = getContentPane();cp.setLayout(null);JList jl = new JList(new MyListModel();JScrollPane js = new JScrollPane(jl);js.setBounds(10, 10, 100, 100);cp.add(js);setTitle(在这个窗体中使用了列表框);setSize(200, 150);setVisible(true);setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);public static void main(String args) new JListTest();class MyListModel

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论