Java Jpanel自行移动 Extra



我有问题。现在,我正在使用3个面板,Mainpanel和其他2个面板(BTNPanel和Iconpanel)。因此,问题是当我按下"重置"按钮时,我会删除Iconpanel,然后再次添加它自行移动到右侧。也许有人可以检查我的代码问题吗?

我也不想创建另一个问题,所以我给出两个额外的问题。

我是否正确删除了jpanel?如果我删除带有内部组件的jpanel,它们也将从内存中删除?

P.S。我是初学者,所以不要判断我:)

Main.java
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Main extends JFrame {
    public static void main(String[] args) {    
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("Made by Mac4s");
                frame.setVisible(true);
                frame.setSize(310, 654);
                frame.setLocationRelativeTo(null);
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.setResizable(false);          


                MainScreen screenObj = new MainScreen();  
                screenObj.setPreferredSize(new Dimension(310, 650));
                frame.add(screenObj);
            }
        });
    }
}
MainScreen.java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

public class MainScreen extends JPanel {
    private JButton resetBtn;
    private JPanel btnPanel;
    private JPanel iconPanel;
    public MainScreen() {
        JPanel mainPanel = new JPanel(new BorderLayout());  
        this.setBackground(Color.BLACK);
        setBtnPanelAndComp();
        setIconPanelAndComp();
        add(mainPanel);
    }
    private void setBtnPanelAndComp() {
        btnPanel = new JPanel(new BorderLayout());
        btnPanel.setBackground(Color.GREEN);
        btnPanel.setPreferredSize(new Dimension(295, 30));
        setButtons();
        this.add(btnPanel, BorderLayout.NORTH);
    }
    private void setButtons() {
        resetBtn = new JButton("Reset");
        resetBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                resetIconLabel();
            }               
        });
        btnPanel.add(resetBtn, BorderLayout.WEST);
    }
    private void resetIconLabel() {
        this.remove(iconPanel);
        this.repaint();
        this.revalidate();
        setIconPanelAndComp();
    }
    private void setIconPanelAndComp() {
        iconPanel = new JPanel(new BorderLayout());
        iconPanel.setBackground(Color.YELLOW);
        iconPanel.setPreferredSize(new Dimension(295, 580)); 
        this.add(iconPanel, BorderLayout.SOUTH);
    }
}

"问题是当我按下"重置"时,我会删除Iconpanel并再次添加它自行移动到右移动。"

发生这种情况的原因是因为JPanel默认情况下具有FlowLayout。您正在尝试添加到不存在的BorderLayout位置。

this.add(iconPanel, BorderLayout.SOUTH);

FlowLayout的边缘上有默认差距,因此,当您设置框架的大小时,这些间隙不会尊重。为此,它也比pack()框架更可取,而不是setSize()

BorderLayout工作(不移动)的原因是因为不尊重优选尺寸。

如果将构造函数中的布局设置为this.setLayout(new BorderLayout());,则不会有偏移。

public MainScreen() {
    JPanel mainPanel = new JPanel(new BorderLayout());
    this.setLayout(new BorderLayout());                  <----
    setBtnPanelAndComp();
    setIconPanelAndComp();
    add(mainPanel);
}

注释

  • 您应该在添加组件后setVisible() 。这就是为什么您第一次打开框架时会跳跃的原因。您将框架设置为可见的,然后然后将其与位置一起移动,并添加组件。

最新更新