[英]Problem with EDT (Java) how to split few actions/events
Basically, I have a JFrame
& in this frame there is a JPanel
.基本上,我有一个
JFrame
在这个框架中有一个JPanel
。 On top of panel there are four JLabel
components, lets call them colorLabels
, and on the bottom of panel I have one label (let's call it playerLabel
).在面板顶部有四个
JLabel
组件,我们称之为colorLabels
,在面板底部我有一个标签(我们称之为playerLabel
)。 I want to implements next thing.我想实现下一件事。
Top colorLabels
need to change their color from time to time (less then second) and they need to, let's say, shoot projectiles on bottom.顶部
colorLabels
需要不时改变它们的颜色(少于一秒),并且它们需要,比如说,在底部发射弹丸。 On bottom playerLabel
need to be move around x-axis so it can block shots.在底部
playerLabel
需要绕 x 轴移动,以便它可以阻挡镜头。
My problem is that, when I activate SwingWorker
or any thread on colorLabels
so they can shoot projectiles and change color overtime my EDT get blocked so I can not move playLabel
(event doesn't work).我的问题是,当我启动
SwingWorker
或在任何线程colorLabels
,使他们能够拍出弹和改变颜色加班我EDT被封锁,所以我不能移动playLabel
(事件不工作)。 I mean they work but at the same time, playerLabel
get back to original position.我的意思是他们工作,但同时,
playerLabel
回到原来的位置。 (EDT blocked) (EDT 被封锁)
My question is, how can I split actions on colorLabel
(top labels) from action on playerLabel
(bottom label)?我的问题是,我怎么能拆的行动
colorLabel
从行动(顶级品牌) playerLabel
(底部标签)?
How to make colorLabels
do their job and to be able to move playerLabel
?如何让
colorLabels
完成他们的工作并能够移动playerLabel
? Basically, How to unblock EDT and still have or actions going.基本上,如何解除对 EDT 的阻止并且仍然有操作。
EDIT:编辑:
static Point click;
static int thisX;
static int xMoved;
static int x;
static Integer elapsed = 0;
public static void main(String[] args) {
JFrame frame = new JFrame();
SpringLayout sp = new SpringLayout();
frame.setLayout(sp);
JLabel label = new JLabel("This is label");
label.setBorder(BorderFactory.createLineBorder(Color.red));
JLabel timeLabel = new JLabel("Time: ");
frame.add(label);
sp.putConstraint(SpringLayout.NORTH, label, 10, SpringLayout.NORTH, frame);
sp.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, frame);
frame.add(timeLabel);
sp.putConstraint(SpringLayout.NORTH, timeLabel, 100, SpringLayout.NORTH, frame);
sp.putConstraint(SpringLayout.WEST, timeLabel, 100, SpringLayout.WEST, frame);
frame.setSize(600, 400);
frame.setVisible(true);
Timer t = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
elapsed++;
timeLabel.setText("Time: " + elapsed);
}
});
label.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
click = e.getPoint();
}
});
label.addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
thisX = label.getLocation().x;
xMoved = (thisX + e.getX()) - (thisX + click.x);
x = thisX + xMoved;
label.setLocation(x, label.getLocation().y);
label.repaint();
}
});
t.start();
}
You will not be able to move around label which have listeners to move.
您将无法在有听众移动的标签周围移动。
That is because you are using a layout manager.那是因为您正在使用布局管理器。 The layout manager will determine the size/location of the component, even if you manually set the location.
即使您手动设置位置,布局管理器也会确定组件的大小/位置。
If you need the ability to dynamically move components then you need to add the component to a panel with a null layout.如果您需要动态移动组件的能力,那么您需要将组件添加到具有空布局的面板。 You will then be responsible for setting the initial size/location of the component.
然后您将负责设置组件的初始大小/位置。
So I would suggest that you:所以我建议你:
Basic Dragging
section from Moving Windows for a simple class that will allow you to drag the label around the panel.Basic Dragging
部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.