Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getComponentAt()找不到组件?_Java_Swing_Jframe_Contentpane - Fatal编程技术网

Java getComponentAt()找不到组件?

Java getComponentAt()找不到组件?,java,swing,jframe,contentpane,Java,Swing,Jframe,Contentpane,我正在使用JFrame设置一个纸牌游戏,使用扩展JLabel的卡,以便可以在屏幕上拖动纸牌。然而,我的一个要求是,我能够双击一张卡,它可以捕捉到4叠ACE。这不是问题。造成问题的是,我将其设置为一个数组,这样ace将到达与ace套装相对应的位置,然后如果将一张卡拖到那里,我将重新排列这些位置。如果双击,则ACE必须转到第一个点(从左侧),然后是第二个点,依此类推。我计划使用getComponentAt()来找出第一个位置是什么,如果我能把ace放在那里,如果不能,我就转到第二个位置,依此类推。但

我正在使用
JFrame
设置一个纸牌游戏,使用扩展
JLabel
的卡,以便可以在屏幕上拖动纸牌。然而,我的一个要求是,我能够双击一张卡,它可以捕捉到4叠ACE。这不是问题。造成问题的是,我将其设置为一个数组,这样ace将到达与ace套装相对应的位置,然后如果将一张卡拖到那里,我将重新排列这些位置。如果双击,则ACE必须转到第一个点(从左侧),然后是第二个点,依此类推。我计划使用
getComponentAt()
来找出第一个位置是什么,如果我能把ace放在那里,如果不能,我就转到第二个位置,依此类推。但是,出于某种原因,即使我将参数硬编码到
getComponentAt()
中,以使我知道有一个组件,它仍然返回null

这是我的代码的相关部分:

Container contentPane = getContentPane();
contentPane.setLayout(null);
...
for(int i = 0; i < 4; i++)
{
    aces[i] = new Card();
    aces[i].setBounds((i * 75) + 475, 25, 75, 100);
    contentPane.add(aces[i]);
    aces[i].setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
    aces[i].setSuit(i + 1);

}
System.out.println(contentPane.getComponentAt(475, 25));
这是班级卡:

import javax.swing.*;
public class Card extends JLabel
{
    private int numb;

    private int value;

    private int suit;

    public Card(int n, int s)
    {
        super();
        numb = n;
        suit = s;
    }
}

这是为我运行的,如果需要更多解决问题,请发表评论。

使用
getComponentAt

首先,父容器必须实际实现并调整大小。如果不是,它将返回
null
。如果该位置不存在子对象,则该方法能够返回容器本身

然后,我使用child
Component#contains
来测试给定的点是否直接在子组件中,但这需要将该点转换为子组件的坐标空间

因此,我直接进入了
getBounds

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestComponentLocation {

    public static void main(String[] args) {
        new TestComponentLocation();
    }

    public TestComponentLocation() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.pack();
                frame.setLocationRelativeTo(null);


                Container contentPane = frame.getContentPane();
                contentPane.setLayout(null);
                for (int i = 0; i < 4; i++) {
                    JPanel panel = new JPanel();
                    panel.setBounds((i * 75) + 475, 25, 75, 100);
                    System.out.println(panel.getBounds());
                    contentPane.add(panel);
                    panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
                }
                System.out.println(getComponentAt(contentPane, new Point(475, 25)));
                System.out.println(getComponentAt(contentPane, new Point(100, 25)));

                frame.setVisible(true);

            }
        });
    }

    public Component getComponentAt(Container parent, Point p) {
        Component comp = null;
        for (Component child : parent.getComponents()) {
            if (child.getBounds().contains(p)) {
                comp = child;
            }
        }
        return comp;
    }
}
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.Container;
导入java.awt.EventQueue;
导入java.awt.Point;
导入javax.swing.BorderFactory;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类TestComponentLocation{
公共静态void main(字符串[]args){
新的TestComponentLocation();
}
公共TestComponentLocation(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.pack();
frame.setLocationRelativeTo(空);
容器contentPane=frame.getContentPane();
contentPane.setLayout(null);
对于(int i=0;i<4;i++){
JPanel面板=新的JPanel();
面板立根((i*75)+475,25,75,100);
System.out.println(panel.getBounds());
contentPane.add(面板);
panel.setboorder(BorderFactory.createLineBorder(Color.BLACK,3));
}
System.out.println(getComponentAt(contentPane,newpoint(475,25));
System.out.println(getComponentAt(contentPane,newpoint(100,25));
frame.setVisible(true);
}
});
}
公共组件getComponentAt(容器父级,点p){
组件comp=null;
对于(组件子级:parent.getComponents()){
if(child.getBounds()包含(p)){
comp=儿童;
}
}
返回补偿;
}
}

要更快地获得更好的帮助,请发布一个.Use
contentpane.getComponentAt(index)
@andrewhompson。那么您是否将该响应编程为功能键?;)@不,不。我有一个网页,上面有一些常见的回答。它比功能键更容易复制/粘贴。;)
getComponentAt
首先检查给定的坐标是否在其自身(即内容窗格)内,如果不在其内,则返回
null
。在添加“卡片”之前和之后,我通过调用
setVisible
对此进行了测试,如果框架首先可见,那么它工作正常,否则它返回null…您还应该注意,如果在该点不存在其他组件,它将返回自身OK,我刚刚为getComponentAt()添加了your代码到我的SSCCE版本,它将返回我的帧,而不是475,25处的组件。我的卡有什么特别的地方不起作用吗?不过,我只是用你的代码用JPanels代替卡试过了,它起作用了。所以这是关于卡片的,JLabel是否必须有一个imageIcon/不透明才能让getComponentAt()找到它?应该没有什么区别。只需确保您传递的是包含要查找的孩子的直系家长。该方法是基于子对象的边界工作的。我刚刚确认,我将其添加到contentPane中,使其成为父对象,对吗?因为如果这一直是我的问题,我会觉得自己非常愚蠢。是的,所以在你的情况下,你会做一些类似于
getComponentAt(getContentPane(),point)
。。。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Point;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestComponentLocation {

    public static void main(String[] args) {
        new TestComponentLocation();
    }

    public TestComponentLocation() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.pack();
                frame.setLocationRelativeTo(null);


                Container contentPane = frame.getContentPane();
                contentPane.setLayout(null);
                for (int i = 0; i < 4; i++) {
                    JPanel panel = new JPanel();
                    panel.setBounds((i * 75) + 475, 25, 75, 100);
                    System.out.println(panel.getBounds());
                    contentPane.add(panel);
                    panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 3));
                }
                System.out.println(getComponentAt(contentPane, new Point(475, 25)));
                System.out.println(getComponentAt(contentPane, new Point(100, 25)));

                frame.setVisible(true);

            }
        });
    }

    public Component getComponentAt(Container parent, Point p) {
        Component comp = null;
        for (Component child : parent.getComponents()) {
            if (child.getBounds().contains(p)) {
                comp = child;
            }
        }
        return comp;
    }
}