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

        <bdo id='r6GpY'></bdo><ul id='r6GpY'></ul>
    2. <tfoot id='r6GpY'></tfoot>

        getSource() 和 getActionCommand()

        时间:2023-06-28
        <i id='ChF4Z'><tr id='ChF4Z'><dt id='ChF4Z'><q id='ChF4Z'><span id='ChF4Z'><b id='ChF4Z'><form id='ChF4Z'><ins id='ChF4Z'></ins><ul id='ChF4Z'></ul><sub id='ChF4Z'></sub></form><legend id='ChF4Z'></legend><bdo id='ChF4Z'><pre id='ChF4Z'><center id='ChF4Z'></center></pre></bdo></b><th id='ChF4Z'></th></span></q></dt></tr></i><div id='ChF4Z'><tfoot id='ChF4Z'></tfoot><dl id='ChF4Z'><fieldset id='ChF4Z'></fieldset></dl></div>

        <tfoot id='ChF4Z'></tfoot>
      1. <legend id='ChF4Z'><style id='ChF4Z'><dir id='ChF4Z'><q id='ChF4Z'></q></dir></style></legend>

                <bdo id='ChF4Z'></bdo><ul id='ChF4Z'></ul>
              • <small id='ChF4Z'></small><noframes id='ChF4Z'>

                    <tbody id='ChF4Z'></tbody>
                • 本文介绍了getSource() 和 getActionCommand()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  什么是getSource?它返回什么?

                  What is getSource? and what does it return?

                  什么是getActionCommand(),它返回什么??

                  and what is getActionCommand() and what does it return??

                  我对这两者感到困惑,任何人都可以给我或区分它们吗?UI 中的 getSource 和 getActionCommand() 有什么用?具体是 TextField 还是 JTextField?

                  I am getting confused between these two can anyone give or differentiate them to me? what's the use of getSource and getActionCommand() in UI's? specifically TextField or JTextField?

                  推荐答案

                  假设你说的是 ActionEvent 类,那么这两种方法有很大区别.

                  Assuming you are talking about the ActionEvent class, then there is a big difference between the two methods.

                  getActionCommand() 给你一个代表动作命令的字符串.该值是特定于组件的;对于 JButton 您可以选择使用 setActionCommand(String command) 设置值,但对于 JTextField 如果您不设置此值,它会自动为您提供文本字段的值.根据 javadoc,这是为了与 java.awt.TextField 兼容.

                  getActionCommand() gives you a String representing the action command. The value is component specific; for a JButton you have the option to set the value with setActionCommand(String command) but for a JTextField if you don't set this, it will automatically give you the value of the text field. According to the javadoc this is for compatability with java.awt.TextField.

                  getSource()ActionEvent 是子级的 EventObject 类指定(通过 java.awt.AWTEvent).这为您提供了对事件来自的对象的引用.

                  getSource() is specified by the EventObject class that ActionEvent is a child of (via java.awt.AWTEvent). This gives you a reference to the object that the event came from.

                  这是一个例子.有两个字段,一个具有明确设置的操作命令,另一个没有.在每个中输入一些文本,然后按 Enter.

                  Here is a example. There are two fields, one has an action command explicitly set, the other doesn't. Type some text into each then press enter.

                  public class Events implements ActionListener {
                  
                    private static JFrame frame; 
                  
                    public static void main(String[] args) {
                  
                      frame = new JFrame("JTextField events");
                      frame.getContentPane().setLayout(new FlowLayout());
                  
                      JTextField field1 = new JTextField(10);
                      field1.addActionListener(new Events());
                      frame.getContentPane().add(new JLabel("Field with no action command set"));
                      frame.getContentPane().add(field1);
                  
                      JTextField field2 = new JTextField(10);
                      field2.addActionListener(new Events());
                      field2.setActionCommand("my action command");
                      frame.getContentPane().add(new JLabel("Field with an action command set"));
                      frame.getContentPane().add(field2);
                  
                  
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.setSize(220, 150);
                      frame.setResizable(false);
                      frame.setVisible(true);
                    }
                  
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                      String cmd = evt.getActionCommand();
                      JOptionPane.showMessageDialog(frame, "Command: " + cmd);
                    }
                  
                  }
                  

                  这篇关于getSource() 和 getActionCommand()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 JavaFX 在任何地方处理鼠标事件 下一篇:从普通 Java 事件创建 Observable

                  相关文章

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

                  1. <small id='qOfpC'></small><noframes id='qOfpC'>

                      • <bdo id='qOfpC'></bdo><ul id='qOfpC'></ul>
                      <tfoot id='qOfpC'></tfoot>
                      <legend id='qOfpC'><style id='qOfpC'><dir id='qOfpC'><q id='qOfpC'></q></dir></style></legend>