Java面向对象程序设计课后答案(5)

2019-03-04 12:45

{ Button button; TextField text; Mypanel()

{ button=new Button(" "); text=new TextField(12); add(button); add(text);

button.addActionListener(this); }

public void actionPerformed(ActionEvent e) { String name=text.getText(); if(name.length()>0) button.setLabel(name); validate(); } }

class WindowPanel extends Frame { Mypanel panel1,panel2; WindowPanel() { panel1=new Mypanel(); panel2=new Mypanel(); panel1.setBackground(Color.red); panel2.setBackground(Color.blue); add(panel1,BorderLayout.SOUTH); add(panel2,BorderLayout.NORTH); setSize(300,320); setVisible(true);

addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); validate(); } }

7.参见10.13, 参照本章例子10.21。 8.

import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Xiti8

{ public static void main(String args[]) { MoveFrame f=new MoveFrame(); f.setBounds(12,12,300,300);

f.setVisible(true);

f.setTitle("移动"); f.validate();

f. addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } }

class MoveFrame extends JFrame implements ActionListener { JButton controlButton,movedButton; public MoveFrame()

{ controlButton=new JButton("单击我运动另一个按钮"); controlButton.addActionListener(this); movedButton=new JButton();

movedButton.setBackground(new Color(12,200,34)); setLayout(null); add(controlButton); add(movedButton);

controlButton.setBounds(10,30,130,30); movedButton.setBounds(100,100,10,10); }

public void actionPerformed(ActionEvent e) { int x=movedButton.getBounds().x; int y=movedButton.getBounds().y; x=x+5; y=y+1;

movedButton.setLocation(x,y); if(x>200) { x=100; y=100; } } } 9.

import java.awt.*; import java.awt.event.*; public class Xiti9

{ public static void main(String args[])

{ Win win=new Win(); } }

class Win extends Frame implements KeyListener { Button b[]=new Button[8]; int x,y; Win()

{ setLayout(new FlowLayout()); for(int i=0;i<8;i++)

{ b[i]=new Button(""+i); b[i].addKeyListener(this); add(b[i]); }

addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e){ System.exit(0); } });

setBounds(10,10,300,300); setVisible(true); validate(); }

public void keyPressed(KeyEvent e) { int moveDistance=1;

Component com=(Component)e.getSource(); int x=(int)com.getBounds().x; int y=(int)com.getBounds().y;

Component component[]=this.getComponents(); if(e.getKeyCode()==KeyEvent.VK_UP) { y=y-moveDistance; com.setLocation(x,y);

Rectangle comRect=com.getBounds(); for(int k=0;k<component.length;k++)

{ Rectangle orthRect=component[k].getBounds();

if(comRect.intersects(orthRect)&&com!=component[k]) { y=y+moveDistance; com.setLocation(x,y); break; } }

if(y<=0) y=10; }

else if(e.getKeyCode()==KeyEvent.VK_DOWN) { y=y+moveDistance; com.setLocation(x,y);

Rectangle comRect=com.getBounds(); for(int k=0;k<component.length;k++)

{ Rectangle orthRect=component[k].getBounds();

if(comRect.intersects(orthRect)&&com!=component[k]) { y=y-moveDistance; com.setLocation(x,y); break; } }

if(y>=300) y=300; }

else if(e.getKeyCode()==KeyEvent.VK_LEFT) { x=x-moveDistance; com.setLocation(x,y);

Rectangle comRect=com.getBounds(); for(int k=0;k<component.length;k++)

{ Rectangle orthRect=component[k].getBounds();

if(comRect.intersects(orthRect)&&com!=component[k]) { x=x+moveDistance; com.setLocation(x,y); break; } }

if(x<=0) x=0; }

else if(e.getKeyCode()==KeyEvent.VK_RIGHT) { x=x+moveDistance; com.setLocation(x,y);

Rectangle comRect=com.getBounds(); for(int k=0;k<component.length;k++)

{ Rectangle orthRect=component[k].getBounds();

if(comRect.intersects(orthRect)&&com!=component[k]) { x=x-moveDistance; com.setLocation(x,y); break; } }

if(x>=300) x=300; } }

public void keyTyped(KeyEvent e) {}

public void keyReleased(KeyEvent e) {} }


Java面向对象程序设计课后答案(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2018年中国家禽养殖行业分析报告-市场深度分析与投资前景研究(目

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: