如何制作一个将在JPanel中创建其他按钮的JButton?

时间:2017-12-23 13:36:05

标签: java swing jbutton

我正在创建一个用户系统来保存多个用户的多个详细信息。所以我想创建一个能够创建另一个按钮的按钮。当按下第二个按钮时,将打开一个表单供用户填写。我已经创建了用户填写的表单,但我无法设置按钮来创建更多按钮来工作。我编写了这个,但它没有显示Jpanel上的按钮。

我创建了以下代码:

private void mainButtonActionPerformed(java.awt.event.ActionEvent evt) {    
        JButton b=new JButton("Click Here"); 
        b.setBounds(50,100,95,30);  
        jPanel3.add(b); 
        b.setVisible(true); 
}

这似乎不会在jPanel3中创建新按钮。我是否错误地输入了代码,或者有其他正确的方法吗?

我想要连续3个按钮,然后是新的按钮行。

1 个答案:

答案 0 :(得分:1)

您的代码和问题缺少太多信息,无法完全或妥善回答。关于我能说的全部是

  1. 在添加或删除组件后,始终在容器上调用jPanel3.revalidate()jPanel3.repaint(),因为这会告诉容器(此处为jPanel3)布局管理器重新布局所有组件然后重新布局 - 拉他们。
  2. 容器的布局管理器是使其工作良好的关键 - 我们目前不知道它是什么,一些布局管理器将允许您轻松地执行此操作(例如,FlowLayout,GridLayout)其他人不会(例如,GroupLayout)。
  3. 由于您新创建的JComponent(此处为JButton)默认情况下已经可见,因此不需要b.setVisible(true);
  4. 您似乎假设它使用空布局,因为您正在调用setBounds(...),这是 Bad Idea™。虽然null布局和setBounds()似乎是Swing新手,比如创建复杂GUI的最简单和最好的方法,但是你创建的Swing GUI越多,你在使用它们时会遇到更严重的困难。 。当GUI调整大小时,他们不会调整组件的大小,他们是增强或维护的皇室女巫,当他们放置在滚动窗格中时,他们完全失败,当他们在所有平台或屏幕分辨率不同时看起来很糟糕原来的。
  5. 在提出此类问题时,请尝试创建并发布一个小而完整的程序,我们可以对其进行测试和运行,并说明您的问题,minimal example program(请点击链接)。
  6. 例如,我的 MCVE显示了代码的工作方式:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class AddButton extends JPanel {
        private JPanel jPanel3 = new JPanel(); // panel to hold buttons
    
        public AddButton() {
            // create JButton that will add new buttons to jPanel3
            JButton addMoreButtonsBtn = new JButton("Add More Buttons");
            // give it an ActionListener
            addMoreButtonsBtn.addActionListener(e -> {
                final JButton newButton = new JButton("Click Here");
                // when you click it, it removes itself (just for grins)
                newButton.addActionListener(e2 -> {
                    jPanel3.remove(newButton);
                    // again revalidate and repaint
                    jPanel3.revalidate();
                    jPanel3.repaint();            
                });
                // add to jPanel3, the "container"
                jPanel3.add(newButton);
                // revalidate and repaint the container
                jPanel3.revalidate();
                jPanel3.repaint();            
            });
    
            // create a JPanel and put the add more buttons button to it
            JPanel bottomPanel = new JPanel();
            bottomPanel.add(addMoreButtonsBtn);
    
            // give jPanel3 a layout that can handle new buttons
            // a gridlayout with 1 column and any number of rows
            jPanel3.setLayout(new GridLayout(0, 1));
    
            // add it to the top of another JPanel that uses BorderLayout
            JPanel borderLayoutPanel = new JPanel(new BorderLayout());
            borderLayoutPanel.add(jPanel3, BorderLayout.PAGE_START);
            // and add that to a JScrollPane, so we can add many buttons and scroll
            JScrollPane scrollPane = new JScrollPane(borderLayoutPanel);
            // make the vert scrollbar always visible
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    
            // force GUI to be larger
            setPreferredSize(new Dimension(400, 200));
            // give the main JPanel a BorderLayout
            setLayout(new BorderLayout());
            // and add scrollpane to center
            add(scrollPane, BorderLayout.CENTER);
            // add bottom panel to the bottom
            add(bottomPanel, BorderLayout.PAGE_END);
        }
    
        private static void createAndShowGui() {
            AddButton mainPanel = new AddButton();
    
            JFrame frame = new JFrame("AddButton");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(mainPanel);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> createAndShowGui());
        }
    }