如何将JLabel添加到ScrollPane中的特定位置? [英] how do I add a JLabel to a specific location into my ScrollPane?

查看:276
本文介绍了如何将JLabel添加到ScrollPane中的特定位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的JPanel中显示图像和/或标签.问题是,我想在自定义位置显示许多JLabel,但目前只能显示4个标签.我了解我需要使用ScrollPane.我已经尝试过使用它,但是它不能让我在海关设置他们的位置. (通过自定义位置,我的意思是使用坐标放置我的标签.)

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

以下是不使用ScrollPane的代码:

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

    }

}

推荐答案

Java GUI必须在不同的语言环境中使用不同的PLAF在不同的OS',屏幕大小,屏幕分辨率等上工作.因此,它们不利于像素的完美布局.而是使用布局管理器或它们的组合以及

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

这篇关于如何将JLabel添加到ScrollPane中的特定位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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