当前位置:首页 > Java2实用教程(第三版)课后答案 - 耿祥义主编
setBackground(Color.cyan); }
public void setX(int x) {
this.x=x; }
public void setY(int y) {
this.y=y; }
public void setW(int w) {
this.w=w; }
public void setH(int h) {
this.h=h; }
public void paint(Graphics g) {
g.drawRect(x,y,w,h); } }
class WindowCanvas extends Frame implements ActionListener {
Mycanvas canvas;
TextField text1,text2,text3,text4; Button button; WindowCanvas() {
canvas=new Mycanvas(); text1=new TextField(4); text2=new TextField(4); text3=new TextField(5); text4=new TextField(5);
Panel pNorth=new Panel(),pSouth=new Panel(); button=new Button(\确定\
button.addActionListener(this);
pNorth.add(new Label(\矩形的宽: \pNorth.add(text3);
pNorth.add(new Label(\矩形的高: \pNorth.add(text4);
pSouth.add(new Label(\左上角位置坐标:\
29
pSouth.add(text1); pSouth.add(text2); pSouth.add(button);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); } } );
add(canvas,BorderLayout.CENTER); add(pNorth,BorderLayout.NORTH); add(pSouth,BorderLayout.SOUTH); setBounds(100,100,500,500); setVisible(true); validate(); }
public void actionPerformed(ActionEvent e) {
int x,y,w,h; try {
x=Integer.parseInt(text1.getText()); y=Integer.parseInt(text2.getText());
w=Integer.parseInt(text3.getText()); h=Integer.parseInt(text4.getText()); canvas.setX(x); canvas.setY(y); canvas.setW(w); canvas.setH(h); canvas.repaint(); }
catch(NumberFormatException ee) {
x=0;y=0;w=0;h=0; } } }
public class Test {
public static void main(String args[]) {
30
new WindowCanvas(); } }
10.编写应用程序,有一个窗口对象,该窗口取它的默认布局: BorderLayout布局,北面添加一个List组件,该组件有四个商品名称的选项。中心添加一个文本区,当选择List组件中的某个选项后,文本区显示对该商品的价格和产地:当双击List组件中的某个选项后,文本区显示该商品的详细广告。 答: import java.awt.*; import java.awt.event.*;
class WindowGoods extends Frame implements ActionListener,ItemListener {
String s[]={\产地:北京\产地:上海\产地:沈阳\产地:广东\String p[]={\价格:3200\价格:158\价格:13.2\价格:320/打\String a[]={\本商品****\本商品*****\本商品******\本商品*******\List list;
TextArea text; WindowGoods() {
list=new List(3,false); text=new TextArea(6,20); text.setEditable(false); list.add(\商品1\list.add(\商品2\list.add(\商品3\list.add(\商品4\
add(list,BorderLayout.NORTH); add(text,BorderLayout.CENTER);
list.addItemListener(this); list.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); } } );
setBounds(100,100,300,300); setVisible(true); validate(); }
public void itemStateChanged(ItemEvent e)
31
{
if(e.getItemSelectable()==list) {
int m=list.getSelectedIndex(); text.setText(p[m]+'\\n'+s[m]); } }
public void actionPerformed(ActionEvent e) {
int n=list.getSelectedIndex(); text.setText(a[n]); } }
public class Test {
public static void main(String args[]) {
new WindowGoods(); } }
11.编写程序,观察各种组件设置背景色和前景色的情况。 答: import java.awt.*; import java.awt.event.*;
class WindowColor extends Frame implements ActionListener {
Button button; //按钮
TextField textfield; //文本框 TextArea textarea; //文本区 Mypanel panel; //面板
Checkbox box; //选择框 Choice choice; //下拉列表 List list; //滚动列表 Label label; //标签 Mycanvas can; //画布
Button buttonBackColor,buttonForeColor; WindowColor() {
button=new Button(\我是按钮\
textfield=new TextField(\我是文本框\textarea=new TextArea(6,15); textarea.setText(\我是文本区\textfield.setEditable(false); textarea.setEditable(false);
32
共分享92篇相关文档