java怎么让jpanel移动,我的jpanel上 放着照片

xiexiexiexie... xiexie xiexie 展开
 我来答
skyyks0001
2010-02-19 · TA获得超过430个赞
知道小有建树答主
回答量:228
采纳率:100%
帮助的人:405万
展开全部
首先将JPanel的容器的布局管理器设为null 即绝对布局
然后移动的时候只需要修改JPanel的bounds就行了

附一个简单的示例代码

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
*
* @author Jeky
*/
public class MainFrame extends JFrame {

private JPanel movePanel;
private JButton button;

public MainFrame() {
movePanel = new JPanel();
movePanel.setBorder(BorderFactory.createRaisedBevelBorder());
movePanel.setBounds(new Rectangle(50, 50, 50, 50));

button = new JButton("移动");
button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
Rectangle bounds = movePanel.getBounds();
Point location = bounds.getLocation();
location.x+=5;
location.y+=5;
bounds.setLocation(location);
movePanel.setBounds(bounds);
}
});

JPanel center = new JPanel();
center.setLayout(null);
center.add(movePanel);

JPanel south = new JPanel();
south.setLayout(new FlowLayout(FlowLayout.CENTER));
south.add(button);

this.getContentPane().add(center,BorderLayout.CENTER);
this.getContentPane().add(south,BorderLayout.SOUTH);
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

public static void main(String[] args) {
new MainFrame();
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式