在运行时隐藏和显示Panel

时间:2012-11-15 17:14:41

标签: java swing panel

我正在尝试在运行时显示和隐藏面板,所以我从另一个类调用方法:

Con.Action(1);

这个方法:

 public void Action(int whichPanel) {
    if (whichPanel == 1) {
        if (frame.Data.isVisible()) {
            frame.Data.setVisible(false);
            // frame.splitPaneSec.remove(frame.Data);
        } else {
            System.out.println(".....");

            //frame.Data.setVisible(false);
            frame.Data.setVisible(true);
            //frame.getContentPane().validate();
                            //frame.revalidate();
                            //frame.repaint();
            //frame.pack();
        }
    }

到目前为止,我可以隐藏一个面板,但是一旦隐藏它就无法再显示它。 我已经尝试了很多方法,其中一些被注释掉了 任何帮助表示赞赏,欢呼声

4 个答案:

答案 0 :(得分:0)

我想我宁愿删除并添加回来。

parent.remove(dataPanel);
parent.validate();
parent.repaint();

parent.add(dataPanel);
parent.validate();
parent.repaint();

答案 1 :(得分:0)

我强烈建议您使用以下任务的枚举:

public void Action(int whichPanel) {
    if (whichPanel == 1) {} 
//Better;
public void Action(enumType myPanel){
    if(myPanel == enumType.LoginScreen){}

与`

public enum enumType
{
    LoginScreen,EditScreen //...
}`

如果您直接在要显示的面板上执行setVisible方法,则可以隐藏和显示枚举:

public void showLoginScreen()
{
    loginPanel.setVisible(true);
    registerMenu.setVisible(false);
}

答案 2 :(得分:0)

问题是因为将组件设置回visible后 需要GUIClass.splitPane.setDividerLocation(157);

答案 3 :(得分:0)

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

public class HidePanel extends JFrame {

    private static final int PANEL_HEIGHT = 50;
    private static final int INITIAL_PANEL_WIDTH = 50;
    private static final int TIMER_INTERVAL = 1;
    private static final int PIXEL_DELTA = 2;
    private Dimension panelDimension = new Dimension(INITIAL_PANEL_WIDTH, PANEL_HEIGHT);
    private int vertOffset = -PANEL_HEIGHT;
    private Timer showTimer = new HidePanel.ShowPanelTimer();
    private Timer hideTimer = new HidePanel.HidePanelTimer();

    static JLayeredPane lpane = new JLayeredPane();
    JPanel panel = new JPanel();


    public HidePanel() {
        setBackground(Color.BLACK);

        setLayout(null);

        panel.setBounds(1200, 50, 100, 600);
        panel.setBorder(new TitledBorder(new EtchedBorder(), "Hide"));
        panel.add(new JButton("Click Me"));
        lpane.add(panel);//, //new Integer(0), 0);

        add(panel);

        addMouseMotionListener(new MouseMotionListener() {
            public void mouseDragged(MouseEvent e) {
            }

            public void mouseMoved(MouseEvent e) {
                final int height = e.getY() - 100 ;

                if (height < 50 && !showTimer.isRunning() && !panel.isVisible()) {
                    showTimer.start();
                } else if (height > PANEL_HEIGHT + 5 && !hideTimer.isRunning() && panel.isVisible()) {
                    hideTimer.start();
                }
            }
        });
    }

    public static void main(String args[]) {

        HidePanel h = new HidePanel();
        h.setSize(1350, 700);
        h.setVisible(true);
        h.add(lpane, BorderLayout.CENTER);
    }

    private void positionPanel(final int offset) {
        int panelWidth = getWidth();

        System.out.println(" offset = " + offset);

        panelDimension.setSize(panelWidth, PANEL_HEIGHT);

        //panel.setBounds(insets.left, offset + insets.top, size.width, size.height);
    }

    private class ShowPanelTimer extends Timer implements ActionListener {

        ShowPanelTimer() {
            super(TIMER_INTERVAL, null); 
            addActionListener(this);
        }

        public void start() {
            vertOffset = -PANEL_HEIGHT;
            panel.setVisible(true);
            super.start();
        }

        public void actionPerformed(ActionEvent e) {

            if (vertOffset <= 0) {
                positionPanel(vertOffset);
            } else {
                showTimer.stop();
            }

            vertOffset += PIXEL_DELTA;
        }
    }

    private class HidePanelTimer extends Timer implements ActionListener {

        HidePanelTimer() {
            super(TIMER_INTERVAL, null); 
            addActionListener(this);
        }

        public void stop() {
            panel.setVisible(false);
            super.stop(); 
        }

        public void actionPerformed(ActionEvent e) {

            if (vertOffset >= (-PANEL_HEIGHT)) {
                vertOffset -= PIXEL_DELTA;
                positionPanel(vertOffset);
            } else {
                hideTimer.stop();
            }
        }
    }
}