新手在这里
这是我的代码,我希望当光标进入绿色jpanel(这里是" p2")时,程序将有另一个jpanel(这里是" p1")来显示一些信息关于对象。 (像工具提示)
但是当光标进入JPanel p2时... mouseMove无法获取光标的位置......只有当我删除JPanel p2时它才有效。请帮帮我们
注意:它仍在JPanel p2的边框上工作;
import javax.management.Attribute;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AttributeSet.FontAttribute;
import org.w3c.dom.events.EventTarget;
import org.w3c.dom.events.MouseEvent;
import org.w3c.dom.views.AbstractView;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.text.AttributedCharacterIterator;
import java.util.Locale;
public class InterfaceSubject extends JFrame {
public JPanel contentPane, p1, p2;
public JButton bt1;
public JLabel lb2;
public JTextPane tp1, tp2, tp3;
public InterfaceSubject() {
this.setResizable(false);
setTitle("Demo");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100, 20, 1024, 768);
bt1 = new JButton();
p1 = new JPanel();
p1.setLayout(new BorderLayout());
p1.setBackground(Color.yellow);
p2 = new JPanel();
GridLayout gr = new GridLayout();
gr.setColumns(1);
gr.setRows(3);
p2.setLayout(gr);
lb2 = new JLabel();
tp1 = new JTextPane();
tp1.setForeground(Color.BLUE);
tp1.setBackground(Color.green);
tp1.setText("MMH: 212121");
tp2 = new JTextPane();
tp2.setForeground(Color.BLUE);
tp2.setBackground(Color.green);
tp2.setText("Tên MH: Hello");
tp3 = new JTextPane();
tp3.setForeground(Color.BLUE);
tp3.setBackground(Color.green);
tp3.setText("TC : 1");
contentPane = new ImagePanel(
new ImageIcon("images\\formtkb.jpg").getImage());
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
p2.setBounds(60, 121, 125, 75);
p2.add(tp1);
p2.add(tp2);
p2.add(tp3);
contentPane.add(p2);
setContentPane(contentPane);
contentPane.setLayout(null);
this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(java.awt.event.MouseEvent arg0) {
if ((arg0.getX()) >= (60) && arg0.getX() <= (60 + 125)) {
if ((arg0.getY()) >= (121 + 25)
&& (arg0.getY()) <= (121 + 100)) {
p1.setVisible(true);
p1.setBounds(new Rectangle(arg0.getPoint(),
new Dimension(200, 200)));
lb2.setText(arg0.getX() + " / " + arg0.getY());
p1.add(lb2, BorderLayout.SOUTH);
contentPane.add(p1);
} else {
p1.setVisible(false);
}
} else {
p1.setVisible(false);
}
}
@Override
public void mouseDragged(java.awt.event.MouseEvent arg0) {
// TODO Auto-generated method stub
}
});
}
}
class ImagePanel extends JPanel {
/**
*
*/
private Image img;
public ImagePanel(String img) {
this(new ImageIcon(img).getImage());
}
public ImagePanel(Image img) {
this.img = img;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 100, null);
}
}
&#13;
答案 0 :(得分:0)
为您的面板添加mouseMotionListener。像
p1.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(java.awt.event.MouseEvent arg0) {
}
});
p2.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(java.awt.event.MouseEvent arg0) {
}
});