gpt4 book ai didi

java - 调整窗口大小时组件移动到原始位置

转载 作者:太空宇宙 更新时间:2023-11-04 07:08:54 24 4
gpt4 key购买 nike

我有一个可拖动的 JPanel,它使用 NetBeans IDE JFrame 表单编辑器添加到 JFrame 中。当我运行程序并将卡拖动到原始位置以外的位置并调整 JFrame 的大小时,JPanel 将返回到其原始位置。

之前(程序首次运行时):

original position

移动卡:

Moved Card

调整窗口大小:

Resized Window

我不明白为什么会发生这种情况以及如何预防它。

我的框架:

package cards;

import cards.cards.*;
/**
*
* @author Nicki
*/
public class ImgTest extends javax.swing.JFrame {

/**
* Creates new form ImgTest
*/
public ImgTest() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

card2 = new cards.cards.Card();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Imgtest");
setMinimumSize(new java.awt.Dimension(250, 250));
setPreferredSize(new java.awt.Dimension(250, 250));

javax.swing.GroupLayout card2Layout = new javax.swing.GroupLayout(card2);
card2.setLayout(card2Layout);
card2Layout.setHorizontalGroup(
card2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 71, Short.MAX_VALUE)
);
card2Layout.setVerticalGroup(
card2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 96, Short.MAX_VALUE)
);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(card2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(158, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(card2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(133, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

/**
* @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(ImgTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ImgTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ImgTest.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ImgTest.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 ImgTest().setVisible(true);
}
});
}

// Variables declaration - do not modify
private cards.cards.Card card2;
// End of variables declaration
}

我的卡片面板:

package cards.cards;

import cards.images.CardImageProvider;
import java.awt.Canvas;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JPanel;

/**
*
* @author Nicki
*/
public class Card extends JPanel {

private Image cardImg;
private Cards card;
private volatile int draggedAtX, draggedAtY;


public Card(Cards c) {
this.setSize(71, 96);
card = c;
cardImg = CardImageProvider.getCardImage(c);
addMouseListener(new MouseAdapter(){

public void mousePressed(MouseEvent e){
draggedAtX = e.getX();
draggedAtY = e.getY();
}
});

addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
setLocation(e.getX() - draggedAtX + getLocation().x,
e.getY() - draggedAtY + getLocation().y);
repaint();
}
});
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}

public Card(){
this(Unknown.UNKNOWN);
}

@Override
public void paintComponent(Graphics g) {
this.setSize(71, 96);
g.drawImage(cardImg, 0, 0, this);
}

public void setCard(Cards c) {
card = c;
cardImg = CardImageProvider.getCardImage(c);
repaint();
}

public Cards getCard() {
return card;
}


}

我想知道如何防止卡片移回其第一个位置。提前致谢。

最佳答案

默认情况下,IDE 将使用布局管理器在框架上定位组件。

拖动面板时,您将手动设置面板的位置并覆盖布局管理器确定的位置。

但是,当您调整框架大小时,布局管理器代码将再次执行,并且面板将设置回其原始位置。

您需要更改代码,以便您拖动的面板的父面板使用“空布局”。但是,当您执行此操作时,您现在负责设置面板的大小和位置。

关于java - 调整窗口大小时组件移动到原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940913/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com