Java 使用Netbeans IDE动态设计JFrame表单';设计师

Java 使用Netbeans IDE动态设计JFrame表单';设计师,java,swing,netbeans,Java,Swing,Netbeans,我想添加按钮,但它们的数量不确定。添加时,必须动态设置其位置。我使用的是Netbeans IDE的设计器,因此框架布局是GroupLayout。如何在GroupLayout中执行此操作?谢谢你的帮助 这是课堂 package addButton; import javax.swing.JButton; /** * * @author 12043 */ public class MyGui extends javax.swing.JFrame { /** * Creates new

我想添加按钮,但它们的数量不确定。添加时,必须动态设置其位置。我使用的是Netbeans IDE的设计器,因此框架布局是GroupLayout。如何在GroupLayout中执行此操作?谢谢你的帮助

这是课堂

package addButton;

import javax.swing.JButton;

/**
 *
 * @author 12043
 */
public class MyGui extends javax.swing.JFrame {

/**
 * Creates new form MyGui
 */
public MyGui() {
    initComponents();
    addButtons(3);
}

private void addButtons(int numberOfButtons) {
    for (int i = 0; i < numberOfButtons; i++) {
        JButton newButton = new JButton("NewButton" + i);
        newButton.setSize(newButton.getPreferredSize());
        newButton.setLocation(10, i*30);
        newButton.setVisible(true);
        add(newButton);
    }
}
//netbeans code after here
包添加按钮;
导入javax.swing.JButton;
/**
*
*@author 12043
*/
公共类MyGui扩展了javax.swing.JFrame{
/**
*创建新表单MyGui
*/
公共MyGui(){
初始化组件();
添加按钮(3);
}
私有void addButtons(int numberOfButtons){
对于(int i=0;i
框架看起来像:

gui中的按钮将类似于android launcher中的按钮。它们的位置将按行和列排序。但由于按钮的数量不确定,我必须自动执行此操作

gui中的按钮将类似于android launcher中的按钮

它看起来像这样

但当然,台式计算机不理解“滑动”到下一个屏幕,因此我们有键盘和鼠标来更改视图,并滚动长列表的窗格

以下是创建上述GUI的源代码。您可能需要更改顶部的路径和文件类型,但它将在路径字符串中指定的最顶层目录中搜索可执行文件

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.io.*;
import java.util.Vector;
import javax.swing.filechooser.FileSystemView;

public class ExecutableList {

    private JComponent ui = null;
    String exePathString = "C:\\Windows";
    String exeFileType = "exe";

    ExecutableList() {
        initUI();
    }

    public final void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        File[] fAll0 = new File(exePathString).listFiles();
        Vector<File> v = new Vector<>();
        for (File f0 : fAll0) {
            if (f0.isDirectory()) {
                FilenameFilter fNF = new FilenameFilter() {

                    @Override
                    public boolean accept(File dir, String name) {
                        return name.toLowerCase().endsWith("exe");
                    }
                };
                File[] fExe0 = f0.listFiles(fNF);
                System.out.println("fExe0: ");
                if (fExe0 != null 
                        ) {
                        //&& fExe0.length<100) {
                    System.out.println("fExe0: " + fExe0.length);
                    for (File f1 : fExe0) {
                        v.add(f1);
                    }
                }
            }
        }
        System.out.println("v.size(): " + v.size());
        DefaultListModel dlm = new DefaultListModel();
        for (File f : v) {
            dlm.addElement(f);
        }
        JList list = new JList(dlm);
        list.setCellRenderer(new File2CellRenderer());
        list.setVisibleRowCount(v.size()/5);
        list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        list.setFixedCellHeight(20);
        list.setFixedCellWidth(30);
        ui.add(new JScrollPane(
                list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = () -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception useDefault) {
            }
            ExecutableList o = new ExecutableList();

            JFrame f = new JFrame(o.getClass().getSimpleName());
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            f.setLocationByPlatform(true);

            f.setContentPane(o.getUI());
            f.pack();
            Dimension d = f.getSize();
            d = new Dimension(d.width, d.height/3);
            f.setMinimumSize(d);
            f.setSize(d);

            f.setVisible(true);
        };
        SwingUtilities.invokeLater(r);
    }
}

/** A cell renderer for a File. */
class File2CellRenderer implements ListCellRenderer {

    private FileSystemView fileSystemView;

    private JLabel label;

    File2CellRenderer() {
        label = new JLabel();
        label.setOpaque(true);
        fileSystemView = FileSystemView.getFileSystemView();
    }

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        File file = (File)value;
        label.setIcon(fileSystemView.getSystemIcon(file));
        //label.setText(fileSystemView.getSystemDisplayName(file));
        label.setToolTipText(file.getName());

        if (isSelected) {
            label.setBackground(Color.RED);
            label.setForeground(Color.YELLOW);
        } else {
            label.setBackground(Color.WHITE);
            label.setForeground(Color.BLACK);
        }

        return label;
    }
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.border.EmptyBorder;
导入java.io.*;
导入java.util.Vector;
导入javax.swing.filechooser.FileSystemView;
公共类可执行列表{
私有JComponent ui=null;
String exePathString=“C:\\Windows”;
字符串exeFileType=“exe”;
可执行列表(){
initUI();
}
公共最终无效初始值(){
如果(ui!=null){
返回;
}
ui=新的JPanel(新的BorderLayout(4,4));
ui.setboorder(新的空订单(4,4,4,4));
File[]fAll0=新文件(exepath字符串).listFiles();
向量v=新向量();
对于(文件f0:0){
if(f0.isDirectory()){
FilenameFilter fNF=新FilenameFilter(){
@凌驾
公共布尔接受(文件目录,字符串名称){
返回name.toLowerCase().endsWith(“exe”);
}
};
文件[]fExe0=f0.listFiles(fNF);
System.out.println(“fExe0:”);
如果(fExe0!=null
) {
//&&fExe0.0长度{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}捕获(异常使用默认值){
}
ExecutableList o=新的ExecutableList();
JFrame f=新的JFrame(o.getClass().getSimpleName());
f、 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f、 setLocationByPlatform(真);
f、 setContentPane(o.getUI());
f、 包装();
维度d=f.getSize();
d=新尺寸(d.宽度,d.高度/3);
f、 设定最小尺寸(d);
f、 设置大小(d);
f、 setVisible(真);
};
SwingUtilities.invokeLater(r);
}
}
/**文件的单元格渲染器*/
类File2CellRenderer实现ListCellRenderer{
私有文件系统视图文件系统视图;
私人标签;
File2CellRenderer(){
label=新的JLabel();
label.set不透明(true);
fileSystemView=fileSystemView.getFileSystemView();
}
@凌驾
公共组件getListCellRenderComponent(JList列表、对象值、int索引、布尔isSelected、布尔cellHasFocus){
文件=(文件)值;
label.setIcon(fileSystemView.getSystemIcon(文件));
//label.setText(fileSystemView.getSystemDisplayName(文件));
label.setToolTipText(file.getName());
如果(当选){
标签.立根背景(颜色.红色);
标签。设置前景(颜色。黄色);
}否则{
标签.背景(颜色.白色);
标签。设置前景(颜色。黑色);
}
退货标签;
}
}

btn.setSize(btn.getPreferredSize());
btn.setVisible(true);
是毫无意义的,因为布局管理器会处理第一个组件,而且大多数组件默认情况下都是可见的。我强烈建议查看@andrewhompson好的,我理解并编辑了我的帖子。我将在“设置”中创建一个JList窗格。请再次查看。@MadProgrammer,但是如果删除
btn.setSize(btn.getPreferredSize());
按钮将不可见。我认为默认情况下其大小为0,0。那么您没有使用正确的布局管理器