JTable 内的 MouseMotionListener
我正在尝试将 MouseMotion 事件添加到标签中,并根据鼠标的拖动来移动它,并使其与我的鼠标一起移动。但是 mousemotion 很难控制,使得此操作不可用。 这是代码,
import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class TableTest
{
public TableTest()
{
String[] columnNames =
{ "FileName", "Integer" };
Object[][] data =
{
{ new FileName("AAA.jpg", Color.YELLOW), new Integer(2) },
{ new FileName("BBB.png", Color.GREEN), new FileName("BBB.png", Color.GREEN) },
{ new FileName("CCC.jpg", Color.RED), new Integer(-1) }, };
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
public Class getColumnClass(int column)
{
System.out.println("column is" + column);
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable(model);
//JTable table = new JTable(data, columnNames);
table.setDefaultRenderer(FileName.class, new FileNameCellRenderer());
final JLabel label = new JLabel("TESTING", SwingConstants.CENTER);
label.setBackground(java.awt.Color.RED);
label.setBounds(450, 100, 90, 20);
label.setOpaque(true);
label.setVisible(true);
label.addMouseMotionListener(new MouseMotionListener()
{
public void mouseDragged(MouseEvent arg0)
{
label.setBounds(arg0.getX(), arg0.getY(), 90, 20);
}
public void mouseMoved(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
});
table.add(label);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(table);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
static class FileNameCellRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object v,
boolean isSelected, boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(table, v, isSelected, hasFocus,
row, column);
FileName fn = (FileName) v;
setBorder(BorderFactory.createMatteBorder(0, 60, 0, 0,
new java.awt.Color(143, 188, 143)));
return this;
}
}
static class FileName
{
public final Color color;
public final String label;
FileName(String l, Color c)
{
this.label = l;
this.color = c;
}
public String toString()
{
return label;
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TableTest();
}
});
}
}
我只想让标签跟随标签跟随我的鼠标,标签应该附加到表格上。有没有比这更简单的方法。
I am trying to add MouseMotion event to the label and move it based on the dragging of the mouse and make it move along with my mouse.However the mousemotion is very difficult to control making this action not usable.
Here is the code
import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
public class TableTest
{
public TableTest()
{
String[] columnNames =
{ "FileName", "Integer" };
Object[][] data =
{
{ new FileName("AAA.jpg", Color.YELLOW), new Integer(2) },
{ new FileName("BBB.png", Color.GREEN), new FileName("BBB.png", Color.GREEN) },
{ new FileName("CCC.jpg", Color.RED), new Integer(-1) }, };
DefaultTableModel model = new DefaultTableModel(data, columnNames)
{
public Class getColumnClass(int column)
{
System.out.println("column is" + column);
return getValueAt(0, column).getClass();
}
};
JTable table = new JTable(model);
//JTable table = new JTable(data, columnNames);
table.setDefaultRenderer(FileName.class, new FileNameCellRenderer());
final JLabel label = new JLabel("TESTING", SwingConstants.CENTER);
label.setBackground(java.awt.Color.RED);
label.setBounds(450, 100, 90, 20);
label.setOpaque(true);
label.setVisible(true);
label.addMouseMotionListener(new MouseMotionListener()
{
public void mouseDragged(MouseEvent arg0)
{
label.setBounds(arg0.getX(), arg0.getY(), 90, 20);
}
public void mouseMoved(MouseEvent arg0)
{
// TODO Auto-generated method stub
}
});
table.add(label);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(table);
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
static class FileNameCellRenderer extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable table, Object v,
boolean isSelected, boolean hasFocus, int row, int column)
{
super.getTableCellRendererComponent(table, v, isSelected, hasFocus,
row, column);
FileName fn = (FileName) v;
setBorder(BorderFactory.createMatteBorder(0, 60, 0, 0,
new java.awt.Color(143, 188, 143)));
return this;
}
}
static class FileName
{
public final Color color;
public final String label;
FileName(String l, Color c)
{
this.label = l;
this.color = c;
}
public String toString()
{
return label;
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new TableTest();
}
});
}
}
I just want to make the label follow the label follow my mouse and the label should be attached to the table.Is there an easy way than this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我认为您错误地使用了鼠标事件坐标 (x,y),因为它们是相对于源组件的。在这种情况下,来源就是标签本身。因此,您应该将源组件的原始 (x,y) 添加到这些值中:
自从我不得不与鼠标事件作斗争以来已经有一段时间了,但也许您可以尝试这个并让我们知道它是否有帮助。
我不确定的另一件事是向 JTable 添加标签。 JTable 的 LayoutManager 可能会阻止您实际实现这种方式,您可能需要使用 GlassPane 或表格顶部的其他东西。
First of all, I think you using the mouse event coordinates (x,y) incorrectly, since those are relative to the source component. In this case the source is the label itself. So you should add to those values the original (x,y) of the source component:
It's been a while since I had to struggle with the mouse events, but maybe you could try this and let us know if it helps.
Another thing which I'm not sure of is the adding of label to the JTable. The LayoutManager of the JTable might prevent you from actually implementing this way, you may need to use GlassPane or something on top of the table.