資源簡介
1.編程實現以下功能:
界面如下圖所示;
當點擊不同的按鈕時,圓的填充顏色會隨之改變;
用鼠標點擊窗體時,圓的 填充顏色會自動依照”面板背景色-紅色-綠色-藍色”循環改變;
鼠標移到圓內時,光標變成十字形;
代碼片段和文件信息
import?javax.swing.*;
import?java.awt.*;
import?java.awt.event.*;
import?java.awt.geom.Ellipse2D;
import?java.awt.geom.Point2D;
public?class?CircleTest?{
public?static?void?main(String[]args){????//設置可見窗體
Buttonframe?f?=?new?Buttonframe();
f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class?Buttonframe?extends?Jframe{
public?Buttonframe(){??//面板加入窗體當中
this.settitle(“Button?Window“);
this.setSize(DEFAULT_WIDTH?DEFAULT_HEIGHT);
this.add(new?ButtonPanel());
}
public?static?final?int?DEFAULT_WIDTH?=?400;
public?static?final?int?DEFAULT_HEIGHT?=?300;
}
class?ButtonPanel?extends?JPanel{
private?Ellipse2D?yuan;
private?Color?ecolor=getBackground();
private?Color[]?colors={getBackground()Color.yellowColor.redColor.green};
?public?void?init(){
???new?Thread().start();
??}
public?ButtonPanel(){
//用標簽字符串構造按鈕;
JButton?b1?=?new?JButton(“Yellow“);
JButton?b2?=?new?JButton(“Red“);
JButton?b3?=?new?JButton(“Green“);
this.add(b1);//將按鈕添加到面板上
this.add(b2);
this.add(b3);
//用適當顏色構造監聽器對象
ColorAction?a1?=?new?ColorAction(Color.YELLOW);
ColorAction?a2?=?new?ColorAction(Color.RED);
ColorAction?a3?=?new?ColorAction(Color.GREEN);
MouseHandler?x?=?new?MouseHandler();
MouseMotionHandler?moh?=?new?MouseMotionHandler();
/*eventSourceobject.addEventListener(eventListenerobject);
?????????//把對象注冊到事件源上*/
b1.addActionListener(a1);//把action與Button連接上???????將監聽器對象注冊到組件
b2.addActionListener(a2);
b3.addActionListener(a3);?
this.addMouseMotionListener(moh);//監聽鼠標移動事件
this.addMouseListener(x);
}
public?void?paintComponent(Graphics?g)
評論
共有 條評論