Java setLocation()在新的keyEvent之后重置

时间:2014-09-04 18:06:37

标签: java

首先,抱歉英语不好。

我有一个问题。我编写了一个简单的keyEvent,它可以移动我的JLabel。它工作正常,但当我要求我的代码使用keyEvent更改setText时,他将重置我的setLocation()。这是我的代码:

package view;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import controller.Keylisteners;

@SuppressWarnings("serial")
public class Builder extends JFrame {

public JLabel plaatje = new JLabel();
public static Builder instance;

public static Builder getInstance(){
    if(instance == null){
        instance = new Builder();
    }
    return instance;
}

public Builder() {
    setSize(getMaximumSize());
    this.addKeyListener(new Keylisteners());
    setLayout(null);
    //plaatje.setText("UP");
    GroupLayout groupLayout = new GroupLayout(getContentPane());
    groupLayout.setHorizontalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                .addContainerGap(595, Short.MAX_VALUE)
                .addComponent(plaatje)
                .addGap(567))
    );
    groupLayout.setVerticalGroup(
        groupLayout.createParallelGroup(Alignment.LEADING)
            .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                .addContainerGap(570, Short.MAX_VALUE)
                .addComponent(plaatje, GroupLayout.PREFERRED_SIZE, 65,       GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
    );
    getContentPane().setLayout(groupLayout);


} 
}

另一堂课。

package controller;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import view.Builder;

public class Keylisteners extends KeyAdapter{



public void keyPressed(KeyEvent e) {
     if (e.getKeyCode() == KeyEvent.VK_UP) {
 Builder.getInstance().plaatje.setText("UP");
         Builder.getInstance().plaatje.setLocation(Builder.getInstance().plaatje.getX(),      Builder.getInstance().plaatje.getY()-5);
         Builder.getInstance().plaatje.repaint();
        }
     else if (e.getKeyCode() == KeyEvent.VK_DOWN) { 
     Builder.getInstance().plaatje.setText("DOWN");
         Builder.getInstance().plaatje.setLocation(Builder.getInstance().plaatje.getX(), Builder.getInstance().plaatje.getY()+5);
         Builder.getInstance().plaatje.repaint();
        }
     else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
   Builder.getInstance().plaatje.setText("LEFT");
         Builder.getInstance().plaatje.setLocation(Builder.getInstance().plaatje.getX()-5, Builder.getInstance().plaatje.getY());
         Builder.getInstance().plaatje.repaint();
        }
     else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
      Builder.getInstance().plaatje.setText("RIGHT");
         Builder.getInstance().plaatje.setLocation(Builder.getInstance().plaatje.getX()+5, Builder.getInstance().plaatje.getY());
         Builder.getInstance().plaatje.repaint();
        }

}

@Override
public void keyReleased(KeyEvent e) {


}

@Override
public void keyTyped(KeyEvent e) {


} 
}

我希望有人可以提供帮助。

2 个答案:

答案 0 :(得分:0)

您的问题是,即使您在框架上将layoutmanager设置为null,您也已将其设置为contentPane上的GroupLayout

getContentPane().setLayout(groupLayout);

当您手动调用setLocation时,这是有效的,因为您的contentpane未重新验证。但是当你调用setText时,它会调用validate()。

因此,如果您想要移动组件,我的建议是将您的contentpane上的布局设置为null

答案 1 :(得分:0)

尝试使用AbsoluteLayout。 setLayout(null) 更改标签的文本会导致容器valitdate()。这意味着容器重新计算位置,并且您正在使用布局,因此重置。

import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;

import java.awt.event.KeyListener;

@SuppressWarnings("serial")
public class Builder extends JFrame implements KeyListener {

public JLabel plaatje = new JLabel();
public static Builder instance;

public static Builder getInstance() {
    if (instance == null) {
        instance = new Builder();
    }
    return instance;
}

public Builder() {
    setSize(getMaximumSize());
    this.addKeyListener(this);
    setLayout(null);
    plaatje.setText("UP");

    add(plaatje);
    plaatje.setBounds(0, 0, 50, 50);

}

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_UP) {
        Builder.getInstance().plaatje.setText("UP");
        Builder.getInstance().plaatje.setLocation(Builder.getInstance().plaatje.getX(), Builder.getInstance().plaatje.getY() - 5);
        Builder.getInstance().plaatje.repaint();
    } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
        Builder.getInstance().plaatje.setText("DOWN");
        Builder.getInstance().plaatje.setLocation(Builder.getInstance().plaatje.getX(), Builder.getInstance().plaatje.getY() + 5);
        Builder.getInstance().plaatje.repaint();
    } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        Builder.getInstance().plaatje.setText("LEFT");
        Builder.getInstance().plaatje.setLocation(Builder.getInstance().plaatje.getX() - 5, Builder.getInstance().plaatje.getY());
        Builder.getInstance().plaatje.repaint();
    } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        Builder.getInstance().plaatje.setText("RIGHT");
        Builder.getInstance().plaatje.setLocation(Builder.getInstance().plaatje.getX() + 5, Builder.getInstance().plaatje.getY());
        Builder.getInstance().plaatje.repaint();
    }

}

@Override
public void keyReleased(KeyEvent e) {

}

@Override
public void keyTyped(KeyEvent e) {

}

public static void main(String[] args) {
    Builder.getInstance().setVisible(true);
}
}