繁体   English   中英

将JScrollPane添加到从JPanel扩展的类中

[英]Adding JScrollPane to a class that extend from JPanel

我正在创建一个显示来自网络的文件和文件夹列表的应用程序。如何将JScrollPane添加到从JPanel扩展的类中。 JPanel包含指示文件和文件夹的JButtons

package fileviewer;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/*
 * @author      : GUNNER.
 * Title        : Explorer. 
 * Description  : You can browse the folders and download the files using this.
 */

@SuppressWarnings("serial")
public class FoldersAndFiles extends JPanel {
    private final static int PADDING = 20;
    private ArrayList<FolderAndFilePaths> totalFoldersAndFiles;
    @SuppressWarnings("unused")
    private ArrayList<JButton> buttons;
    private BufferedImage myImage;

    public FoldersAndFiles() {
        // super("Explorer");
        setLayout(new FlowLayout(FlowLayout.LEFT, PADDING, PADDING));
        buttons = new ArrayList<JButton>();
        totalFoldersAndFiles = new ArrayList<FolderAndFilePaths>();

    }

    public void addToTotalFolderAndFiles(FolderAndFilePaths folderAndFile) {
        if (this.totalFoldersAndFiles != null)
            this.totalFoldersAndFiles.add(folderAndFile);
        createFoldersAndFiles(folderAndFile);
    }

    private void setGBC(GridBagConstraints g, int width, int height, int gridX,
            int gridY) {
        g.gridheight = height;
        g.gridwidth = width;
        g.gridx = gridX;
        g.gridy = gridY;
    }

    private void createFoldersAndFiles(FolderAndFilePaths folderAndFile) {

        FolderClickHandler folderClickHandler = new FolderClickHandler();
        FileClickHandler fileClickHandler = new FileClickHandler();

        // Here is the button, Panel and layouts which we set for the explore.
        JButton button = new JButton();
        JPanel panel = new JPanel();
        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();

        // This loop adds the available folders in the given path
        for (String folder : folderAndFile.getFolderNames()) {
            button = new JButton();
            panel = new JPanel();
            panel.setLayout(layout);

            // Here we set the image for the folder.
            try {
                myImage = ImageIO.read(new File("res/folder.png"));
                button.setIcon(new ImageIcon(myImage));
                new JLabel("file").setLabelFor(button);
            } catch (IOException e) {
                e.printStackTrace();
            }

            button.setName("f" + folder);

            // This is to set the backgrouond of the button as transparent.
            button.setOpaque(false);
            button.setContentAreaFilled(false);
            button.setBorderPainted(false);

            // Here we set the Constraints for the buttons.
            setGBC(constraints, 1, 1, 0, 0);
            layout.setConstraints(button, constraints);
            panel.add(button);

            // This registers the Event Handler for the folder.
            button.addMouseListener(folderClickHandler);

            // This sets the label and Constraints for the corresponding folder.
            JLabel label = new JLabel(folder);
            setGBC(constraints, 1, 1, 0, 1);
            layout.setConstraints(label, constraints);
            panel.add(label);
            add(panel);
        }

        // This loop adds the available files in the given path
        for (String file : folderAndFile.getFileNames()) {
            button = new JButton();
            panel = new JPanel();
            panel.setLayout(layout);

            // Here we set the image for the files.
            try {
                myImage = ImageIO.read(new File("res/file.png"));
                button.setIcon(new ImageIcon(myImage));
            } catch (IOException e) {
                e.printStackTrace();
            }

            // This is to set the backgrouond of the button as transparent.
            button.setOpaque(false);
            button.setContentAreaFilled(false);
            button.setBorderPainted(false);

            button.setName("F" + file);

            setGBC(constraints, 1, 1, 0, 0);
            layout.setConstraints(button, constraints);
            panel.add(button);

            // This register the Event Handler for the files.
            button.addMouseListener(fileClickHandler);

            // This sets the label and Constraints for the corresponding file.
            JLabel label = new JLabel(file);
            setGBC(constraints, 1, 1, 0, 1);
            layout.setConstraints(label, constraints);
            panel.add(label);
            add(panel);
        }

        setVisible(true);
    }

    private class FolderClickHandler extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent event) {
            // This is used to set the background color for the selected file or
            // folder.
            if (event.getClickCount() == 1) {
                // Here we get the button and set color.

            }

            // This is used to check for double clicks.
            if (event.getClickCount() == 2) {
                // We print the folder / file name to start with
                System.out.println(((JButton) event.getSource()).getName());
            }

        }

    }

    private class FileClickHandler extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent event) {
            // This is used to set the background color for the selected file or
            // folder.
            if (event.getClickCount() == 1) {
                // Here we get the button and set color.

            }

            // This is used to check for double clicks.
            if (event.getClickCount() == 2) {
                // We print the folder / file name to start with
                System.out.println(((JButton) event.getSource()).getName());
            }

        }

    }

}

试试这个

    JPanel panel=new MyPanel();
    JScrollPane pane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.getViewport().add(panel);

MyPanel是从JPanel扩展的类。

您还可以设置水平和垂直滚动条的可见性。


-编辑-

请看一下main方法。

public static void main(String[] a) {
    JFrame fram = new JFrame();
    FoldersAndFiles b = new FoldersAndFiles();

    JScrollPane pane = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    pane.getViewport().add(b);
    fram.getContentPane().add(pane);

    b.addToTotalFolderAndFiles(new FolderAndFilePaths());
    fram.pack();
    fram.setVisible(true);
    fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

首先通读如何使用滚动窗格

至少有两个是这样做的...

首先,通过构造函数指定视图组件。

JScrollPane scrollPane = new JScrollPane(panel);

其次, 查看setViewportView方法...

scrollPane.setViewportView(panel);

或者,您可以直接设置JViewports视图...

scrollPane.getViewport().setView(panel);

确保将滚动窗格添加到显示中...例如...

JFrame frame = ...
JPanel panel = ...
frame.add(new JScollPsne(panel));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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