云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > Java2实用教程(第三版)课后答案 - 耿祥义主编

Java2实用教程(第三版)课后答案 - 耿祥义主编

  • 62 次阅读
  • 3 次下载
  • 2025/5/8 0:47:49

panel=new Mypanel();

box=new Checkbox(\我是选择框\choice=new Choice();

choice.add(\我是下拉列表\list=new List(3,false); list.add(\我是滚动列表\label=new Label(\我是标签\can=new Mycanvas();

buttonBackColor=new Button(\背景色\buttonForeColor=new Button(\前景色\setLayout(new FlowLayout()); add(button); add(textfield); add(textarea); add(panel); add(box); add(choice); add(list); add(label); add(can);

add(buttonBackColor); add(buttonForeColor);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0); } } );

buttonBackColor.addActionListener(this); buttonForeColor.addActionListener(this); setBounds(100,100,300,300); setVisible(true); validate(); }

public void actionPerformed(ActionEvent e) {

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

button.setBackground(Color.yellow); textfield.setBackground(Color.yellow); textarea.setBackground(Color.yellow);

33

panel.setBackground(Color.yellow); box.setBackground(Color.yellow); choice.setBackground(Color.yellow); list.setBackground(Color.yellow); label.setBackground(Color.yellow); can.setBackground(Color.yellow); }

else if(e.getSource()==buttonForeColor) {

button.setForeground(Color.blue); textfield.setForeground(Color.blue); textarea.setForeground(Color.blue); panel.setForeground(Color.blue); box.setForeground(Color.blue); choice.setForeground(Color.blue); list.setForeground(Color.blue); label.setForeground(Color.blue); can.setForeground(Color.blue); } } }

class Mycanvas extends Canvas {

Mycanvas(){ }

public void paint(Graphics g) {

g.drawString(\我是画布\} }

class Mypanel extends Panel {

Button button1; Mypanel() {

button1=new Button(\我是面板\add(button1); } }

public class Test {

public static void main(String args[]) { new WindowColor(); }

34

}

12.编写应用程序,有一个标题为“移动”的窗口,窗口的布局为null,在窗口中有两个按钮,单击一个按钮让另一个按钮移动。 答: import java.awt.*; import java.awt.event.*;

class WindowMove extends Frame implements ActionListener {

Button button1,button2; WindowMove(String s) {

super(s);

setLayout(null);

button1=new Button(\我让它横向走动\button2=new Button(\我让它纵向走动\button1.setBackground(Color.blue); button2.setBackground(Color.green); button1.addActionListener(this); button2.addActionListener(this);

button1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); button2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); add(button1); add(button2);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0); } } );

button1.setBounds(20,80,100,30); button2.setBounds(100,180,100,30);

setBounds(100,100,500,500); setVisible(true); validate(); }

public void actionPerformed(ActionEvent e) {

Rectangle rect1=button1.getBounds(); int x1=(int)rect1.getX(); int y1=(int)rect1.getY();

Rectangle rect2=button2.getBounds(); int x2=(int)rect2.getX();

35

int y2=(int)rect2.getY(); if(e.getSource()==button1) {

x2=x2+5;

button2.setLocation(x2,y2); }

else if(e.getSource()==button2) {

y1=y1+5;

button1.setLocation(x1,y1); } } }

public class Test {

public static void main(String args[]) {

new WindowMove(\移动\} }

13.编写应用程序,有一个标题为“改变颜色”的窗口,窗口的布局为null,在窗口中有3个按钮和一个画布,3个按钮的颜色分别是红、绿、蓝。单击相应的按钮,画布绘制相应颜色的圆。 答: import java.awt.*; import java.awt.event.*;

class WindowChangeColor extends Frame implements ActionListener {

Button buttonRed,buttonGreen,buttonBlue; Mycanvas canvas;

WindowChangeColor(String s) {

super(s);

setLayout(null);

buttonRed=new Button(\红色\

buttonGreen=new Button(\绿色\buttonBlue=new Button(\蓝色\canvas=new Mycanvas();

buttonRed.setBackground(Color.red);

buttonGreen.setBackground(Color.green); buttonBlue.setBackground(Color.blue); add(canvas);

canvas.setBounds(10,10,150,150); add(buttonRed);

36

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

panel=new Mypanel(); box=new Checkbox(\我是选择框\choice=new Choice(); choice.add(\我是下拉列表\list=new List(3,false); list.add(\我是滚动列表\label=new Label(\我是标签\can=new Mycanvas(); buttonBackColor=new Button(\背景色\buttonForeColor=new Button(\前景色\setLayout(new FlowLayout()); add(button); add(textfield); add(textarea); add(panel); add(box); add(choice); add(list); add(label); add(can);

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com