gpt4 book ai didi

java - 如何动态创建多个 Swing 对象并一次删除一个?

转载 作者:行者123 更新时间:2023-12-04 04:43:53 24 4
gpt4 key购买 nike

我想通过每次单击按钮创建一个新的 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;

}

最佳答案

  • 不要使用空布局。它会咬你的尾部。
  • 如果您的目标是删除最后添加的组件,您可以通过调用 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();
    }
    });
    }
    }

    关于java - 如何动态创建多个 Swing 对象并一次删除一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18497982/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com