import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyPanel extends JPanel{
public void print(int r){
Graphics g = getGraphics();
g.clearRect(0,0,this.getWidth(),this.getHeight());
g.setColor(Color.red);
g.fillOval(10,10,r,r);
}
}
class MyWindow extends JFrame implements MouseListener{
JTextArea text;
MyPanel panel;
int x,y,r =10;
int mouseFlg=0;
static String mouseStates[]={"鼠标键按下","鼠标松开","鼠标进来","鼠标走开","鼠标双击"};
MyWindow(String s){
super(s);
Container con = this.getContentPane();
con.setLayout(new GridLayout(2,1));
this.setSize(200,300);
this.setLocation(100,100);
panel = new MyPanel();
con.add(panel);
text = new JTextArea(10,20);
text.setBackground(Color.blue);
con.add(text);
addMouseListener(this);
this.setVisible(true);
this.pack();
}
public void paint(Graphics g){
r = r+4;
if(r>80){
r=10;
}
text.append(mouseStates[mouseFlg]+"了,位置是:" +x+","+y+"n");
panel.print(r);
}
public void mousePressed(MouseEvent e){
x = e.getX();
y = e.getY();
mouseFlg = 0;
repaint();
}
public void mouseRelease(MouseEvent e){
x = e.getX();
y = e.getY();
mouseFlg = 1;
repaint();
}
public void mouseEntered(MouseEvent e){
x = e.getX();
y = e.getY();
mouseFlg = 2;
repaint();
}
public void mouseExited(MouseEvent e){
x = e.getX();
y = e.getY();
mouseFlg = 3;
repaint();
}
public void mouseClicked(MouseEvent e){
if(e.getClickCount()==2){
x = e.getX();
y = e.getY();
mouseFlg = 4;
repaint();
}
else{}
}
}
public class Example6_8 extends Applet{
public void init(){
MyWindow myWnd = new MyWindow("鼠标事件示意程序");
}
}
JButton button;
public void init(){
button = new JButton(“按钮也能发生鼠标事件”);
r = 10;
text = new JTextArea(15,20);
add(button);
add(text);
button.addMouseListener(this);
}
e.getModifiers()==InputEvent. BUTTON3_MASK
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyWindow extends JFrame{
public MyWindow(String s){
super(s);
Container con = this.getContentPane();
con.setLayout(new BorderLayout());
this.setLocation(100,100);
JScrollBar xAxis = new JScrollBar(JScrollBar.HORIZONTAL,50,1,0,100);
jScrollBar yAxis = new jScrollBar(JScrollBar.VERTICAL,50,1,0,100);
MyListener listener = new MyListener(xAxis,yAxis,238,118);
Jpanel scrolledCanvas = new JPanel();
scrolledCanvas.setLayout(new BorderLayout());
scrolledCanvas.add(listener,BorderLayout.CENTER);
scrolledCanvas.add(xAix,BorderLayout.SOUTH);
scrolledCanvas.add(yAix,BorderLayout.EAST);
con.add(scrolledCanvas,BorderLayout.NORTH);
this.setVisible(true);
this.pack();
}
public Dimension getPreferredSize(){
return new Dimension(500,300);
}
}
class MyListener extends JComponent implements MouseListener, MouseMotionListener,AdjustmentListener{
private int x,y;
private JScrollBar xScrollBar;
private JScrollBar yScrollBar;
private void updateScrollBars(int x,int y){
int d;
d = (int)(((float)x/(float)getSize().width)*100.0);
xScrollBar.setValue(d);
d = (int)(((float)y/(float)getSize().height)*100.0);
yScrollBar.setValue(d);
}
public MyListener(JScrollBar xaxis,JScrollBar yaxis,int x0,int y0){
xScrollBar =xaxis;
yScrollBar =yaxis;
x = x0;
y=y0;
xScrollBar.addAdjustmentListener(this);
yScrollBar.addAdjustmentListener(this);
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void paint(Graphics g){
g.setColor(getBackground());
Dimension size = getSize();
g.fillRect(0,0,size.width,size.height);
g.setColor(Color.blue);
g.fillRect(x,y,50,50);
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseRelease(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mousePressed(MouseEvent e){
x = e.getX();
y = e.getY();
updateScrollBars(x,y);
repaint();
}
public void mouseDragged(MouseEvent e){
x = e.getX();
y = e.getY();
updateScrollBars(x,y);
repaint();
}
public void adjustmentValueChanged(AdjustmentEvent e){
if(e.getSource()==xScrollBar)
x=(int)((float)(xScrollBar.getValue()/100.0)*getSize().width);
else if(e.getSource()==yScrollBar)
y = (int)((float)(yScrollBar.getValue()/100.0)*getSize().height);
repaint();
}
}
public class Example6_9{
public static void main(){
MyWindow myWindow = new MyWindow("滚动条示意程序");
}
}
class MyWindow extends JFrame{
public MyWindow(String s){
super(s);
Container con = this.getContentPane();
con.setLayout(new BorderLayout());
this.setLocaltion(100,100);
MyListener listener = new MyListener();
listener.setPreferredSize(new Dimension(700,700));
JScrollPane scrolledCanvas = new JScrollPane(listener);
this.add(scrolledCanvas,BorderLayout.CENTER);
this.setVisible(true);
this.pack();
}
public Dimension getPreferredSize(){
return new Dimension(400,400);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有