public?class?BtnText1?extends??JFrame?implements?MouseMotionListener
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括覃塘網(wǎng)站建設(shè)、覃塘網(wǎng)站制作、覃塘網(wǎng)頁制作以及覃塘網(wǎng)絡(luò)營(yíng)銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,覃塘網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到覃塘省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
不需要實(shí)現(xiàn)MouseMotionListener接口,你已經(jīng)用了addMouseMotionListener方法
MouseAdapter類已經(jīng)是實(shí)現(xiàn)了MouseMotionListener接口的。
改成
public?class?BtnText1?extends??JFrame
可以運(yùn)行成功
//這是應(yīng)用程序,跟java小程序的大體思路還是差不多的,你改改
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.Border;
/**
*建立個(gè)界面,可以加載本機(jī)中圖片。
*加載后可以通過鼠標(biāo)點(diǎn)擊獲得圖片上任意點(diǎn)坐標(biāo)。
*/
public class MyPicture extends JFrame implements MouseListener{
private JLabel tipLabel;
/**
*main()
*/
public static void main(String[] args){
MyPicture frame = new MyPicture();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
/**
*constructor
*/
public MyPicture(){
setSize(800, 600);//根據(jù)要求調(diào)整大小
setLocation(100,100);
setTitle("獲得圖片上任意點(diǎn)坐標(biāo)");
setResizable(false);
Container con=getContentPane();
ImageIcon bgIcon=new ImageIcon("bgpic.jpg");//注意圖片的路徑
ImagePanel backpicPanel=new ImagePanel(bgIcon);
backpicPanel.addMouseListener(this);
con.add(backpicPanel,BorderLayout.CENTER);
tipLabel=new JLabel("--------------------提示:坐標(biāo)依次打印在屏幕上!--------------------");
con.add(tipLabel,BorderLayout.NORTH);
}
/**
*
*/
public void mousePressed(MouseEvent e){
int x=e.getX();
int y=e.getY();
String message="("+x+","+y+")";
tipLabel.setText(message);
System.out.println(message);
}
public void mouseReleased(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
}
/**
*類ImagePanel,用于添加背景圖片
*/
class ImagePanel extends JPanel{
private Image img;
public ImagePanel (ImageIcon imageIcon){
img=imageIcon.getImage();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img,0,0,this);
}
}
import java.util.Date;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MyTime extends JFrame {//刪除了集成的接口
JFrame frame = new JFrame();
JLabel label = null;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JButton btn1 = new JButton(" 紅 色 ");
JButton btn2 = new JButton(" 綠 色 ");
JButton btn3 = new JButton(" 藍(lán) 色 ");
public MyTime() {
panel1.setBackground(Color.white);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 10));
panel1.setBackground(Color.WHITE);
panel2.add(btn1);
panel2.add(btn2);
panel2.add(btn3);
add(panel1, BorderLayout.CENTER);
add(panel2, BorderLayout.SOUTH);// 創(chuàng)建一個(gè)監(jiān)聽器對(duì)象
btn1.addActionListener(new TimeThread(this).new AL()); // 為按鈕注冊(cè)監(jiān)聽器(使用了TimeThread類的AL內(nèi)部類)
btn2.addActionListener(new TimeThread(this).new AL());
btn3.addActionListener(new TimeThread(this).new AL());
frame = new JFrame();
label = new JLabel();
label.setIcon(new ImageIcon("QQ拼音截圖未命名.jpg"));
label.setFont(new Font("幼圓", Font.LAYOUT_NO_LIMIT_CONTEXT, 76));
label.setForeground(Color.BLACK);
add(label);
setTitle("個(gè)性數(shù)字時(shí)鐘");
setResizable(false);// 鎖定窗口大小
setSize(320, 160);
setLocation(300, 300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
}
MyTime mt = new MyTime();
new TimeThread(mt).start();
}
}
class TimeThread extends Thread {
private MyTime mt;
public TimeThread(MyTime mt) {
this.mt = mt;
}
public void run() {
while (true) {
String fullTime = new Date().toString();
String time = fullTime.substring(11, 19);
mt.label.setText(time);
mt.label.repaint();
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class AL implements ActionListener { // 內(nèi)部類
public void actionPerformed(ActionEvent e) {
if ((JButton) e.getSource() == mt.btn1)//btn1屬于MyTime類
mt.label.setForeground(Color.RED);
else if ((JButton) e.getSource() == mt.btn2)
mt.label.setForeground(Color.GREEN);
else if ((JButton) e.getSource() == mt.btn3)
mt.label.setForeground(Color.BLUE);
}
}
}
//修改的地方已經(jīng)注釋
畫圖不能這樣來畫的,因?yàn)镚raphics 不能自己來控制:
DrawLine DW=new DrawLine();
DW.g.setColor(Color.orange);
DW.g.drawLine(orgX, orgY, e.getX(), e.getY());
public void mouseRealeased(MouseEvent e)這個(gè)方法你拼錯(cuò)了,應(yīng)該是:
public void mouseReleased(MouseEvent e)
幫你改了下,實(shí)現(xiàn)的功能:安下鼠標(biāo)不放 -- 移動(dòng)別處 -- 松開,畫圖
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JPanel;
public class DrawLine extends Frame {
static JPanel drawPanel;
static int orgX;
static int orgY;
static int eX;
static int eY;
public static void main(String[] args) {
// TODO 自動(dòng)生成方法存根
drawPanel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawLine(orgX, orgY, eX, eY);
}
};
DrawLine mainFrame = new DrawLine();
mainFrame.setSize(400, 400);
mainFrame.setTitle("畫線");
mainFrame.add(drawPanel);
mainFrame.setVisible(true);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
System.exit(0);
}
});
mainFrame.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
orgX = e.getX();
orgY = e.getY();
}
public void mouseReleased(MouseEvent e) {
System.out.println("mouseReleased");
eX = e.getX();
eY = e.getY();
drawPanel.repaint();
}
});
}
}