您好,登錄后才能下訂單哦!
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class TestEventMouse extends JFrame{
Container contentPanel; //內(nèi)容格引用
JButton a1,a2,a3,a4,a5;//按鈕
JRadioButton backGroundRadio,foreGroundRadio; //定義兩個(gè)單選框
ButtonGroup radioGroup; //單選按鈕組
int xValue,yValue; //保留鼠標(biāo)位置(x,y)
JFrame jf=this;
public TestEventMouse(){ //構(gòu)造函數(shù)
super("寫(xiě)字板");
a1=new JButton("Black");//實(shí)現(xiàn)五個(gè)按鈕
a2=new JButton("Blue");
a3=new JButton("Red");
a4=new JButton("White");
a5=new JButton("Yellow");
contentPanel=this.getContentPane();//獲取內(nèi)容格
contentPanel.setLayout(new FlowLayout());//設(shè)置內(nèi)容格的布局管理器
backGroundRadio=new JRadioButton("backGround"); //實(shí)例化復(fù)選框
foreGroundRadio=new JRadioButton("foreGround");
contentPanel.add(backGroundRadio);//添加到內(nèi)容格里面去
contentPanel.add(foreGroundRadio);
contentPanel.add(a1);//添加按鈕
contentPanel.add(a2);
contentPanel.add(a3);
contentPanel.add(a4);
contentPanel.add(a5);
MouseListenerHandler mou=new MouseListenerHandler(); //實(shí)現(xiàn)一個(gè)事件監(jiān)聽(tīng)類
a1.addMouseListener(mou); //注冊(cè)為五個(gè)按鈕的監(jiān)聽(tīng)者
a2.addMouseListener(mou);
a3.addMouseListener(mou);
a4.addMouseListener(mou);
a5.addMouseListener(mou);
radioGroup=new ButtonGroup(); //實(shí)例單選按鈕組
radioGroup.add(backGroundRadio);//將兩個(gè)radio按鈕構(gòu)成一個(gè)組
radioGroup.add(foreGroundRadio);
addMouseMotionListener( //匿名類
new MouseMotionAdapter(){ //因?yàn)槭钱?dāng)前一個(gè)對(duì)象調(diào)用,所以搞了個(gè)適配器玩玩
public void mouseDragged(MouseEvent e){
xValue=e.getX();
yValue=e.getY();
repaint(); //調(diào)用paint()方法
}
}
);
setSize(500,500);
setVisible(true); //設(shè)置可見(jiàn)
}
public class MouseListenerHandler implements MouseListener{ //鼠標(biāo)事件監(jiān)聽(tīng)類
public void mousePressed(MouseEvent e){
if(e.getSource()==a1){ //用getSouce()獲取對(duì)象
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.BLACK);//用內(nèi)容格來(lái)設(shè)置背景色
else
jf.setForeground(Color.BLACK); //用Frame框架來(lái)設(shè)置前景色
}
if(e.getSource()==a2){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.BLUE);
else
jf.setForeground(Color.BLUE);
}
if(e.getSource()==a3){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.RED);
else
jf.setForeground(Color.RED);
}
if(e.getSource()==a4){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.WHITE);
else
jf.setForeground(Color.WHITE);
}
if(e.getSource()==a5){
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.YELLOW);
else
jf.setForeground(Color.YELLOW);
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
}
public void paint(Graphics g){
g.fillOval(xValue, yValue, 4, 4);
}
public static void main(String args[]){
new TestEventMouse();
}
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。