简体   繁体   English

EDT(Java)的问题如何拆分几个动作/事件

[英]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:所以我建议你:

  1. Uuse the default BorderLayout of the frame.使用框架的默认 BorderLayout。
  2. Add your "text label" to the BorderLayout.PAGE_START.将您的“文本标签”添加到 BorderLayout.PAGE_START。
  3. Add a JPanel with a null layoout to the BorderLayout.CENTER将具有空布局的 JPanel 添加到 BorderLayout.CENTER
  4. Add the label you want to drag to the above panel将要拖动的标签添加到上面的面板
  5. See the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM