单击“确定”按钮后关闭窗口并继续进入下一个窗口

时间:2014-11-27 18:24:55

标签: jframe jbutton actionlistener

我现在没有任何代码,但是任何人都可以给我一个教程链接或任何能够让我了解如何在单击“确定”按钮并移动到下一个窗口后关闭窗口的想法?

我已经搜索了标签,但似乎我找不到答案。

感谢您将来的帮助。

-Christian

2 个答案:

答案 0 :(得分:0)

只需向调用frame.setVisible(false)的按钮添加操作侦听器,然后打开一个新窗口(例如newFrame.setVisible(true))。

JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    frame.setVisible(false);
    newFrame.setVisible(true);
  }
});

答案 1 :(得分:0)

是的,最好的方法可能是使用" Anonymous内部课程"方式,正如java设计师所说的那样, 它是这样的:

/// inside the constructor or method where the JButton should be declared 
//and defined:

JButton button1 = new JButton("OK") ;// creating a JButton object
                                 // with the label OK .
// Now Adding an ActionListener to the Button:

   button1.addActionListener(new ActionListener(){ // Here starts the anonymous class.. 

   @Override  // This annotation denotes that it overrides
           // an abstract method from the ActionListener abstract class

  public void actionPerformed(actionEvent  e){

        // Now this is where you want to place
        // all command and actions to be performed when the
        // button is clicked

         Name_Of_Frame.setVisible(false);// This makes the frame not visible anymore.
         Name_Of_Frame.dispose() ;     // And this just throws the frame out
                                       // of the stack, and make it available for the
                                       // Garbage Collector.

  } // End of "public void actionPerformed(actionEvent  e){....." method


  }// End of anonymous class.


  ); // End of method call and command " button1.addActionListener(new ActionListener() {

我希望你能管理这个样本, 您还可以在以下位置查看有关此主题的精彩视频教程: http://www.newthinktank.com/2012/03/java-video-tutorial-21/