我想更新工具提示,所以我認爲在使用重繪方法時,每隔30秒重新繪製一次幀,但不起作用。有人可以helpme ..如何在特定時間後調用repaint()方法
GrphicsTut方法
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
public class GrphicsTut extends JFrame{
Timer timer;
Image image;
Image image2;
int x1,y1,x2,y2;
Font fonte = new Font("TimesRoman ",Font.BOLD,100);
public GrphicsTut(){
MouseListenerJ bkg = new MouseListenerJ();
this.setTitle("Remember g for Graphics");
this.setSize(600,500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.addMouseMotionListener(bkg);
this.addMouseListener(bkg);
this.add(bkg);
}
public void paint(Graphics gr){
gr.setFont(fonte);
gr.setColor(Color.black);
gr.drawLine(35,35, 410, 110);
gr.drawLine(410,110, 310, 410);
ImageIcon i=new ImageIcon("image/1005511030.jpg");
image=i.getImage();
gr.drawImage(image, 35, 35,null);
gr.drawImage(image, 400, 100,null);
gr.drawImage(image, 300, 400,null);
repaint();
}
public static void main(String[] args) {
new GrphicsTut();
}
}
這裏的方法MouseListner
package Carte;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MouseListenerJ extends JPanel implements MouseListener, MouseMotionListener {
public MouseListenerJ(){}
public void paint(Graphics g){}
@Override
public void mouseClicked(MouseEvent me) {
// TODO Auto-generated method stub
if(me.getX()>=35 && me.getX()<=70 && me.getY()>=35 &&me.getY()<=70){
//JOptionPane.showMessageDialog(null,"Que voulez vous faire");
new Carte1();
}
}
@Override
public void mouseEntered(MouseEvent me) {
// TODO Auto-generated method stub
//System.out.println("Entered at x "+me.getX());
}
@Override
public void mouseExited(MouseEvent me) {
// TODO Auto-generated method stub
//System.out.println("Exited");
}
@Override
public void mousePressed(MouseEvent me) {
// TODO Auto-generated method stub
System.out.println("Pressed at : "+me.getX()+" "+me.getY());
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
System.out.println("released");
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent me) {
// TODO Auto-generated method stub
if(me.getX()>=35 && me.getX()<=70 && me.getY()>=35 && me.getY()<=70){
//JOptionPane.showMessageDialog(null,"Que voulez vous faire");
this.setToolTipText("text");
System.out.println(me.getX()+" "+me.getY());
}
else if(me.getX()>=400 && me.getX()<=470 && me.getY()>=100 && me.getY(<=170){
this.setToolTipText("Text 2");
System.out.println(me.getX()+" "+me.getY());
}
}
}
1)考慮告訴我們你的原意,因爲這看起來很糟糕2)不要在任何一個內部調用repaint() paint()方法,這將創建一個無限循環3)總是在paintXXX()方法中調用super.paintXXX()4)考慮重寫paintComponent而不是paint()5)不要在paint()方法中創建ImageIcon,相反,創建它,並重復使用相同的變量(在這種情況下,你甚至可以使它成爲常量) –
謝謝你的回答。我的目標是將一些圖標放入圖形界面,然後使用mouseEvent獲取這些圖標的工具提示,或者如果我喜歡它,可以傳遞給其他界面。 – aziz