您的位置:首页 > 编程语言 > Java开发

《Java高级程序设计》第二周作业

2016-03-18 19:09 387 查看
第二周我主要学习了组合框的使用方法。

一、学习内容

  1.定义:组合框(JComboBox)是文本框和列表的组合,可以在文本框中输入选项,也可以单击下拉按钮从显示的列表中进行选择。

  2.组合框的常用构造方法:

 JComboBox():建立一个没有选项的JComboBox对象。

  JComboBox(JComboBoxModel aModel):用数据模型建立一个JComboBox对象。

  JComboBox(Object[]items):利用数组对象建立一个JComboBox对象。

3.组合框的其他常用方法有以下几个:

  addItem(Object obj):向组合框加选项。

  getItemCount():获取组合框的条目总数。

  removeItem(Object ob):删除指定选项。

  removeItemAt(int index):删除指定索引的选项。

  insertItemAt(Object ob,int index):在指定的索引处插入选项。

  getSelectedIndex():获取所选项的索引值(从0开始)。

  getSelectedItem():获得所选项的内容。

  setEditable(boolean b):设为可编辑。组合框的默认状态是不可编辑的,需要调用本方法设定为可编辑,才能响应选择输入事件。

4.在JComboBox对象上发生事件分为两类。一是用户选定项目,事件响应程序获取用户所选的项目。二是用户输入项目后按回车键,事件响应程序读取用户的输入。第一类事件的接口是ItemListener;第二类事件是输入事件,接口是ActionListener。

二、我的程序设计—组合框的使用以及复习上周网格布局

1.程序原代码如下:

import java.awt.Component;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.ButtonGroup;

import javax.swing.JButton;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JRadioButton;

import javax.swing.JTextField;

import javax.swing.border.Border;

import java.util.Vector;

public class Register_GUI {

public Register_GUI(){

RegisterFrame rf=new RegisterFrame();

rf.setVisible(true);

}

public static void main (String args []){

new Register_GUI();

}

}

class RegisterFrame extends JFrame {

private Toolkit tool;

public RegisterFrame (){

setTitle ("用户注册");

tool=Toolkit.getDefaultToolkit();

Dimension ds =tool.getScreenSize();

int w =ds.width;

int h =ds.height;

setBounds ((w-300)/2,(h-300)/2,300,300);

setResizable (false);

RegisterPanel rp =new RegisterPanel(this);

add(rp);

}

}

class RegisterPanel extends JPanel implements ActionLister {

private JLabel

titlelabel,namelabel,pwdlabel,pwdlabel2,sexlabel,agelabel,class label;

private JTextField namefield,agefield;

private JPasswordField pwdfield1,pwdfield2;

private JButton commitbtn,resetbtn,cancelbtn;

private JRadioButton rbtn1,rbtn2;

private JComboBox combo;

private Vectorv;

private GridBagLayout gbl;

private GridBagConstraints gbc;

private JPanel panel;

private Box box;

private JFrame iframe;

public RegisterPanel(JFrame frame){

iframe=frame;

titlelable=new JLabel("用户注册");

titlelabel.setFont(new Font("隶书",Font.BOLD,24));

namelabel=new JLabel("用户名: ");

pwdlabel1=new JLabel("密码: ");

pwdlabel2=new JLabel("确认密码: ");

sexlabel=new JLabel("性别: ");

agelabel=new JLabel("年龄: ");

classlabel=new JLabel("所属班级: ");

namefield=new JTextField(16);

pwdfield1=new JPasswordField(16);

pwdfield1.setEchoChar('');

pwdfield2=new JPasswordField(16);

pwdfield2.setEchoChar('');

agefield=new JTextField(16);

rbtn1=new JRadioButton("男");

rbtn2=new JRadioButton("女");

rbtn1.setSelected(true);

ButtonGroup bg=new ButtonGroup();

bg.add(rbtn1);

bg.add(rbtn2);

v.add("软件英语053");

v.add("软件英语052");

v.add("软件英语051");

v.add("计算机应用051");

v.add("计算机应用052");

combo=new JComboBox(v);

commitbtn=new JButton("注册");

commitbtn.addActionListner(this);

resebtn=new JButton("重置");

resebtn.addActionLitener(this);

cancelbtn=new JButton("取消");

cancelbt.addActionListner(this);

panel=new JPanel();

panel.add(rbtn1);

panel.add(rbtn2);

Border border=BorderFactory.createTitledBorder("");

panel.setBorder(border);

box=Box.createHorizontalBox();

box.add(commitbtn);

box.add(Box.createHorizontalStrut(30));

box.add(resetbtn);

box.add(Box.createHorizontalStrut(30));

box.add(cancelbtn);

gbl=new GridBagLayout();

setLayout(gbl);

gbc=new GridBagConstraints();

addCompnent(titlelabel1,0,0,4,1);

add(Box.createVerticalStrut(20));

gbc.anchor=GridBagContraints.CENTER;

gbc.fill=GridBagConstraints.HORIZONTAL;

gbc.weightx=0;

gbc.weighty=100;

addCompnent(namelabel,0,1,1,1);

addCompnent(namefield,1,1,4,1);

addCompnent(pwdlabel1,0,2,1,1);

addCompnent(pwdfield1,1,2,4,1);

addCompnent(pwdlabel2,0,3,1,1);

addCompnent(pwdfield2,1,3,4,1);

addCompnent(sexlabel,0,4,1,1);

addCompnent(panel,1,4,1,1);

gbc.anchor=GridBagConstraints.EAST;

gbc.fill=GridBagContraints.NONE;

addCompnent(agelabel,2,4,1,1);

gbc.fill=GridBagConstraints.HORIZONTAL;

addCompnent(agefield,3,4,2,1);

gbc.gridheight=h;

add(c,gbc);

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==commitbtn){

Register rinfo=new Register();

rinfo.name=namefield.getText().trim();

rinfo.password=new String(pwdfield1.getPassword());

rinfo.sex=rbtn1.isSelected()?"男":"女";

rinfo.age=agefield.getText().trim();

rinfo.nclass=combo.getSelectedItem().toString();

if(rinfo.name.length()==0){

JOptionPane.showMessageDialog(null,"\t用户名不能为空");

return;

}

if(rinfo.password.length()==0){

JOptionPane.showMessageDialog(null,"\t密码不能为空");

return;

}

if(!rinfo.password.equals(new String(pwdfield2.getPassword()))){

JOptionPane.showMessageDialog(null,"密码两次输入不一致,请重新输入");

return;

}

if(rinfo.age.length()==0){

JOptionPane.showMessageDialog(null,"\t年龄不能为空");

return;

}

int age=Integer.parseInt(rinfo.age);

if(age<=Ollage>100){

JOptionPane.showMessageDialog(null,"\年龄输入不合法");

return;

}

JOptionPane.showMessageDialog(null,"\t注册成功!");

}

if(e.getSource()==resebtn){

namefield.setText("");

pwdfield1.setText("");

pwdfield2.setText("");

rbtn1.isSelected();

agefield.setText("");

combo.setSelectedIndex(0);

}

if(e.getSource()==cancelbtn){

iframe.dispose();

}

}

}

class Register{

String name;

String password;

String sex;

String age;

String nclass;

}

2.程序运行效果图




三、个人照片

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: