在Swing中显示许多按钮

时间:2016-03-22 20:30:42

标签: java swing button

我正在尝试创建一个带有可变数量按钮的java swing面板(1-10000个按钮)。 但是,似乎我可以在表单中显示的按钮数量有限制,并且按钮似乎在一段时间后重复。 我已经查看了Java Swing上提供的教程。但是,它没有扩展到我期望处理的按钮数量。

这是我到目前为止编写的代码。

public JPanel createBlockGroup() {
    JPanel blockPanel = new JPanel();
    Dimension buttonDimension = new Dimension(40,70);
    GroupBlockJNI group = new GroupBlockJNI();
    System.out.println(group.getTotalBlockGroups());
    blockPanel.setMaximumSize(new Dimension(group.getTotalBlockGroups()*50, 100));
    blockPanel.setSize(group.getTotalBlockGroups()*50, 100);

    for(int i=0; i<group.getTotalBlockGroups(); i++) {    // getTotalBlockGroups() returns 6400
        final int j=i;
        JButton partition = new JButton("Block Group");
        partition.setPreferredSize(buttonDimension);
        partition.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 2) {
                    updateLevel(gCurrentLevel+1);
                    gButtonZoomOut.setEnabled(true);
                } else if(e.getClickCount() == 1) {
                    updateText("Block Group "+String.valueOf(j));
                }
            }
        });
        blockPanel.add(partition);
    }

    return blockPanel;
}

运行此代码时,我只能显示574个按钮。 This is the snapshot.

感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

  

我正在尝试创建一个带有可变数量按钮的java swing面板(1-10000个按钮)。但是,似乎我可以在表单中显示的按钮数量有限制,按钮似乎会在一段时间后重复。

这是我编写的一个示例,用于测试它是否可以处理10,000个按钮。一切都在我这边工作得很好。即使对于1000,000个按钮,它仍然可以正常工作。

enter image description here

持有滚动窗格的测试面板:

class MainPanel extends JPanel{

    private JScrollPane scrollPane;
    private JPanel scrollPanel;
    private JButton btnAddPage;
    private static int idx = 0;

    public MainPanel(){
        setPreferredSize(new Dimension(400, 140));
        setLayout(new BorderLayout());
        initComponents();
    }       
    private void initComponents(){
        scrollPanel = new JPanel();
        scrollPanel.setSize(new Dimension(300, 300));       
        scrollPane = new JScrollPane(scrollPanel);  //Let all scrollPanel has scroll bars
        btnAddPage = new JButton("Add New Page");
        btnAddPage.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                //for(int x=0; x<10000; x++) //uncomment to test with 10,000 buttons
                addButton(new JButton("Page " + (idx++)));
                scrollPanel.revalidate();
            }
        });             
        add(scrollPane, BorderLayout.CENTER);
        add(btnAddPage, BorderLayout.SOUTH);                
    }

    public void addButton(JButton btn){
        scrollPanel.add(btn);
    }
}

Test Runner类运行代码:

class TestRunner{
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run(){
                JFrame frame = new JFrame("Scrollable Panel");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new MainPanel());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);                 
            }   
        });     
    }
}

我已发布完整代码,我认为这对您有用。