import java.awt.Color;
曹妃甸網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、APP開(kāi)發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開(kāi)發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)公司2013年開(kāi)創(chuàng)至今到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來(lái)保證我們的工作的順利進(jìn)行。專(zhuān)注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
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)類(lèi)
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( //匿名類(lèi)
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)類(lèi)
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();
}
}