gpt4 book ai didi

java - 将 JScrollPane 添加到从 JPanel 扩展的类

转载 作者:行者123 更新时间:2023-12-01 18:34:17 25 4
gpt4 key购买 nike

我正在创建一个显示来自网络的文件和文件夹列表的应用程序。如何将 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 扩展的类。

You can set the horizontal and vertical scroll bars visibility also.

<小时/>

-- 编辑--

请查看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);
}

关于java - 将 JScrollPane 添加到从 JPanel 扩展的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22749647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com