简体   繁体   中英

How can I draw multiple JLabels in different places? (icons don't show up)

I am trying to draw multiple icons in many different locations using for loop. For some reason they just dont show up. I don't know what I'm missing - only clue I found was about setting layout to null, but IntelliJ overwrites it. Icons should show up in "mapa" panel. I tried adding them manually, but nothing changed.

My code:

package okna;

import logistyka.Walker;
import logistyka.errand.Errand;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class WalkerGUI extends JFrame{
    private JPanel mainPanel;
    private JLabel walkerName;
    private  JTextField walkerNameField;
    private JLabel walkerAddress;
    private  JTextField walkerAddressField;
    private JButton searchErrandsButton;
    private JComboBox errandsComboBox;
    private JLabel errandsList;
    private JTextField walletValue;
    private JLabel Wallet;
    private JRadioButton archivalErrandsRadioButton;
    private JRadioButton currentErrandsRadioButton;
    private JButton payOutButton;
    private JButton seeProfileButton;
    private JPanel mapa;
    ArrayList<Errand> masterErrandList = new ArrayList<>();

    public WalkerGUI(Walker w, ArrayList<Errand> masterErrandList)  {
        setContentPane(mainPanel);
        ArrayList<JLabel> errandsLabels = new ArrayList<>();
        ImageIcon x = new ImageIcon(System.getProperty("user.dir") + "\\src\\x.png");
        this.masterErrandList = masterErrandList;
        for (Errand errand: masterErrandList) {
            JLabel label = new JLabel("whtvr");
            label.setIcon(x);
            label.setLocation((int)errand.getAddress().getX(),(int)errand.getAddress().getY());
            label.setMinimumSize(new Dimension(20,20));
            label.setPreferredSize(new Dimension(20,20));
            label.setVisible(true);
            errandsLabels.add(label);
        }

        walkerNameField.setText(w.getDescription().getName());
        walkerAddressField.setText(w.getDescription().getHomeRegion().getCurrentAddress().toString());
        walletValue.setText(String.valueOf(w.getWalletSatus()));
        seeProfileButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame frame = new ProfileGUI(w);
                frame.pack();
                frame.setVisible(true);
            }
        });
        payOutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                w.setWalletSatus(0);
                walletValue.setText(String.valueOf(w.getWalletSatus()));
            }
        });
    }


}

You can use SpringLayout (rather than null layout). Method Errand.getAddress().getX() will return the distance of the left edge of the JLabel from the left edge of its container while Errand.getAddress().getY() will return the distance of the top edge of the JLabel from the top edge of its container.

The code in your question is not a minimal, reproducible example so the below code should be considered as a proof of concept .

import java.awt.EventQueue;
import java.awt.Point;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SpringLayout;

public class SprinGui {

    private void createAndDisplayGui() {
        JFrame frame = new JFrame("Spring");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel contentPane = (JPanel) frame.getContentPane();
        SpringLayout layout = new SpringLayout();
        contentPane.setLayout(layout);
        Point[] points = new Point[]{new Point(5, 5),
                                     new Point(5, 100),
                                     new Point(225, 150),
                                     new Point(250, 210)};
        Class<?> meClass = getClass();
        Icon[] icons = new Icon[4];
        icons[0] = new ImageIcon(meClass.getResource("coffee.png"));
        icons[1] = new ImageIcon(meClass.getResource("dollar.png"));
        icons[2] = new ImageIcon(meClass.getResource("dynamite.png"));
        icons[3] = new ImageIcon(meClass.getResource("soccer-player.png"));
        for (int i = 0; i < 4; i++) {
            JLabel label = new JLabel(icons[i]);
            contentPane.add(label);
            layout.putConstraint(SpringLayout.WEST,
                                 label,
                                 points[i].x,
                                 SpringLayout.WEST,
                                 contentPane);
            layout.putConstraint(SpringLayout.NORTH,
                                 label,
                                 points[i].y,
                                 SpringLayout.NORTH,
                                 contentPane);
        }
        frame.setSize(450, 300);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SprinGui instance = new SprinGui();
        EventQueue.invokeLater(() -> instance.createAndDisplayGui());
    }
}

This is how the GUI looks.

屏幕截图

Note that How to Use Icons may also be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM