Java 如何在运行时移动jpanel
我正在开发一个应用程序,我希望jpanel在运行时在jframe中移动。这是我使用的代码Java 如何在运行时移动jpanel,java,swing,Java,Swing,我正在开发一个应用程序,我希望jpanel在运行时在jframe中移动。这是我使用的代码 package testing; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class ChangePanel extends javax.swing.JFrame { public ChangePanel() { initComponents(); } @SuppressWa
package testing;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ChangePanel extends javax.swing.JFrame {
public ChangePanel() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jPanel1MousePressed(evt);
}
});
jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
jPanel1MouseDragged(evt);
}
});
jLabel1.setText("panel to move");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(jLabel1)
.addContainerGap(119, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(62, 62, 62)
.addComponent(jLabel1)
.addContainerGap(81, Short.MAX_VALUE))
);
getContentPane().add(jPanel1);
jPanel1.setBounds(160, 60, 247, 161);
pack();
}// </editor-fold>
private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {
MouseAdapter ma = new MouseAdapter() {
int lastX =0, lastY =0;
@Override
public void mouseDragged(MouseEvent e) {
int x = e.getXOnScreen();
int y = e.getYOnScreen();
// Move frame by the mouse delta
setLocation(getLocationOnScreen().x + x - lastX,
getLocationOnScreen().y + y - lastY);
lastX = x;
lastY = y;
}
};
}
private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {
MouseAdapter ma = new MouseAdapter() {
int lastX, lastY;
@Override
public void mousePressed(MouseEvent e) {
lastX = e.getXOnScreen();
lastY = e.getYOnScreen();
}};
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ChangePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ChangePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ChangePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ChangePanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ChangePanel().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
封装测试;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
公共类变更面板扩展了javax.swing.JFrame{
公共变更面板(){
初始化组件();
}
@抑制警告(“未选中”)
//
私有组件(){
jPanel1=newjavax.swing.JPanel();
jLabel1=newjavax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
setBorder(javax.swing.BorderFactory.create蚀刻边界());
addMouseListener(新java.awt.event.MouseAdapter(){
public void mousePressed(java.awt.event.MouseEvent evt){
JPANEL1鼠标按下(evt);
}
});
addMouseMotionListener(新的java.awt.event.MouseMotionAdapter(){
public void mouseDragged(java.awt.event.MouseEvent evt){
JPANEL1鼠标标记(evt);
}
});
jLabel1.setText(“要移动的面板”);
javax.swing.GroupLayout jPanel1Layout=新的javax.swing.GroupLayout(jPanel1);
setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jpanellayout.createSequentialGroup()
.addGap(56,56,56)
.addComponent(jLabel1)
.addContainerGap(119,简称最大值))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jpanellayout.createSequentialGroup()
.addGap(62,62,62)
.addComponent(jLabel1)
.addContainerGap(81,简称最大值))
);
getContentPane().add(jPanel1);
jPanel1.挫折(160、60、247、161);
包装();
}//
私有void jpanelmousedragged(java.awt.event.MouseEvent evt){
MouseAdapter ma=新的MouseAdapter(){
int lastX=0,lastY=0;
@凌驾
公共无效鼠标标记(鼠标事件e){
int x=e.getXOnScreen();
int y=e.getYOnScreen();
//用鼠标移动帧
设置位置(getLocationOnScreen().x+x-lastX,
getLocationOnScreen().y+y-lastY);
lastX=x;
lastY=y;
}
};
}
私有void jpanelmousepressed(java.awt.event.MouseEvent evt){
MouseAdapter ma=新的MouseAdapter(){
int lastX,lastY;
@凌驾
公共无效鼠标按下(MouseEvent e){
lastX=e.getXOnScreen();
lastY=e.getYOnScreen();
}};
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串参数[]){
/*设置Nimbus的外观和感觉*/
//
/*如果Nimbus(在JavaSE6中引入)不可用,请使用默认的外观。
*详情请参阅http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
试一试{
for(javax.swing.UIManager.LookAndFeelInfo:javax.swing.UIManager.getInstalledLookAndFeels()){
if(“Nimbus”.equals(info.getName())){
setLookAndFeel(info.getClassName());
打破
}
}
}捕获(ClassNotFoundException ex){
getLogger(ChangePanel.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(实例化异常){
getLogger(ChangePanel.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}捕获(非法访问例外){
getLogger(ChangePanel.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}catch(javax.swing.UnsupportedLookAndFeelException ex){
getLogger(ChangePanel.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
}
//
/*创建并显示表单*/
invokeLater(new Runnable()){
公开募捐{
新建ChangePanel().setVisible(true);
}
});
}
//变量声明-不修改
私有javax.swing.JLabel jLabel1;
私有javax.swing.JPanel jPanel1;
//变量结束声明
}
但是不起作用不久前,我已经为这些任务编写了一个框架。也许您会发现它很有用(该库是开源的): 教程: Javadoc: 有关最新版本的信息:
无需在已定义的侦听器中放置
MouseAdapter
。事实上,您只是简单地声明了一个,而没有以任何方式使用它。只需将其保存在拖动的事件中即可
像这样。您可以根据需要进行的计算对其进行调整
private void jPanel1MouseDragged(java.awt.event.MouseEvent evt) {
jPanel1.setLocation(evt.getXOnScreen(), evt.getYOnScreen());
}
无法工作,请您详细解释无法工作的原因。它会崩溃吗?面板没有移动吗?对布局管理器以及它们提供的功能进行一些研究也可以看到。谢谢,我这样做了,它可以工作,但是当点击并拖动它时,它可以工作,但是鼠标指针远离jpanel