在JFrame上放了两个JLabel,当把A移动(setLocation)到新位置后.改变了B的文字(setText),A又回到了原来的初始位置////////////////源码,可直接使用////////////////////////////
package chip.test;import java.awt.*;import javax.swing.*;
import java.awt.BorderLayout;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import chip.AppChip;
import chip.FrameMain;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class Frame1 extends JFrame {
    JPanel pnl = (JPanel)getContentPane();    JLabel lblMove = new JLabel();
    XYLayout xYLayout1 = new XYLayout();
    JLabel lblchange = new JLabel();
    JButton btnMove = new JButton();
    JButton btnReset = new JButton();
    JButton btnChange = new JButton();
    public Frame1() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }    private void jbInit() throws Exception {
        setSize(640,480);
        pnl.setLayout(xYLayout1);
        lblMove.setText("被移动的标签!");
        lblchange.setText("将要改变的文字的标签");
        btnMove.setText(" 移 动 ");
        btnMove.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                lblMove.setLocation(300,400);
            }
        });        btnReset.setText(" 还 原 ");
        btnReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 lblMove.setLocation(20,50);
                 lblchange.setText("将要改变的文字的标签");
                 lblchange.setForeground(Color.BLACK);
            }
        });
                btnChange.setText("改变文字");
        btnChange.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lblchange.setText("改变了文字此标签的文字,但并没有改变[lblmove]的位置,但它却回到原来的位置") ;
                lblchange.setForeground(Color.red);
            }
        });
        
        pnl.add(btnMove, new XYConstraints(70, 224, 90, 26));
        pnl.add(btnReset, new XYConstraints(266, 224, 90, 26));
        pnl.add(btnChange, new XYConstraints(168, 224, 90, 26));
        
        pnl.add(lblMove, new XYConstraints(20, 50, -1, -1));
        pnl.add(lblchange, new XYConstraints(20, 100, -1, -1));
    }
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new Frame1();
                frame.validate();
                Dimension screenSize = Toolkit.getDefaultToolkit().
                                       getScreenSize();
                Dimension frameSize = frame.getSize();
                if (frameSize.height > screenSize.height) {
                    frameSize.height = screenSize.height;
                }
                if (frameSize.width > screenSize.width) {
                    frameSize.width = screenSize.width;
                }
                frame.setLocation((screenSize.width - frameSize.width) / 2,
                                  (screenSize.height - frameSize.height) / 2);
                frame.setVisible(true);
            }
        });
    }
}

解决方案 »

  1.   

    如果你用XYLayout,就必须这样写移动,因为布局是通过layout来设置的
    btnMove.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            pnl.remove(lblMove);
            pnl.add(lblMove, new XYConstraints(300, 400, -1, -1));
            pnl.validate();
           // lblMove.setLocation(300, 400);
          }
        });
    或者将layout设置为null
    this.getContentPane().setLayout(null);
    这样就可以通过设置location来改变位置了
      

  2.   

    楼上的方法不错.如果使用remove,add方式又遇到新问题了.如果有两个或以的标签时,重叠时,标签置前的问题怎么处理呢?将layout设置为null,印像中好像有副作用的!