Exercises 2.7 Fill in theblanks in each of the following statements: a) Comments areused to document a program and improve its readability. b) A decision can be madein a Java program with a(n) if statement. c) Calculations are normallype
1. 创建一用户界面窗口有两栏,其中一栏显示"北京","大连","武汉","上海","杭州","南京","广州","昆明",您可以从中选择您最喜欢的城市,另一栏显示六种品牌的电脑"宏基 "、"NEC"、"联想"、"北大方正"、"DELL"、"Applet"。这两个列表可以实现单选和多选,Shift+鼠标点击项可以进行列表数据项的连续选择,Ctrl+鼠标点击项可以进行列表数据项的不连续选项。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
public class Eg_1 {
JFrame f;
public Eg_1(){
f=new JFrame();
Container con=f.getContentPane();
con.setLayout(new GridLayout(1,2));
1.
String s[]={"11111","22222","33333","44444","55555",
"66666666","77777","888","9999999"};
JList list1=new JList(s);
con.add(new JScrollPane(list1));
//2
Vector v=new Vector();//向量----可变长度的数组
v.addElement("aaaaaaaa");
v.addElement("bbbba");
v.addElement("ccccccccc");
v.addElement("ddddddd");
v.addElement("aaaaaaaa");
v.addElement("bbbba");
v.addElement("ccccccccc");
v.addElement("ddddddd");
JList list2=new JList(v);
con.add(new JScrollPane(list2));
f.setSize(400,300);
f.setVisible(true);
}
public static void main(String args[]) {
new Eg_1();
}
}
2设计一个库存查询窗口,要求能够在选择商品种类的同时,在窗口中显示该商品的价格和数量。商品有"色拉油","齐心汽水","米酒","冰淇淋","蛋糕",相对应的价格和数量是:{"56"、"8"、"10"、"20"、"90"},{"232"、"50"、"109"、"48"、"30"}。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Vector;
public class Eg_2 extends JApplet implements ItemListener{
//声明类,因为是小应用程序,故需要继承父类JApplet,并声明接口
Container ctp;
//容器
JTextField tf1;
JTextField tf2=new JTextField(5);//创建两个文本框类实例tf1
JLabel lb1=new JLabel("单价:"),
lb2=new JLabel("库存量:");//创建两个标签类实例tf2
String obj[]={"色拉油","齐心汽水","米酒","冰淇淋","蛋糕"},//定义三个数组,并付初始值
price[]={"56","8","10","20","90"},
num[]={"232","50","109","48","30"};
JComboBox cbx=new JComboBox(); //创建弹出式列表实例cbx
public void init(){ //写小应用程序的初始化方法init()
ctp=this.getContentPane();
ctp.setLayout(new FlowLayout()); //设置布局管理器-流布局管理器
tf1=new JTextField(5);
for(int n=0;n
cbx.addItem(obj[n]);
ctp.add(cbx);
cbx.addItemListener(this); //为cbx安装监视器,this是指类自己本身
ctp.add(lb1);
ctp.add(tf1);
ctp.add(lb2);
ctp.add(tf2);
} //将标签、文本框、加到内容面板上
public void itemStateChanged(ItemEvent e){
int x=0,y;
String ko=(String)e.getItem();
for(y=0;y
if(ko==obj[y])
x=cbx.getSelectedIndex();
tf1.setText(price[x]);
tf2.setText(num[x]);
}
}
3 请你为单位设计一个“图书资料借用系统”。系统要求:借用图书资料的科室人员必须详细填写职工编号、图书资料名称、借书日期、还书日期、备注。(该例题形式常在网络问卷或会员注册中见到)。
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class Eg_3 implements ActionListener{
JFrame f = null;
Exercises 1.4Fill in the blanks in each of the following statements: a) The logical unit that receives information from outside thecomputer for use by thecomputer is theinput unit. b) The process of instructing the computer to solve a probl
public void actionPerformed(ActionEvent e) { //实现ActionListener类接口的方法
String cls = e.getActionCommand(); //获得事件按钮的内容赋给字符串变量cls
//得到按钮上的名称
if (cls.equals("借阅图书资料")) { //将cls的内容与字符串"借阅图书资料"比较
new LendingSystem(f); //如果相同,就创建LendingSystem类的匿名实例
}
else if (cls.equals("关闭系统")) { //否则就关闭系统窗口
System.exit(0);
}
}
public Eg_3(){ // 构建BookdataDialog类的构造方法
f = new JFrame("图书资料借阅系统");
Container contentPane = f.getContentPane();
JPanel buttonPanel = new JPanel();
JButton b = new JButton("借阅图书资料");
b.addActionListener(this); //为按钮实例b安装监视器
buttonPanel.add(b); //将按钮实例b加到面板容器中
b = new JButton("关闭系统");
b.addActionListener(this);
buttonPanel.add(b);
buttonPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.red,8),
"图书资料借阅系统",TitledBorder.CENTER,TitledBorder.TOP));
contentPane.add(buttonPanel,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
public static void main(String[] args){
new Eg_3();
}
}
class LendingSystem implements ActionListener{ //声明LendingSystem类和接口
JTextField staffField,
objectField,
borrowDateField,
returnDateField,
reasonField;
JDialog dialog; //声明对话框对象
public void actionPerformed(ActionEvent e) { //实现接口的方法
String cmd = e.getActionCommand();
if (cmd.equals("确定")) { }
else if(cmd.equals("取消")){
dialog.dispose();
}}
LendingSystem(JFrame f){ // 实现LendingSystem类的构造方法
dialog = new JDialog(f,"借用图书资料",false); //创建对话框实例
GridBagConstraints c; //创建容器对象c
//往袋布局限制类
int gridx,gridy,//网格坐标
gridwidth,gridheight,//所占网格的个数
anchor,//锚点
fill,//填充
ipadx,ipady;//定义整型数据变量
double weightx,weighty; //是否可以放大---------不为0
Insets inset; //创件插件类对象
GridBagLayout gridbag = new GridBagLayout(); //创建网袋布局管理器实例
Container dialogPane = dialog.getContentPane();
dialogPane.setLayout(gridbag);
///L1
JLabel label = new JLabel("图书资料编号 : "); //创建标签实例
gridx=0; //第0列
gridy=0; //第0行
gridwidth = 1; //占一个单位宽度
gridheight = 1; //占一个单位高度
weightx = 0; //窗口增大时组件宽度增大比率0
weighty = 0; //窗口增大时组件高度增大比率0
anchor = GridBagConstraints.CENTER; //容器大于组件size时将组件置于容器中央
fill = GridBagConstraints.BOTH; //窗口拉大时会填满水平与垂直空间
inset = new Insets(0,0,0,0); //组件间间距
ipadx = 0; //组件内水平宽度
ipady = 0; //组件内垂直高度
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(label,c);
dialogPane.add(label);
///L2
label = new JLabel("图书资料名称 : ");
gridx=3;
gridy=0;
c = new GridBagConstraints(gridx,gridy,gridwidth,gridheight,
weightx,weighty,anchor,fill,inset,ipadx,ipady);
gridbag.setConstraints(label,c);
dialogPane.add(label);
L3