简体   繁体   English

如何从扩展JDialog的另一个类中调用已存在的活动框架,然后删除其所有组件?

[英]how to call active frame that already exist from another class that extends JDialog then remove all of its component?

I have class main extends jframe , it has a button that calls /shows another class that extends jdialog . 我有一个class main extends jframe ,它有一个按钮,它调用/显示另一个扩展jdialog类。

If the button from jdialog is triggered, it will dispose that dialog and will remove all component of jframe , then add it to a new jpanel . 如果jdialog的按钮被触发,它将处理该对话框并删除jframe所有组件,然后将其添加到新的jpanel

What should I do? 我该怎么办?

Here's my new broken code: 这是我新的破损代码:

public class mainz extends JFrame{
mainz(){
    setVisible(true);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    JToolBar r = new JToolBar();
    r.add(Box.createHorizontalGlue());
    add(r, BorderLayout.NORTH);

    JButton n = new JButton();
    r.add(n, BorderLayout.EAST);

    n.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae){
            show();
        }
    });
}

public void  show(){
    dialogz d = new dialogz(this);
    d.setVisible(true);
}

public void lastHope(){
    getContentPane().removeAll();
    getContentPane().validate();
    getContentPane().repaint();
}

public static void main (String[]args){
    new mainz().setExtendedState(MAXIMIZED_BOTH);
}

} }

public class dialogz extends JDialog{
public dialogz(final mainz owner) {
    setSize(300, 300);
    JButton n = new JButton("execute");
    add(n);

    final JFrame ew = (JFrame)super.getOwner();// <<

    n.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae){
            dispose();
            //owner.lastHope;
            ew.removeAll();// <<
            ew.validate();// <<
            ew.repaint();// <<
        }
    });
}

void yes(){
    getOwner().removeAll();

    getOwner().validate();
    getOwner().repaint();
}

} }

I know I can prevent my main class from extending jframe , and call it from main instead, but I want to do it like that... 我知道我可以阻止我的main类扩展jframe ,而改为从main调用它,但是我想那样做...

Please help me ... TT 请帮助我... TT

Sorry for my English, I from a far away country ~,~" 抱歉,我是来自遥远国家的英语,~~~”

update: the error is 更新:错误是

java.lang.ClassCastException: javax.swing.SwingUtilities$SharedOwnerFrame cannot be cast to javax.swing.JFrame java.lang.ClassCastException:javax.swing.SwingUtilities $ SharedOwnerFrame无法转换为javax.swing.JFrame

it will be done with delete the line that contain // << then call lastHope(); 删除包含// <<的行,然后调用lastHope()即可。

but i think there's a another way to get that existing jframe to removeall (by casting it first or something ~,~" ) 但是我认为还有另一种方法可以使现有的jframe删除全部(通过先转换它或类似的东西〜,〜“)

I'm not clear on what your goal is, but if you want to change the components that are displayed in a container, such as a JFrame or JDialog's contentPane, then I recommend that you use a CardLayout to do this since it allows you to easily swap "views". 我不清楚您的目标是什么,但是如果您想更改容器中显示的组件(例如JFrame或JDialog的contentPane),那么我建议您使用CardLayout来执行此操作,因为它允许您轻松交换“视图”。

There could be two ways to do this: 可能有两种方法可以做到这一点:

  • Your JDialog class could use a reference to the JFrame that is passed in via its constructor (and you should then pass it immediately into the dialog's super constructor so that your modality will work correctly). 您的JDialog类可以使用对JFrame的引用,该引用是通过其构造函数传递的(然后应立即将其传递到对话框的super构造函数中,以使您的模态正确运行)。 You could then call any public methods in the JFrame's class. 然后,您可以在JFrame的类中调用任何公共方法。
  • Or since the JDialog is modal, the JFrame's code will halt while the dialog is visible. 或者,由于JDialog是模态的,因此对话框可见时JFrame的代码将停止。 You could swap "views" immediately after the dialog has been disposed of and is no longer visible. 您可以在处理完对话框且不再可见后立即交换“视图”。 this would keep the JFrame manipulating code in the JFrame class. 这会将JFrame操作代码保留在JFrame类中。
  • Edit: note that if you don't use CardLayout, then you're responsible for calling revalidate() and repaint() on any container who gets its components changed. 编辑:请注意,如果您不使用CardLayout,则您有责任在更改其组件的任何容器上调用revalidate()repaint()

As an aside: since English is not your first tongue and nor is it the native language of many folks on this forum, please avoid using non-standard abbreviations. 顺便说一句:由于英语不是您的母语,也不是该论坛上许多人的母语,因此请避免使用非标准缩写。 The clearer your communication with us, the easier it will be for us to understand you and help you. 您与我们的交流越清晰,我们就越容易理解您并为您提供帮助。

You are calling getParent() but you never set the parent (or owner). 您正在调用getParent()但从未设置父级(或所有者)。 That should happen in the constructor as already pointed out. 如前所述,这应该在构造函数中发生。 Also, be mindful that getParent() returns a Container object and getOwner() returns a Window object. 另外,请注意, getParent()返回一个Container对象,而getOwner()返回一个Window对象。 Both of these refer to the JFrame which is the parent and owner. 这两个都引用了作为父级和所有者的JFrame。 If you want to use it as a JFrame, you'll have to cast the output as (JFrame) . 如果要将其用作JFrame,则必须将输出(JFrame)(JFrame) But removeAll() is in Container class so if that's all you want, there'll be no need for casting. 但是removeAll()Container类中,因此如果您只想这样做,则无需进行强制转换。

Update : 更新

JFrame frame = new JFrame();
JDialog dialog = new JDialog(frame);//frame is owner

JFrame parentOfDialog = (JFrame)(dialog.getParent());
//OR
//JFrame parentOfDialog = (JFrame)(dialog.getOwner());
parentOfDialog.removeAll();

If you are using your custom class, pass JFrame in the constructor and call super . 如果您使用的是自定义类,请在构造函数中传递JFrame并调用super

Please read the javadoc on JDialog before you try to use it. 在尝试使用它之前,请阅读JDialog上的javadoc。 Also, read more about inheritance. 另外,请阅读有关继承的更多信息。

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

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