为什么JPanel无法渲染

时间:2019-10-21 21:26:11

标签: java swing

我正在尝试使用网格布局,我试图在其中放置一些具有某些数据但没有任何渲染的面板。

这是我当前拥有的代码:

package main.cache.test;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ImageView {

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ImageView window = new ImageView();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ImageView() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        JFrame frmSpitePicker = new JFrame("Title");

        frmSpitePicker.setSize(658, 395);
        frmSpitePicker.setResizable(false);
        frmSpitePicker.setLocationRelativeTo(null);
        frmSpitePicker.getContentPane().setLayout(null);
        frmSpitePicker.setVisible(true);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(12, 35, 620, 303);
        frmSpitePicker.getContentPane().add(scrollPane);

        JPanel panel = new JPanel();
        scrollPane.setViewportView(panel);

        File file = new File("images/");
        panel.setLayout(new GridLayout((file.listFiles().length / 6), 6));

        int i = 0;

        // getting files name from folder
        for (String name : file.list()) {

            JPanel panel_1 = new JPanel();
            panel_1.setBounds(209, 362, 82, 87);
            frmSpitePicker.getContentPane().add(panel_1);
            panel_1.setLayout(null);

            // create label
            JLabel lblNewLabel = new JLabel((i++) + "");
            lblNewLabel.setBounds(12, 13, 56, 16);
            lblNewLabel.setIcon(new ImageIcon(new ImageIcon("images/" + name).getImage().getScaledInstance(8, 8, 1)));
            lblNewLabel.setHorizontalTextPosition(JLabel.CENTER);
            lblNewLabel.setVerticalTextPosition(JLabel.BOTTOM);
            panel_1.add(lblNewLabel);

            // create button
            JButton btnNewButton = new JButton("btn");
            btnNewButton.setBounds(12, 42, 58, 25);
            panel_1.add(btnNewButton);

            // add to the panel
            panel.add(panel_1);

        }
    }
}

我不知道这是怎么回事,为什么不添加Jpanel却无法呈现它,但是添加JLabel却可以。

谢谢!

1 个答案:

答案 0 :(得分:0)

正确使用布局,我们可以轻松获得以下内容:

enter image description here

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

public class ImageView {

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                ImageView window = new ImageView();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public ImageView() {
        initialize();
    }

    private void initialize() {
        int num = 32; // number of images to show..
        JFrame frmSpitePicker = new JFrame("Title");
        frmSpitePicker.setResizable(false);
        JPanel panel = new JPanel(new GridLayout(0, 6));
        JScrollPane scrollPane = new JScrollPane(panel, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        frmSpitePicker.getContentPane().add(scrollPane);

        for (int ii = 1; ii <= num; ii++) {

            JPanel panel_1 = new JPanel(new BorderLayout());

            // create label
            JLabel lblNewLabel = new JLabel(ii + "");
            lblNewLabel.setIcon(new ImageIcon(getImage()));
            lblNewLabel.setHorizontalTextPosition(JLabel.CENTER);
            lblNewLabel.setVerticalTextPosition(JLabel.BOTTOM);
            panel_1.add(lblNewLabel, BorderLayout.CENTER);

            // create button
            JButton btnNewButton = new JButton("btn");
            panel_1.add(btnNewButton, BorderLayout.PAGE_END);

            // add to the panel
            panel.add(panel_1);
            // hack to ensure our scroll bar is active 
            // we require 3 rows to be visible..
            if (ii==18) frmSpitePicker.pack();

        }
        frmSpitePicker.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        // Normally we'd call pack() here!
        //frmSpitePicker.pack();
        frmSpitePicker.setLocationRelativeTo(null);
        frmSpitePicker.setVisible(true);
    }

    java.util.Random r = new java.util.Random();

    private BufferedImage getImage() {
        int s = 16;
        BufferedImage bi = new BufferedImage(
                s, s, BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        g.setColor(new Color(
                r.nextInt(255), r.nextInt(255), r.nextInt(255)));
        g.fillRect(0, 0, s, s);
        g.dispose();
        return bi;
    }
}

请注意,在这种情况下,我倾向于使用JListpanel_1将成为封装标签和按钮的POJO的呈现器。但是可能不需要该按钮,因为列表可以包含用于选择和激活的侦听器。如果这就是按钮的功能,那么它将在列表中是多余的,并且lblNewLabel可以替换整个panel_1

顺便说一句-请确保所有需要的资源都可用于运行代码。当涉及图像时,我们可能会热链接(通过URL加载)到net 1 上可用的图像,或通过代码生成它们(如此处所做)。

  1. 获取图像的一种方法是,热链接到this Q&A中看到的图像。例如。 This answer热链接到嵌入在this question中的图像。