<bdo id='yOwPs'></bdo><ul id='yOwPs'></ul>

    <small id='yOwPs'></small><noframes id='yOwPs'>

    1. <tfoot id='yOwPs'></tfoot>

    2. <i id='yOwPs'><tr id='yOwPs'><dt id='yOwPs'><q id='yOwPs'><span id='yOwPs'><b id='yOwPs'><form id='yOwPs'><ins id='yOwPs'></ins><ul id='yOwPs'></ul><sub id='yOwPs'></sub></form><legend id='yOwPs'></legend><bdo id='yOwPs'><pre id='yOwPs'><center id='yOwPs'></center></pre></bdo></b><th id='yOwPs'></th></span></q></dt></tr></i><div id='yOwPs'><tfoot id='yOwPs'></tfoot><dl id='yOwPs'><fieldset id='yOwPs'></fieldset></dl></div>
        <legend id='yOwPs'><style id='yOwPs'><dir id='yOwPs'><q id='yOwPs'></q></dir></style></legend>

        java中的事件处理和java中actionPerformed方法的执行

        时间:2023-09-30

          <small id='Yf1A1'></small><noframes id='Yf1A1'>

              <bdo id='Yf1A1'></bdo><ul id='Yf1A1'></ul>
              <tfoot id='Yf1A1'></tfoot>
            • <i id='Yf1A1'><tr id='Yf1A1'><dt id='Yf1A1'><q id='Yf1A1'><span id='Yf1A1'><b id='Yf1A1'><form id='Yf1A1'><ins id='Yf1A1'></ins><ul id='Yf1A1'></ul><sub id='Yf1A1'></sub></form><legend id='Yf1A1'></legend><bdo id='Yf1A1'><pre id='Yf1A1'><center id='Yf1A1'></center></pre></bdo></b><th id='Yf1A1'></th></span></q></dt></tr></i><div id='Yf1A1'><tfoot id='Yf1A1'></tfoot><dl id='Yf1A1'><fieldset id='Yf1A1'></fieldset></dl></div>
                <tbody id='Yf1A1'></tbody>

                • <legend id='Yf1A1'><style id='Yf1A1'><dir id='Yf1A1'><q id='Yf1A1'></q></dir></style></legend>
                • 本文介绍了java中的事件处理和java中actionPerformed方法的执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我用java写了一个simpleGUI的小代码.

                  I have written a small code in java for simpleGUI.

                  package guidemo1;
                  
                  import java.awt.event.ActionEvent;
                  import java.awt.event.ActionListener;
                  import javax.swing.JButton;
                  import javax.swing.JFrame;
                  
                  public class GuiDemo1 implements ActionListener{
                   JButton button;
                      /**
                       * @param args the command line arguments
                       */
                      public static void main(String[] args) {
                          GuiDemo1 gui=new GuiDemo1();
                          gui.go();
                      }
                  
                      public void go()
                      {
                          JFrame frame=new JFrame();
                          button=new JButton();
                          frame.getContentPane().add(button);
                          button.addActionListener(this);
                          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          frame.setSize(300, 200);
                          frame.setVisible(true);
                  
                      }
                      @Override
                      public void actionPerformed(ActionEvent e) {
                          //throw new UnsupportedOperationException("Not supported yet.");
                          button.setText("I've been clicked");
                      }
                  }
                  

                  我是 JAVA 的新手.我有几个关于这个程序的问题.

                  I am newbie to JAVA.I have few questions related to this program.

                  有人能解释一下 actionPerformed 方法是如何在没有任何调用的情况下执行的吗?

                  Can some one explain how actionPerformed method gets executed with out any call?

                  在这里,我在 go() 方法中定义了本地框架对象,我们在 actionPerformed 中使用按钮,这是另一种方法.这怎么可能?按钮不是嵌入在框架上吗?

                  Here I have defined frame object locally to the go() method and we are using button in actionPerformed which is another method.How is that possible?Isn't the button gets embedded on the frame?

                  谢谢..

                  推荐答案

                  有人能解释一下 actionPerformed 方法是如何在没有任何调用的情况下执行的吗?

                  Can some one explain how actionPerformed method gets executed with out any call?

                  GUI 框架 Swing 在后台运行动作处理代码.每当按下按钮或用户以其他方式与 GUI 交互时,Swing 都会通过许多 Listener 接口之一通知您的应用程序.为了接收这些事件,您的类需要实现正确的 Listener 接口,并在它感兴趣的每个组件上注册为侦听器.

                  The GUI framework Swing runs action handling code in the background. Whenever a button is pressed or the user interacts with the GUI in some other way, Swing will notify your application through one of many Listener interfaces. In order to receive these events, your class needs to implement the proper Listener interface and be registered as a listener on each component it is interested in.

                  您的类实现了 ActionListener 接口并调用 addActionListener 将自身注册到按钮.当单击按钮时,Swing 将尝试通过调用它们的 actionPerformed 方法来通知所有已注册的 ActionListener.魔法"就是这样发生的.

                  Your class implements the ActionListener interface and calls addActionListener to register itself to the button. When a button is clicked, Swing will attempt to notify all registered ActionListeners by calling their actionPerformed method. That's how the "magic" happens.

                  在这里,我在 go() 方法中定义了本地框架对象,我们在 actionPerformed 中使用按钮,这是另一种方法.这怎么可能?按钮不是嵌入在框架上吗?

                  Here I have defined frame object locally to the go() method and we are using button in actionPerformed which is another method.How is that possible?Isn't the button gets embedded on the frame?

                  您将按钮 添加 到框架的内容窗格中 - 这会将按钮放在框架内的 布局 中,而不是放在 代码 中.因为您通过将 JButton button; 放在任何方法之外来将 button 声明为实例变量,所以仍然可以从任何(非 static)方法访问它在那个班级.

                  You add the button to the frame's content pane - this puts the button inside the frame in your layout, not in your code. Because you declare button as an instance variable by putting JButton button; outside any method, it is still accessible from any (non-static) method in that class.

                  这篇关于java中的事件处理和java中actionPerformed方法的执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Swing 中停止事件的默认行为 下一篇:在类的构造函数中找出实例化对象

                  相关文章

                  最新文章

                      <bdo id='Oh8RI'></bdo><ul id='Oh8RI'></ul>
                  1. <small id='Oh8RI'></small><noframes id='Oh8RI'>

                  2. <tfoot id='Oh8RI'></tfoot>

                  3. <legend id='Oh8RI'><style id='Oh8RI'><dir id='Oh8RI'><q id='Oh8RI'></q></dir></style></legend>

                    1. <i id='Oh8RI'><tr id='Oh8RI'><dt id='Oh8RI'><q id='Oh8RI'><span id='Oh8RI'><b id='Oh8RI'><form id='Oh8RI'><ins id='Oh8RI'></ins><ul id='Oh8RI'></ul><sub id='Oh8RI'></sub></form><legend id='Oh8RI'></legend><bdo id='Oh8RI'><pre id='Oh8RI'><center id='Oh8RI'></center></pre></bdo></b><th id='Oh8RI'></th></span></q></dt></tr></i><div id='Oh8RI'><tfoot id='Oh8RI'></tfoot><dl id='Oh8RI'><fieldset id='Oh8RI'></fieldset></dl></div>