我有一个JPanel
和JLabel
的阵列,图标代表剧院中的一个座位,所有这些都是使用循环生成的。 加载座位后,已预订的座位需要有不同的图像图标。因此,if(){}
检查在所有座位上执行,以便在座位被预订后更改标签图标,之后它们被生成。图标图像加载速度不够快
但我的磁盘上的图像图标加载速度不够快,所以有时面板只会添加到最后一个预订或根本不加载。 代表座椅的每个面板也都添加了接口。因此,在鼠标悬停或单击ImageIcon
添加到面板的对象更改时,发生这种情况时会有太多延迟。我认为这与磁盘上的图像有关!
我如何可以加载和存储这些图标图像内存在大小2,78 KB,并指它在内存中,所以它不会被延迟读书呢?
当单击座位时,我需要更改该座位的标签图像,并从该座位上移除鼠标侦听器。有没有办法将鼠标监听器移动到特定的座位而不参考特定的鼠标监听器。我需要在鼠标监听器本身之外做到这一点!
panel.removeAll();
不会删除生成面板时添加的鼠标侦听器。
public void drawSeats(int ammountSeat, int localLength, int localWidth) {
pnlSeatsHolder = new JPanel();
pnlSeatsHolder.setPreferredSize(new Dimension(localLength * 40,localLength * 45));
pnlSeatsHolder.setLayout(new FlowLayout());
for (int d = 0; d <= (ammountSeat); d++) {
imgIconYellow = new ImageIcon("seatYellow.png");
imgIconBlue = new ImageIcon("seatBlue.png");
imgIconRed = new ImageIcon("seatRed.png");
JButton chairs = new JButton();
chairs.setPreferredSize(new Dimension(30, 40));
pnlSeatsHolder.add(chairs);
chairs.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
for (int i = 0; i < listSeatsObjects.size(); i++) {
if (listSeatsObjects.get(i).equals(e.getSource())) {
/*I need to do this also outside of this method! how can i refer to this MouseListener
* to forexample do the equivalent of chairs.removeMouseListener(this);*/
chairs.removeAll();
chairs.setIcon(imgIconRed);
chairs.repaint();
chairs.removeMouseListener(this);
// send information of the chair somewhere else
}
}
}
public void mouseEntered(MouseEvent e) {
// chairs.setBackground(Color.blue);
chairs.removeAll();
chairs.setIcon(imgIconBlue);
chairs.repaint();
}
public void mouseExited(MouseEvent e) {
// chairs.setBackground(Color.BLACK);
chairs.removeAll();
chairs.setIcon(imgIconYellow);
chairs.repaint();
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
}
}
所以这是调用的时候,吸引了座位上的方式自我。我做了一些修改@AndrewThompson建议,而不是JPanels我现在使用JButtons,但会发生的是,图像不会加载到按钮..我错过了什么?鼠标悬停无论是.. tho它确实工作,如果我有例如charis.setBackgroundColor();在悬停或点击..所以我现在我需要宁愿改变按钮图片点击和悬停时,我已经试过chairs.chairs.setRolloverIcon();和.setIcon();两者都没有工作。哪里不对。我的图像与类文件位于同一目录..所以不能成为问题..
int localLength,int localWidth是座位将被绘制的房间的大小。约1立方米/座
*“我如何加载和存储这些图标图像”*总共有多少(不同)图像? –
您还应该考虑在'MouseListener'中使用带有'ActionListener'的'JButton'或'JToggleButton',而不是'JLabel'。按钮不仅对鼠标和键盘输入有反应,而且该按钮还支持在悬停,按下,对焦等情况下更改图标。 –
3.红色座椅,蓝色&&黄色座椅的生成量取决于其他周长。但是这3张图片是由所有席位共享的。 黄色是在开始时添加到所有。在鼠标悬停时变成蓝色,点击鼠标时变成红色。 – user2624724