Opening a new JFrame from a Button

余生颓废 提交于 2019-11-28 14:48:06
Reimeus

Although you have implemented the actionPerformed method as per the ActionListener interface, you class is not of that that type as you haven't implemented the interface. Once you implement that interface and register it with the JButton btnAdd,

btnAdd.addActionListener(this);

the method will be called.

A more compact alternative might be to use an anonymous interface:

btnAdd.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
      // handle button ActionEvent & display dialog...    
   }
});

Side notes:

  • Using more than one JFrame in an application creates a lot of overhead for managing updates that may need to exist between frames. The preferred approach is to use a modal JDialog if another window is required. This is discussed more here.

Use this :

btnAdd.addActionListener(this);

@Override

public void actionPerformed(ActionEvent e)
{
    MainFrame frame = new MainFrame();
    frame.setVisible(true);
}

Type this inside the button

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {   
this.dispose();
ActionListener ActList = new ActionListener();
ActList.setVisible(true);

}

see my last line

{ ActList.setVisible(true); }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!