简体   繁体   English

如何关闭框架却又打开新框架? (重访)

[英]How do I close a frame yet open a new frame? (revisited)

I'm trying to close a frame yet open a new frame. 我正在尝试关闭一个框架,但打开一个新框架。

My application has page A, a JPanel with some controls and a specific button, and when the user clicks the button, I want page A to disappear and page B to appear (page B has controls that depend on the choices that are made by the user on page A). 我的应用程序具有页面A,带有某些控件和特定按钮的JPanel ,并且当用户单击该按钮时,我希望页面A消失而页面B出现(页面B的控件取决于控件的选择)。 A页上的用户)。

This has been asked before , but there was no satisfactory answer. 之前曾有人问过这个问题 ,但没有令人满意的答案。 Inside the ActionListener implementation, namely public void ActionPerformed(ActionEvent e) from my jpanelForPageA class, I can comfortably write this.setVisible(false) , but how can I set page B to a visible state? 在ActionListener实现中,即jpanelForPageA类中的public void ActionPerformed(ActionEvent e) ,我可以轻松编写this.setVisible(false) ,但是如何将页面B设置为可见状态?

You can do the removal of panel a and then the addition of panel b trick. 您可以删除面板a,然后添加面板b技巧。 Another is to use a CardLayout . 另一种是使用CardLayout

When you create your panels, you add them to a containing JPanel that you initialize with a CardLayout : 创建面板时,将它们添加到包含JPanel ,并使用CardLayout初始化:

JPanel container = new JPanel(new CardLayout());
containter.add(getPanelA(), "PANEL_A"); 
containter.add(getPanelB(), "PANEL_B"); 

Then, in your actionPerformed , when you want to show panelB , you do this: 然后,在actionPerformed ,当要显示panelB ,请执行以下操作:

CardLayout cl = (CardLayout) container.getLayout();
cl.show("PANEL_B");

Take a look at this tutorial for some more ideas. 请看一下本教程,以获得更多想法。

For some reason, I can never to get setVisible() to work for me to do what you're describing. 出于某种原因,我永远无法让setVisible()来帮助我完成您正在描述的事情。 Instead, I do this: 相反,我这样做:

frame.remove(panelA);
frame.add(panelB);

"frame" is just the JFrame you want to put the panels in. Try this if the setVisible() method doesn't work :) “ frame”只是您要放入面板的JFrame。如果setVisible()方法不起作用,请尝试以下方法:)

To your original question, all you have to do is (like aioobe said): 对于您的原始问题,您要做的就是(就像aioobe所说的那样):

panelB.setVisible(true);

((btw, posting some of your code would help me figure out what you're trying to ask)) ((顺便说一句,发布您的一些代码将帮助我弄清楚您要问的问题))

And this is just a guess as to what you're trying to do -- I'm guessing your JPanels are in different classes. 这只是对您要执行的操作的猜测-我猜您的JPanels位于不同的类中。 Then, you'll need to do this: 然后,您需要执行以下操作:

class pages extends JFrame implements ActionListener
{
    public pages()
    {
        panelA a = new panelA(this)
    }

    changeToA(panelB b)
    {
        remove(panelB);
        add(new panelA(this));
    }        

    changeToB(panelA a)
    {
        remove(panelA);
        add(new panelB(this));
    }
}

class panelA extends JPanel implements ActionListener
{
    pages p;
    public panelA(pages p)
    {
        this.p = p
    }
    // all that actionlistener code stuff
        p.changeToB(this);
}

class panelB extends JPanel implements ActionListener
{
    pages p;
    public panelB(pages p)
    {
        this.p = p
    }
    // all that actionlistener code stuff
        p.changeToA(this);
}

You pass the pages class to the panels so the panels can tell the pages class to remove themselves. 您将页面类传递给面板,以便面板可以告诉页面类自行删除。 ((I don't know if there is an easier way, but this is what I do all the time)) ((我不知道是否有更简单的方法,但这就是我一直在做的事情))

I hope I helped :) 我希望我能帮到:)

You have to remove Panel A from the frame, add Panel B to the frame, and call invalidate on the frame (or containing panel). 您必须从框架中删除面板A,将面板B添加到框架,然后在框架(或包含面板)上调用invalidate。 At least in Swing, I'm not sure about AWT, there you might need repaint or revalidate instead of invalidate. 至少在Swing中,我不确定AWT,您可能需要重新绘制或重新验证而不是无效。

You could also just create a whole new JFrame and dispose the one containing panel A. 您也可以只创建一个全新的JFrame并处理包含面板A的JFrame。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM