import java.awt.Color;
河池網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),河池網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為河池上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的河池做網(wǎng)站的公司定做!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Demo1 extends JFrame implements
MouseMotionListener,KeyListener,MouseListener {
private JPanel content;
private JPanel bottom;
private JLabel show;
static String keyCode="";
public Demo1(){
content=new JPanel();
content.setLayout(null);
show=new JLabel();
show.setForeground(Color.white);
bottom=new JPanel();
content.setBounds(0, 0, 800, 600);
content.setBackground(Color.black);
content.add(show);
content.setFocusable(true);
content.addKeyListener(this);
content.addMouseMotionListener(this);
content.addMouseListener(this);
setTitle
("讀取鍵盤輸入的字符并將其顯示到鼠標所指的位置");
setSize(800, 600);
setLocationRelativeTo(getOwner());
add(content);
add(bottom);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
keyCode+=String.valueOf((char)e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
new Demo1();
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
if(keyCode!=""){
show.setText(keyCode);
show.setBounds(e.getX()+20, e.getY(), 800, 30);
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
1、首先打開IDEA軟件,進入Java代碼編輯主界面。
2、其次點擊編輯窗口上方的“文件”菜單項。
3、最后在下拉子菜單中,點擊“設(shè)置”選項,選擇需要的設(shè)置即可。
this.addMouseListener(new
MouseAdapter()
{
@Override
public
void
mousePressed(MouseEvent
e)
{
x
=
e.getX();
y
=
e.getY();
System.out.println("Xbefore:"+e.getX());
}
});
this.addMouseMotionListener(new
MouseMotionListener()
{
public
void
mouseDragged(MouseEvent
e)
{
JButton
btn
=
(JButton)
e.getSource();
setLocation(btn.getX()
+
e.getX()
-
x,
btn.getY()
+
e.getY()
-
y);
}
public
void
mouseMoved(MouseEvent
e)
{
}
});
上面的代碼是給JButton添加的鼠標事件,移動的是按鈕。。。
你這樣不行啊,一閃一閃的根本看不到顯示效果或者更本沒有顯示,繪圖的方法要寫在paintComponent(Graphics g)里面才能長久保存。
我知道你想在mouseClicked里面做什么,但是在這里進行繪圖,repaint沒有立即被執(zhí)行,所以有可能沒有任何顯示;如果repaint執(zhí)行了,也很有可能立刻被后面的繪圖所覆蓋。最好的辦法是在這里出發(fā)一個標志,然后調(diào)用repaint函數(shù),在paintComponent函數(shù)中對這個標志進行繪制,決定到底該怎么畫。
手寫了一段代碼,沒有編譯,你可以參考一下:
package colin;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class JXFrame extends JFrame
{
boolean mouseCliked = false;
int x,y;
public void paintComponent(Graphics g)
{
if(mouseCliked )
{
g.setColor(255,255,0);//必須有這一步,否則畫筆顏色為默認背景色,是根本看不出任何線條效果的。
g.drawLine(0,0,x,y);
}
}
public void setPoint(int x,int y)
{
this.x = x;
this.y = y;
}
}
package colin;
import java.awt.*;
import javax.swing.*;
class frame{
frame(){
JXFrame f = new JXFrame();
f.addMouseListener(new Mouseevent(f));
f.setSize(500,600);
f.setVisible(true);
}
public static void main(String args[]){
new frame();
}
}
package colin;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Mouseevent implements MouseListener{
JXFrame parent;
public Mouseevnt(JXFrame parent){
this.parent = parent;
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseEntered(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseExited(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseClicked(MouseEvent e){
this.parent.setPoint(e.getX(),e.getY());
this.parent.mouseCliked = true;
this.parent.repaint();
}
}
樓主提問的水平很高啊,題目的要求都沒有,如何回答。。。
相關(guān)類都在
import java.awt.*;
import javax.swing.* ;
動作事件相關(guān)類在import java.awt.event.*;中,在代碼頭import就可以看了
給一個相關(guān)的代碼吧,包括所有的鼠標事件。
下面代碼包括
按下,釋放,進入?yún)^(qū)域,離開區(qū)域,點擊 這5個事件
鼠標的相關(guān)事件會在窗口上方的文本區(qū)顯示。。。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestMouseEvent extends JFrame implements MouseListener{
JPanel jp = new JPanel();
JTextField jtf = new JTextField();
public TestMouseEvent(){
setTitle("Mouse Event");
getContentPane().add(jtf,BorderLayout.NORTH);
getContentPane().add(jp,BorderLayout.CENTER);
jp.addMouseListener(this);
}
public static void main(String[] args){
TestMouseEvent frame = new TestMouseEvent();
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mousePressed(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠標在坐標"+"("+x+","+y+")"+"出按下";
jtf.setText(s);
}
public void mouseReleased(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠標在坐標"+"("+x+","+y+")"+"出釋放";
jtf.setText(s);
}
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠標在坐標"+"("+x+","+y+")"+"出點擊";
jtf.setText(s);
}
public void mouseEntered(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠標在坐標"+"("+x+","+y+")"+"出進入";
jtf.setText(s);
}
public void mouseExited(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠標在坐標"+"("+x+","+y+")"+"出離開";
jtf.setText(s);
}
}
使用組件的paint函數(shù)用于繪圖, 使用MouseListener來響應(yīng)鼠標的點擊
效果圖
代碼
import?java.awt.Color;
import?java.awt.Graphics;
import?java.awt.event.*;
import?javax.swing.*;
public?class?DemoWin?extends?JFrame?{
public?DemoWin()?{
MyPanel?mp?=?new?MyPanel();
mp.addMouseListener(mp);
add(mp);
//?窗口屬性設(shè)置
setTitle("Demo");//?標題
setSize(300,?280);//?窗口大小
setLocationRelativeTo(null);//?窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);//?窗口點擊關(guān)閉時,退出程序
}
public?static?void?main(String[]?args)?{
DemoWin?win?=?new?DemoWin();?//?創(chuàng)建窗口
win.setVisible(true);//?顯示窗口
}
class?MyPanel?extends?JPanel?implements?MouseListener?{
int?times;//?記錄點擊的次數(shù)
int?x;//?記錄鼠標X軸的位置
int?y;//?記錄鼠標Y軸的位置
@Override
public?void?paint(Graphics?g)?{
super.paint(g);
if?(times?==?0)?{
g.setColor(Color.BLUE);//?顏色
g.fillOval(150,?150,?50,?50);//?150,150代表位置?50,50代表寬高
}?else?if?(times?==?1)?{
g.setColor(Color.RED);
g.fillRect(150,?150,?50,?50);
}?else?{
g.setColor(Color.RED);
g.fillRect(x,?y,?50,?50);
}
repaint();
}
public?void?mouseClicked(MouseEvent?e)?{
//?if?(e.getButton()==MouseEvent.BUTTON1)?{//單擊左鍵時有效..
//?times++;//?記錄點擊的次數(shù)
//?x?=?e.getX();
//?y?=?e.getY();
//?}
}
public?void?mousePressed(MouseEvent?e)?{//?鼠標按下就有效
times++;//?記錄點擊的次數(shù)
x?=?e.getX();
y?=?e.getY();
}
public?void?mouseReleased(MouseEvent?e)?{//?鼠標釋放
}
public?void?mouseEntered(MouseEvent?e)?{//?鼠標移入
}
public?void?mouseExited(MouseEvent?e)?{//?鼠標移出
}
}
}