我想创建一个新的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;
}
答案 0 :(得分:3)
getComponents()
获取Container保留的组件来轻松找到该组件。这将返回一个组件数组。然后删除数组中的最后一项,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();
}
});
}
}