使用Netbeans IDE的设计器动态设计JFrame表单

时间:2017-07-01 10:24:00

标签: java swing netbeans

我想添加按钮,但他们的号码不确定。在他们添加时,他们的位置必须动态设置。我正在使用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

框架看起来像:

The frame is looking like:

gui中的按钮看起来像在android启动器中。他们的位置将按行和列排序。但由于按钮数量不确定,我必须自动执行此操作。

1 个答案:

答案 0 :(得分:1)

  

gui中的按钮看起来像在android启动器中。

它看起来像这样..

enter image description here

但是,当然,台式电脑不理解&#39;刷卡&#39;到下一个屏幕,所以我们有键盘&amp;鼠标更改视图并滚动窗格以获取长列表。

以下是创建上述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;
    }
}