我想更新toolTip,所以我想在使用重绘方法时,每隔30秒重新绘制一次帧,但它不起作用。能有人帮忙..
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());
}
}
}
答案 0 :(得分:1)
考虑使用原生Swing组件来实现目标。它们提供了您想要的所有功能:
以下是一个小片段,向您展示如何使用JButton
:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestButtons {
protected void initUI() throws MalformedURLException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(2, 2, 20, 20));
panel.add(createButton("https://pbs.twimg.com/profile_images/378800000534649369/489a1e058bea59b62fd73c56f4bcb6c7.jpeg"));
panel.add(createButton("https://pbs.twimg.com/profile_images/378800000714838591/ebbde1563faae6da2be79df945a7a02b.jpeg"));
panel.add(createButton("https://pbs.twimg.com/profile_images/3477392906/f1907df0bd76668deac4a5e31a22fbe7.jpeg"));
panel.add(createButton("https://pbs.twimg.com/profile_images/2718799802/9567ed3c3299f6f6ab1ffcbcbfd93da5.jpeg"));
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
private JButton createButton(final String url) throws MalformedURLException {
final JButton button = new JButton(new ImageIcon(new URL(url)));
button.setToolTipText("You are looking at image located at " + url);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(button,
"You clicked on image located at " + url);
}
});
return button;
}
/**
* @param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new TestButtons().initUI();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
}