我正在使用A类扩展JFrame并且是主要类。我也有B级扩展JPanel。只要在A帧中按下按钮,就会创建一个B类的新对象并将其添加到A帧中。现在,面板B中有一个按钮,当按下时,该对象将从框架中移除。
这里的问题是,如果我在某个时间点不断添加和删除对象,则对象被正确删除但框架未打包到小尺寸应该去。有人可以帮助我。
编辑1:所以,在将我的应用程序代码传输到下面的通用代码时,我发现因为我写了this.setResizable(false);因此,在移除物体后,有时候框架没有被正确包装。一旦我评论并测试了一些添加/删除现在它就好了。 SetResizable是让用户不允许他们调整为什么它会影响这里的功能的任何想法?
已编辑2:尝试从代码中删除多余内容。
所以,问题基本上就在下面了。
问题:
在这里,如果我们玩游戏,即连续添加和删除对象B,则会出现框架未正确打包的时间。
观察: 所以,我发现的问题是可设置的,如果它被设为假,那么只有这种情况发生,否则它工作正常。
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class A extends JFrame{
private static A frame;
JButton AddNewButton;
static JPanel thePanel;
static int buttonBYPos = 1;
public static void main(String[] args)
{
new A();
}
A()
{
this.setTitle("Testing");
this.setResizable(false);
frame = this;
/* Make a panel to add buttons to it and set gridbaglayout to it*/
thePanel = new JPanel();
thePanel.setLayout(new GridBagLayout());
/* Make a new button and add it to the thePanel */
AddNewButton = new JButton("Add New Button");
ListenForButton lbutton = new ListenForButton();
AddNewButton.addActionListener(lbutton);
addComp(thePanel,AddNewButton,0,0,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH);
/* Add thePanel to frame and also set Frame parameters*/
this.add(thePanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);;
this.pack();
this.setVisible(true);
}
private class ListenForButton implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == AddNewButton)
{
B b = new B();
addComp(thePanel,b,0,buttonBYPos,1,1,GridBagConstraints.CENTER,GridBagConstraints.BOTH);
buttonBYPos++;
thePanel.revalidate();
frame.pack();
frame.setLocationRelativeTo(null);
}
}
}
private void addComp(JPanel thePanel, JComponent comp, int xPos, int yPos, int compWidth, int compHeight, int place, int stretch){
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = xPos;
gridConstraints.gridy = yPos;
gridConstraints.gridwidth = compWidth;
gridConstraints.gridheight = compHeight;
gridConstraints.weightx = 100;
gridConstraints.weighty = 100;
gridConstraints.insets = new Insets(5,5,5,5);
gridConstraints.anchor = place;
gridConstraints.fill = stretch;
thePanel.add(comp, gridConstraints);
}
public static void removeBObject(B pointer) {
// TODO Auto-generated method stub
buttonBYPos--;
thePanel.remove(pointer);
thePanel.revalidate();
frame.pack();
frame.setLocationRelativeTo(null);
}
}
class B extends JPanel
{
JButton removeobj;
private B currentBObject;
B()
{
currentBObject = this;
removeobj = new JButton("Remove Object");
ListenForButton lbutton = new ListenForButton();
removeobj.addActionListener(lbutton);
this.add(removeobj);
this.setVisible(true);
}
private class ListenForButton implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == removeobj)
{
A.removeBObject(currentBObject);
}
}
}
}