-1

单击按钮时,我想隐藏选定的面板。我已经完成了大部分工作,但是当面板为 false 时不会更新。

这是我尝试过的事情之一:

compsToExperiment.setVisible(mapVis);
updateUI();


private void updateUI() {
        SwingUtilities.updateComponentTreeUI(this);
    }

这是在正确的轨道上吗?还有其他方法吗?

4

3 回答 3

2

您不必做任何花哨的事情,然后调用setVisible(false);所说的JPanel。下面的例子就是一个证明:

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

public class InvisiblePanel
{
    private JPanel centerPanel;
    private JPanel footerPanel;

    private JTextField addField;
    private JTextField nameField;
    private JTextField occField;
    private JTextField phoneField;
    private JLabel nameLabel;
    private JLabel addLabel;
    private JLabel occLabel;
    private JLabel phoneLabel;

    private JPanel contentPane;
    private JButton hideButton;

    private void displayGUI() 
    {
        JFrame frame = new JFrame("Invisible Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        contentPane = new JPanel();
        contentPane.setOpaque(true);
        contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        contentPane.setBackground(Color.RED.darker().darker());
        contentPane.setLayout(new BorderLayout(5, 5));

        nameLabel = new JLabel("Guarantee Name : ");
        nameField = new JTextField();   
        addLabel = new JLabel("Address : ");
        addField = new JTextField();        
        occLabel = new JLabel("Occupation : ");
        occField = new JTextField();
        phoneLabel = new JLabel("Phone : ");
        phoneField = new JTextField();
        centerPanel = new JPanel();
        hideButton = new JButton("Hide");
        hideButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                if (centerPanel.isShowing())
                    centerPanel.setVisible(false);
                else
                    centerPanel.setVisible(true);
            }
        });

        centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        centerPanel.setOpaque(true);
        centerPanel.setBackground(Color.WHITE);
        centerPanel.setLayout(new GridLayout(0, 2, 5, 5));
        centerPanel.add(nameLabel);
        centerPanel.add(nameField);
        centerPanel.add(addLabel);
        centerPanel.add(addField);
        centerPanel.add(occLabel);
        centerPanel.add(occField);
        centerPanel.add(phoneLabel);
        centerPanel.add(phoneField);

        footerPanel = new JPanel();
        footerPanel.add(hideButton);

        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(footerPanel, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    } 

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new InvisiblePanel().displayGUI();
            }
        });
    }
}
于 2012-11-14T15:05:40.833 回答
2

updateComponentTreeUI()不强制重绘。您必须使用该repaint方法。

compsToExperiment.setVisible(mapVis);
frame.repaint();

面板所在frame的顶部窗口在哪里。

于 2012-11-14T14:10:18.740 回答
1

您可以执行 JFrame.invalidate() 来刷新。请查看链接以获取更多详细信息。对不同问题的类似要求。实时刷新frame内容

于 2012-11-14T14:20:01.600 回答