资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Java怎么制作写字板功能-创新互联

import java.awt.Color;

成都创新互联公司专注于企业全网营销推广、网站重做改版、宜春网站定制设计、自适应品牌网站建设、HTML5商城开发、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为宜春等各大城市提供网站开发制作服务。

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; //内容格引用
JButton a1,a2,a3,a4,a5;//按钮
JRadioButton backGroundRadio,foreGroundRadio;  //定义两个单选框
ButtonGroup radioGroup;  //单选按钮组
int xValue,yValue;  //保留鼠标位置(x,y)
JFrame jf=this;
public TestEventMouse(){ //构造函数
super("写字板");
a1=new JButton("Black");//实现五个按钮
a2=new JButton("Blue");
a3=new JButton("Red");
a4=new JButton("White");
a5=new JButton("Yellow");
contentPanel=this.getContentPane();//获取内容格
contentPanel.setLayout(new FlowLayout());//设置内容格的布局管理器
backGroundRadio=new JRadioButton("backGround"); //实例化复选框
foreGroundRadio=new JRadioButton("foreGround");
contentPanel.add(backGroundRadio);//添加到内容格里面去
contentPanel.add(foreGroundRadio);
contentPanel.add(a1);//添加按钮
contentPanel.add(a2);
contentPanel.add(a3);
contentPanel.add(a4);
contentPanel.add(a5);
MouseListenerHandler mou=new MouseListenerHandler(); //实现一个事件监听类
a1.addMouseListener(mou); //注册为五个按钮的监听者
a2.addMouseListener(mou);
a3.addMouseListener(mou);
a4.addMouseListener(mou);
a5.addMouseListener(mou);
radioGroup=new ButtonGroup(); //实例单选按钮组
radioGroup.add(backGroundRadio);//将两个radio按钮构成一个组
radioGroup.add(foreGroundRadio);

addMouseMotionListener(  //匿名类
new MouseMotionAdapter(){  //因为是当前一个对象调用,所以搞了个适配器玩玩
public void mouseDragged(MouseEvent e){
xValue=e.getX();
yValue=e.getY();
repaint();  //调用paint()方法
}
}
);
setSize(500,500);  
setVisible(true);  //设置可见
}
public class MouseListenerHandler implements MouseListener{  //鼠标事件监听类
public void mousePressed(MouseEvent e){
if(e.getSource()==a1){  //用getSouce()获取对象
if(backGroundRadio.isSelected())
contentPanel.setBackground(Color.BLACK);//用内容格来设置背景色
else                 
jf.setForeground(Color.BLACK);  //用Frame框架来设置前景色
}
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();
}
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:Java怎么制作写字板功能-创新互联
网页URL:http://cdkjz.cn/article/doeoes.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220