how do I add a JLabel to a specific location into my ScrollPane?

拥有回忆 提交于 2019-12-13 16:04:43

问题


Im trying to show images and/or labels in my JPanel. The thing is, I want to show many JLabels in custom locations but currently I can only show like 4 labels. I understand I need to use ScrollPane. I have tried using it but its not letting me alocate them with customs locations. (By custom locations I mean using coords to place my label.)

Here is the code WITHOUT using ScrollPane:

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

public class Images extends JFrame{
    private JPanel contentPane;
    public static void main(String[] args) {
        Images image=new Images();
    }
    public Images(){
        setVisible(true);
        setTitle("myTitle");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 800);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        JLabel lblNombreequipo = new JLabel("trying: ");
        lblNombreequipo.setBounds(147, 110, 145, 14);
        contentPane.add(lblNombreequipo);
        int coordY=150;
        for(int i=0;i<10;i++){
            JLabel myLabel = new JLabel("attempt: "+i);
            myLabel.setBounds(147,coordY, 145, 14);
            contentPane.add(myLabel);
            coordY=coordY+150;
        }

    }

}

回答1:


Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.

Here is an example. Adjust the layout padding and component border parameters to provide the 'exact' requirement & note further comments in the code.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Images extends JFrame {

    private JPanel contentPane;

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

    public Images() {
        setTitle("myTitle");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // setBounds(100, 100, 800, 800); call pack() instead
        contentPane = new JPanel(new BorderLayout());
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        //contentPane.setLayout(null); // never do that!
        JLabel lblNombreequipo = new JLabel("trying: ");
        lblNombreequipo.setBorder(new EmptyBorder(10, 40, 10, 10));
        //lblNombreequipo.setBounds(147, 110, 145, 14);
        contentPane.add(lblNombreequipo, BorderLayout.PAGE_START);
        //int coordY = 150;
        JPanel scrollPanel = new JPanel(new GridLayout(0, 1, 0, 50));
        contentPane.add(new JScrollPane(scrollPanel,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
        for (int i = 0; i < 10; i++) {
            JLabel myLabel = new JLabel("attempt: " + i);
            myLabel.setBorder(new EmptyBorder(20, 140, 20, 140));
            //myLabel.setBounds(147, coordY, 145, 14);
            scrollPanel.add(myLabel);
            //coordY = coordY + 150;
        }
        pack();
        // now the preferred size has been calculated, we can shorten the height
        Dimension d = getSize();
        setSize(new Dimension(d.width, 250));
        setLocationByPlatform(true);
        setMinimumSize(getSize());
        setVisible(true); //should be last
    }
}


来源:https://stackoverflow.com/questions/44465169/how-do-i-add-a-jlabel-to-a-specific-location-into-my-scrollpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!