开发者

Generating JLabels on demand

开发者 https://www.devze.com 2023-03-21 07:10 出处:网络
I would like to be able to generate a map, of sorts, which places small JLabels at coordinate locations on a panel. The problem is that I need them to be randomly generated, so I don\'t know in advanc

I would like to be able to generate a map, of sorts, which places small JLabels at coordinate locations on a panel. The problem is that I need them to be randomly generated, so I don't know in advance how many I will have. Is there a way to do that?

I hope this isn't breaking any Java coding taboos - I'm self-taught.

*Edit: I know I was vague - my program is huge and cumbersome and I have developed my own conventions (which I'm sure would raise the hackles of real java coders :-P) I should have specified that I have a class Location, and I can easily generate random locations. The trouble I have is in creating a new jLabel for each of those locations. Here's what I have:

//Method called after a new Location has been created, to add it to the map
public void addLocation(Location newLocation)
{
    int xx = newLocation.getXloc(); 
    int yy = newLocation.getYloc();

    for (int i=0;i<1;i++)
    {
        JLabel tempLabel = new JLabel(); //tempLabel instantiated elsewhere (is that a problem?)
        tempLabel.setBackground(Color.BLACK);
        tempLabel.setBounds(xx,yy,3,3);
        Map.add(tempLabel); //Map is a JPanel with null layout manager 
        tempLabel.setVisible(true);
    }
}

The problem is that it doesn't seem to do anything. No black dots appear on the map. Maybe now it's as simple as incorrect implementat开发者_JAVA技巧ion of adding a label to a panel?


but do you happen to know if it's possible to add a mouselistener to each of these jlabels as they're created?

Sure its possible. You don't even need to create a new listener every time. You can create one listener that is used by all labels. Then inside the listener code you use:

JLabel label = (JLabel)event.getSource(); 

and you have access to the label that generated the MouseEvent.


Here is some code that might help you in some way, your question was a little vague on the details but i think this is what you are looking for.

public void generate(JPanel panel)
{
    int panelWidth = 500;
    int panelHeight = 500;
    JLabel label;

    Random random = new Random(Calendar.getInstance().getTimeInMillies());
    int numberOfLabels = random.nextInt(100);
    for(int x=0;x<numberOfLabels;x++)
    {
        int locX = random.nextInt(panelWidth);
        int locY = random.nextInt(panelHeight);

        label = new JLabel("Hello World!!");
        label.setLocation(locX,locY);
        label.setVisible(true);
        panel.add(label);
    }
}

If this is not the answer you are looking for or you need more help, let me know ill be glad to help. Hope this helped!!

Update: This is the way you would do it with a MouseListener:

public void generate(JPanel panel)
{
    int panelWidth = 500;
    int panelHeight = 500;
    JLabel label;

    Random random = new Random(Calendar.getInstance().getTimeInMillies());
    int numberOfLabels = random.nextInt(100);
    for(int x=0;x<numberOfLabels;x++)
    {
        int locX = random.nextInt(panelWidth);
        int locY = random.nextInt(panelHeight);

        label = new JLabel("Hello World!!");
        label.setName("World Label");
        // If some of your labels have the same Text then you should set a different
        //     name to each one so that you can tell the difference between them
        //     when you handle the mouse events.
        label.addMouseListener(new PlayerListener());
        // PlayerListener would be the class that implements MouseListener
        label.setLocation(locX,locY);
        label.setVisible(true);
        panel.add(label);
    }
}

This would be your MouseListener:

public void mousePressed(MouseEvent e)
{
    JLabel label = (JLabel)e.getSource();
    if(label.getName().equalsIgnoreCase("World Label"))
    {
        System.out.println("Hello World!!");
    }
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}

This MouseListener should only be added to JLabels because of the type cast. Hopefully this helped!

0

精彩评论

暂无评论...
验证码 换一张
取 消