十三、常用组件之二 _______单行文本框与多行文本框,单选框和复选框
1. 判断:列表对象总包含滚动条。()
2.判断:面板的默认管理器是BorderLayout。
3.____________类用于创建一组单选按钮。
4. 欲编写如下图的一个界面,用于显示用户指定的图像: 如果在区域A中只能放置一个
AWT组件,从各组件的本来功能角度考虑,最好使用哪种组件:
A、 TextArea
B、 Panel
C、 Applet
D、 Canvas
5.Java应用程序中的默认外观是_________。
A.Motif
B.Windows
C.METAl
6. 创建下面的GUI,要求在文本框中输入分数,单击“求和”按钮后在结果文本框中显示总分。
7.编写一个将华氏温度转换为摄氏温度的程序。应从键盘输入华氏温度,然后通过文本显示转换后的摄氏温度。使用下面的公式进行温度转换:
摄氏温度=5/9Χ(华氏温度-32)
8.编写一个程序,使用户能够使用鼠标在applet中绘制一个矩形。按住鼠标左键,确定矩形的左上角,然后拖动鼠标,在需要的位置(即矩形右下角)释放鼠标。另外,在状态栏中显示矩形面积。
参考答案
1.false
2.true
3. CheckboxGroup
4.D
5. C
6. 参考程序如下:
import javawt.*;
import javawt.event.*;
public class Chp9_3_C extends Frame implements ActionListener{
TextField textField1,textField2,textField3;
Button button1=new Button("求和");
Panel panel1,panel2;
public Chp9_3_C(){
textField1=new TextField();
textField2=new TextField();
textField3=new TextField();
panel1=new Panel();
panel2=new Panel();
panel1.setLayout(new GridLayout(3,2));
panel2.setLayout(new FlowLayout());
panel1.add(new Label("数学: "));
panel1.add(textField1);
panel1.add(new Label("英语: "));
panel1.add(textField2);
panel1.add(new Label("总分: "));
panel1.add(textField3);
panel2.add(button1);
add(panel1,BorderLayout.CENTER);
add(panel2,BorderLayout.SOUTH);
button1.addActionListener(this);
setSize(300,200);
setVisible(true);
}
public static void main(String[] args){
new Chp9_3_C();
}
public void actionPerformed(ActionEvent e){
int n1,n2,sum;
n1=Integer.parseInt(textField1.getText());
n2=Integer.parseInt(textField2.getText());
sum=n1+n2;
textField3.setText(""+sum);
}
}
6.参考程序如下:
import javawt.*;
import javawt.event.*;
public class Chp9_3_D extends Frame{
TextField textField1,textField2;
Button button1;
public Chp9_3_D(){
textField1=new TextField(30);
textField2=new TextField(30);
button1=new Button("转换");
setLayout(new FlowLayout());
add(new Label("华氏温度:"));
add(textField1);
add(new Label("摄氏温度:"));
add(textField2);
textField2.setEditable(false);
add(button1);
setSize(400,300);
pack();
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
button1_actionPerformed(e);
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args){
new Chp9_3_D().setVisible(true);
}
private void button1_actionPerformed(ActionEvent e){
double d=Double.parseDouble(textField1.getText());
double h=5.0/9.0*(d-32);
textField2.setText(""+h);
}
}
7.参考程序如下:
import javapplet.Applet;
import javawt.*;
import javawt.event.*;
public class Chp9_3_E extends Applet implements MouseListener,MouseMotionListener{
int x1,y1,x2,y2;
public void init(){
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g){
g.drawRect(x1,y1,x2,y2);
int area=Math.abs(x2-x1)*Math.abs(y2-y1);
showStatus("矩形面积: "+area);
}
//implementation of MouseListener
public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();
}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
//implementation of MouseMotionEvent
public void mouseDragged(MouseEvent e){
x2=e.getX();
y2=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e){}
}
责任编辑:小草