77范文网 - 专业文章范例文档资料分享平台

《Java2实用教程》课后习题参考答案(8)

来源:网络收集 时间:2019-04-02 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

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) {

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); 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); 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(); } }

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();

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库《Java2实用教程》课后习题参考答案(8)在线全文阅读。

《Java2实用教程》课后习题参考答案(8).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/565581.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: