简体   繁体   中英

Problem with EDT (Java) how to split few actions/events

Basically, I have a JFrame & in this frame there is a 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 ). 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. On bottom playerLabel need to be move around x-axis so it can block shots.

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). I mean they work but at the same time, playerLabel get back to original position. (EDT blocked)

My question is, how can I split actions on colorLabel (top labels) from action on playerLabel (bottom label)?

How to make colorLabels do their job and to be able to move playerLabel ? Basically, How to unblock EDT and still have or actions going.

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.
  2. Add your "text label" to the BorderLayout.PAGE_START.
  3. Add a JPanel with a null layoout to the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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