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

时间:2014-03-30 20:46:17

标签: java swing jpanel jscrollpane

我正在创建一个应用程序,它显示网络中的文件和文件夹列表。如何将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());
            }

        }

    }

}

2 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:2)

首先阅读How to Use Scroll Panes

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

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

JScrollPane scrollPane = new JScrollPane(panel);

其次,与setViewportView方法竞争......

scrollPane.setViewportView(panel);

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

scrollPane.getViewport().setView(panel);

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

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