处理GUI元素

时间:2015-02-25 12:15:01

标签: java swing user-interface garbage-collection

我在JPanel中添加了几个GUI元素。 JPanel被添加到JScrollPane。 JScrollPane被添加到JFrame(BorderLayout的CENTER部分)。

有时我需要删除JScrollPane并为其他元素提供空间。我已经为此提供了一种方法。要确保此方法处理旧JScrollPane使用的所有资源,并使它们可用于垃圾收集。请参阅下面的代码。我的clearCenter()方法是否足以完成此任务?有没有更好的方法呢?

感谢。

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

public class MyGui extends JFrame {
    private JScrollPane scroll;
    private JPanel panel;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;

    // Constructor
    public MyGui() {
        super("Playback");
        setSize(250, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BorderLayout layout = new BorderLayout();
        setLayout(layout);

        panel = new JPanel();
        GridLayout grid = new GridLayout(0, 1, 30, 30);
        panel.setLayout(grid);

        button1 = new JButton("Button1");
        button2 = new JButton("Button2");
        button3 = new JButton("Button3");
        button4 = new JButton("Button4");
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);

        scroll = new JScrollPane(panel,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(scroll, BorderLayout.CENTER);
        add(new JLabel("South", JLabel.CENTER),BorderLayout.SOUTH);
        add(new JLabel("North", JLabel.CENTER),BorderLayout.NORTH);
        add(new JLabel("East", JLabel.CENTER),BorderLayout.EAST);
        add(new JLabel("West", JLabel.CENTER),BorderLayout.WEST);
        setVisible(true);
    }

    public void clearCenter() {
        button1 = null;
        button2 = null;
        button3 = null;
        button4 = null;
        panel = null;
        remove(scroll);
        scroll =  null;
    }

}

1 个答案:

答案 0 :(得分:2)

  

有时我需要删除JScrollPane并为其他元素提供空间。

使用CardLayout中显示的this answer。我不会太担心处理滚动窗格,将其保存在内存中。下次需要时,更新滚动窗格内容,然后在布局中翻转回该卡。

重置滚动窗格的内容可以像下面这样完成。激活第一个按钮以查看由黄色面板替换的按钮面板作为滚动窗格的视图。请注意,此代码已准备好运行' MCVE(main(String[])在屏幕上显示)。请将来发布MCVE代码。

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

public class MyGui extends JFrame {

    private JScrollPane scroll;
    private JPanel panel;
    private JPanel brightPanel;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;

    // Constructor
    public MyGui() {
        super("Playback");
        setSize(250, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        BorderLayout layout = new BorderLayout();
        setLayout(layout);

        // create the panel, but don't add it yet.
        brightPanel = new JPanel();
        brightPanel.setBackground(Color.YELLOW);

        panel = new JPanel();
        GridLayout grid = new GridLayout(0, 1, 30, 30);
        panel.setLayout(grid);

        button1 = new JButton("Button1");
        button2 = new JButton("Button2");
        button3 = new JButton("Button3");
        button4 = new JButton("Button4");
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);
        panel.add(button4);

        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                changeViews();
            }
        };
        button1.addActionListener(listener);

        scroll = new JScrollPane(panel,
                JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        add(scroll, BorderLayout.CENTER);
        add(new JLabel("South", JLabel.CENTER), BorderLayout.SOUTH);
        add(new JLabel("North", JLabel.CENTER), BorderLayout.NORTH);
        add(new JLabel("East", JLabel.CENTER), BorderLayout.EAST);
        add(new JLabel("West", JLabel.CENTER), BorderLayout.WEST);
        setVisible(true);
    }

    public void changeViews() {
        scroll.setViewportView(brightPanel);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                new MyGui();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}