内容发布更新时间 : 2024/11/6 3:56:21星期一 下面是文章的全部内容请认真阅读。
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(); 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);
buttonRed.setBounds(10,170,50,30); add(buttonGreen);
buttonGreen.setBounds(70,170,50,30); add(buttonBlue);
buttonBlue.setBounds(130,170,50,30); buttonRed.addActionListener(this); buttonGreen.addActionListener(this); buttonBlue.addActionListener(this); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); } } );
setBounds(100,100,200,250); setVisible(true); validate(); }
public void actionPerformed(ActionEvent e) {
if(e.getSource()==buttonRed) {
canvas.setX(1); canvas.repaint(); }
else if(e.getSource()==buttonGreen) {
canvas.setX(2); canvas.repaint(); }
else if(e.getSource()==buttonBlue) {
canvas.setX(3);
canvas.repaint(); } } }
class Mycanvas extends Canvas { int x=0; Mycanvas() {
setBackground(Color.white); }
public void setX(int y) { x=y; }
public void paint(Graphics g) {
switch(x){ case 1:
g.setColor(Color.red); break; case 2:
g.setColor(Color.green); break; case 3:
g.setColor(Color.blue); break; default:
g.setColor(Color.white); }
g.drawOval(65,65,50,50); } }
public class Test {
public static void main(String args[]) {
new WindowChangeColor(\改变颜色\
} }
14.编写应用程序,测试Cursor类中表示鼠标形状的静态常量。 答: import java.awt.*; import java.awt.event.*;
class WindowCursor extends Frame implements ActionListener
{
Button button,button1; TextField text; int n=-1; WindowCursor() {
button=new Button(\单击我\
text=new TextField(\将鼠标放在上面的按钮上看形状\button1=new Button(\看看你鼠标的形状\add(button,BorderLayout.NORTH); add(button1,BorderLayout.CENTER); add(text,BorderLayout.SOUTH); button.setBackground(Color.cyan); button1.setBackground(Color.pink); button.addActionListener(this); addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0); } } );
setBounds(100,100,190,150); setVisible(true); validate(); }
public void actionPerformed(ActionEvent e) {
n=(n+1); switch(n) { case 0:
button1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); break;
case 1:
button1.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); break; case 2:
button1.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR)); break; case 3:
button1.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); break;
case 4:
button1.setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR)); break; case 5:
button1.setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR)); break; case 6:
button1.setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR)); break; case 7:
button1.setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR)); break; case 8:
button1.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR)); break; case 9:
button1.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); break; } } }
public class Test {
public static void main(String args[]) {
new WindowCursor(); } }
15.改进本章例子7.30,当释放鼠标键时,如果当前组件和其他组件相交,就将其他组件设置为不可见状态。 答: import java.awt.*; import java.awt.event.*;
import javax.swing.SwingUtilities;