2

我想在每次单击按钮时创建一个新的 JPanel,并在面板上创建对象(按钮和/或标签)。该部分似乎在技术上有效,但是在尝试一次删除面板(从最后添加到第一个)时遇到问题。

尽管我留在了一个未使用的数组中,但我已尝试尽可能地精简代码,因为我认为这就是我想使这项工作发挥作用的方式。任何建议将不胜感激。如果有什么太含糊,也让我知道,试图用语言表达我的问题比我想象的要难。

免责声明:抱歉,至少有一些最佳实践被忽略了。

public class CreatePanelsTest{

    JPanel totalGUI;
    GridLayout grid = new GridLayout(0,1);
    int panelX = 10;

     public JPanel createContentPane (){

        //create a bottom JPanel to place everything on.
        totalGUI = new JPanel();
        //set the Layout Manager to null, manually place objects
        totalGUI.setLayout(null);    
        JPanel controlPanel = new JPanel();
        controlPanel.setLocation(50, 220);
        controlPanel.setSize(200, 40);
        JButton addSet = new JButton("add set");
             addSet.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newSetActionPerformed(evt);
            }
        });
         JButton removeSet = new JButton("remove set");
             removeSet.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeSetActionPerformed(evt);
            }
        });     

        controlPanel.add(addSet);
        controlPanel.add(removeSet);
        totalGUI.add(controlPanel);
        totalGUI.revalidate();
        return totalGUI;

    }

private void newSetActionPerformed(java.awt.event.ActionEvent evt) {
        //on button click adds group
        JPanel newPanel = new JPanel(grid);
        newPanel.setLocation(panelX, 10);
        newPanel.setSize(50,200);
        JButton button1 = new JButton("1");
        JButton button2 = new JButton("2");
        newPanel.add(button1);
        newPanel.add(button2);
        totalGUI.add(newPanel);
        totalGUI.validate();
        totalGUI.repaint();
        panelX = panelX+50;
    }

private void removeSetActionPerformed(java.awt.event.ActionEvent evt) {
        //this is suppose to remove one panel at a time
        //totalGUI.remove(newPanel);
        //totalGUI.validate();
        //totalGUI.repaint();
        //panelX = panelX-50;
    }


 public JPanel[] autoArray(){
        //was hoping to make this method work for me, unused atm
        int n = 6;
        //can i change the value of "n" later or will it break stuff?
        JPanel[] panels = new JPanel[n];
        for (int i = 0; i<n; i++){
            panels[i] = new JPanel(grid);

        }
        return panels;

    }
4

1 回答 1

3
  1. 不要使用空布局。它会咬你的尾巴。
  2. 如果您的目标是删除最后添加的组件,您可以通过调用getComponents(). 这将返回一个组件数组。然后删除数组中的最后一项revalidate()repaint()您的容器。

编辑
例如:

import java.awt.*;
import java.awt.event.ActionEvent;   
import javax.swing.*;

@SuppressWarnings("serial")
public class CreatePanelsTest2 {
   protected static final int PREF_W = 600;
   protected static final int PREF_H = 450;
   JPanel mainPanel = new JPanel() {
      @Override
      public Dimension getPreferredSize() {
         return new Dimension(PREF_W, PREF_H);
      }
   };
   JPanel panelHolderPanel = new JPanel(new GridLayout(1, 0));

   public CreatePanelsTest2() {
      JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      btnPanel.add(new JButton(new AddAction()));
      btnPanel.add(new JButton(new RemoveAction()));

      JPanel borderLayoutPanel = new JPanel(new BorderLayout());
      borderLayoutPanel.add(panelHolderPanel, BorderLayout.WEST);
      JScrollPane scrollPane = new JScrollPane(borderLayoutPanel);
      scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

      mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
      mainPanel.setLayout(new BorderLayout());
      mainPanel.add(scrollPane, BorderLayout.CENTER);
      mainPanel.add(btnPanel, BorderLayout.SOUTH);
   }

   public JComponent getMainComponent() {
      return mainPanel;
   }

   private class AddAction extends AbstractAction {
      int counter = 0;
      public AddAction() {
         super("Add");
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         counter++;
         JPanel innerPanel = new JPanel(new GridLayout(0, 1));
         innerPanel.add(new JButton("Foo " + counter));
         innerPanel.add(new JButton("Bar " + counter));

         panelHolderPanel.add(innerPanel);
         mainPanel.revalidate();
         mainPanel.repaint();
      }
   }

   private class RemoveAction extends AbstractAction {
      public RemoveAction() {
         super("Remove");
      }

      @Override
      public void actionPerformed(ActionEvent arg0) {
         Component[] comps = panelHolderPanel.getComponents();
         if (comps.length > 0) {
            panelHolderPanel.remove(comps[comps.length - 1]);
            mainPanel.revalidate();
            mainPanel.repaint();
         }
      }
   }

   private static void createAndShowGui() {
      CreatePanelsTest2 test2 = new CreatePanelsTest2();
      JFrame frame = new JFrame("CreatePanelsTest2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(test2.getMainComponent());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
于 2013-08-28T21:03:13.357 回答