我正在开发一个程序,这意味着每当我悬停某个特定组件时都会打开一个无模式窗口。我没有进入不必要的细节,但基本上我有一个鼠标监听器,可以打开JFrame并显示图片。问题是,我还想要一个按钮来改变图片,但每当我将鼠标悬停在按钮上时,窗口就会关闭,我不明白为什么。 这是代码: 听众电话:
public class ImageAction extends FocusOwnerAction{
/**
* The user interface controller
*/
protected UIController uiController;
public ImageAction(UIController uiController) {
this.uiController = uiController;
}
public void actionPerformed(ActionEvent event) {
MouseHoverController mouse = new MouseHoverController(focusOwner, this.focusOwner.getSpreadsheet());
mouse.addMouseHoverEvent();
}
...
public class MouseHoverController {
private final JTable table;
private final Spreadsheet spread;
public MouseHoverController(JTable table, Spreadsheet spread) {
this.table = table;
this.spread = spread;
}
public void addMouseHoverEvent() {
MouseHoverEvent mouseEvent = new MouseHoverEvent(spread);
table.addMouseMotionListener((MouseMotionListener) mouseEvent);
}
}
...
public class MouseHoverEvent extends MouseMotionAdapter {
private Spreadsheet spreadsheet;
public MouseHoverEvent(Spreadsheet spreadsheet) {
this.spreadsheet = spreadsheet;
}
@Override
public void mouseMoved(MouseEvent e) {
final JTable aTable = (JTable) e.getSource();
int itsRow = aTable.rowAtPoint(e.getPoint());
int itsColumn = aTable.columnAtPoint(e.getPoint());
Cell c = this.spreadsheet.getCell(itsColumn, itsRow);
ImageCell choosedCell = (ImageCell) c.getExtension(ImageExtension.NAME);
if (choosedCell.hasImages()) {
aTable.removeMouseMotionListener(this);
final JFrame frame = new JFrame();
frame.setLayout(new GridLayout(2,1));
ImageIcon img = new ImageIcon("path.png");
Image im = img.getImage();
Image newimg = im.getScaledInstance(400, 500, java.awt.Image.SCALE_SMOOTH);
ImageIcon imgResizable = new ImageIcon(newimg);
JLabel image = new JLabel(imgResizable);
JPanel picPanel = new JPanel();
picPanel.add(image);
frame.add(picPanel);
JButton b1 = new JButton("Next");
frame.add(b1);
frame.setSize(400, 500);
// here's the part where I center the jframe on screen
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.addMouseListener(new MouseListener() {
public void mouseExited(MouseEvent e) {
frame.dispose();
MouseHoverEvent mouseEvent = new MouseHoverEvent(spreadsheet);
aTable.addMouseMotionListener((MouseMotionListener) mouseEvent);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
});
}
}
}