getComponentAt()没有找到组件? [英] getComponentAt() not finding a component?

查看:199
本文介绍了getComponentAt()没有找到组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 JFrame 来设置一个纸牌游戏,使用扩展 JLabel 的卡片,这样他们就可以了可以在屏幕上拖动。但是,我的要求之一就是我可以双击一张卡片,它可以快速连接到4个A盘。这不是问题。造成问题的原因是我把它设置在一个数组中,这样aces会到达与ace的套装相对应的位置,然后如果卡被拖到那里我会重新排列。如果双击,则A必须转到第一个点(从左侧)到第二个点,依此类推。我本来打算用 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));

每次都返回 null ,不无论在组件中我放置坐标的位置。
任何解释?



*更新的SSCCE:
这是主要类别,单人纸牌:

  import java.util。*; 
import javax.swing。*;
import java.awt。*;

公共类Solitaire扩展JFrame
{
私有Container contentPane;

私人卡aces [];

public static void main(String args [])
{
Solitaire frame = new Solitaire();
frame.setVisible(true);
}

public Solitaire()
{
super();

contentPane = getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.GREEN.darker()。darker());

setTitle(Solitaire);
setSize(800,800);
setResizable(false);
setLocation(250,50);
setDefaultCloseOperation(EXIT_ON_CLOSE);

play();
}

public void play()
{
contentPane.removeAll();

aces = new Card [4];

for(int i = 0; i< 4; i ++)
{
aces [i] = new Card(0,i + 1);
aces [i] .setBounds((i * 75)+ 475,25,75,100);
contentPane.add(aces [i]);
aces [i] .setBorder(BorderFactory.createLineBorder(Color.BLACK,3)); (b i $ 0; i< contentPane.getComponents()。length; i ++)
System.out.println ()[一世]);

System.out.println(contentPane.getComponentAt(475,25));
repaint();
}
}

这是类卡:

  import javax.swing。*; 
公共类卡扩展JLabel
{
private int numb;

private int value;

private int suit;

公共卡(int n,int s)
{
super();
numb = n;
suit = s;
}
}

如果你需要更多的使用 getComponentAt 有很多问题



首先,必须实际实现父容器的大小。如果没有,它将返回 null 。如果该位置不存在子项,则该方法能够返回容器本身。



然后我的子组件#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;

公共类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框架= new JFrame(Testing);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.pack();
frame.setLocationRelativeTo(null);


容器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;
}
}
返回comp;
}
}


I am using a JFrame to set up a game of Solitaire, using Cards that extend JLabel so that they can be dragged around the screen. However, one of my requirements is that I be able to double click a card and it snaps up to the 4 stacks of aces. This is not a problem. what is causing problems is that I set this up in an array, so that the aces would go to the spot that corresponds with the suit of the ace, then I have the spots rearrange if a card is dragged up there. If it is double clicked, the aces must go to the first spot (from the left) then the second, and so on. I had planned to use getComponentAt() to find out what was at the first spot and if I could place the ace there, if not then i move on to the second and so on. For some reason though, even if I hard code in the parameters into getComponentAt() to spots I know have a component, it is still returning null.

this is the relevant portion of my code:

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));

This is returning null every time, no matter where in the component i put the coordinates. Any explanations?

*Updated SSCCE: This is the main class, solitaire:

import java.util.*;
import javax.swing.*;
import java.awt.*;

public class Solitaire extends JFrame
{
    private Container contentPane;

    private Card aces[];

    public static void main(String args[])
    {
        Solitaire frame = new Solitaire();
        frame.setVisible(true);
    }

    public Solitaire()
    {
        super();

        contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.GREEN.darker().darker());

        setTitle("Solitaire");
        setSize(800,800);
        setResizable(false);
        setLocation(250, 50);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        play();
    }

    public void play()
    {
        contentPane.removeAll();

        aces = new Card[4];

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


        }
        for(int i = 0; i < contentPane.getComponents().length; i++)
        System.out.println(contentPane.getComponents()[i]);

        System.out.println(contentPane.getComponentAt(475, 25));
       repaint();
    }
}

and this is Class Card:

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;
    }
}

This ran for me, comment if you need more to solve the problem.

解决方案

There are lots of problems with using getComponentAt

The first is, the parent container must actually be realized and sized. If not, it will return null. The method is capable of returning the container itself if no children exist at the location.

I then child Component#contains to test to see if the given point feel within the child components directly, but this wants the point to be translated into the child's coordinate space...

So instead, I simply went straight to 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;
    }
}

这篇关于getComponentAt()没有找到组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆