【问题标题】:How can i move one frame to another in MouseListener如何在 MouseListener 中将一帧移动到另一帧
【发布时间】:2013-08-14 15:13:11
【问题描述】:

我想dispose()我当前的jFrame并移动到下一个jFrame(StudentProfilepage())。但它在this.dispose()显示错误。

我该怎么做。这里我用MouseListnerjLabel l1

我的代码如下

 l1.setCursor(Cursor.getDefaultCursor());

        l1.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseReleased(MouseEvent e) {

            //added check for MouseEvent.BUTTON1 which is left click
            if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {

                this.dispose();   //error here(i want to close my current frame and move to StudentProfile() page 

                new StudentProfilePage().setVisible(true);
            }
        }
    });

【问题讨论】:

  • 你的班级名称是什么?
  • 请查看CardLayout,这个套件更适合这种情况。对于example :-)

标签: java swing jframe multiple-instances mouselistener


【解决方案1】:

this

this.dispose();

指的是MouseAdapter,因此您会看到编译错误。

您需要在 JFrame 上调用 dispose

JFrameClassName.this.dispose();

还可以考虑使用JDialog 而不是JFrame 作为第二个窗口。阅读The Use of Multiple JFrames, Good/Bad Practice?

【讨论】:

    【解决方案2】:

    你应该写

    YourClassName.this.dispose();
    

    指向你的jframe

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多