Java基礎 awt Button 點擊按鈕後在控制檯輸出文字

  •     JDK :OpenJDK-11
  •      OS :CentOS 7.6.1810
  •      IDE :Eclipse 2019‑03
  • typesetting :Markdown

code

package per.jizuiku.gui;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author 給最苦
 * @date 2019/06/30
 * @blog www.cnblogs.com/jizuiku
 */
public class Demo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Frame f = new Frame();

        // 設置窗體大小
        int width = 300;
        int height = 400;
        f.setSize(width, height);

        // 建立按鈕對象,在按鈕上添加文字
        Button b = new Button("I am a button.");
        b.setSize(10, 30);

        // 點擊按鈕後
        b.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                System.out.println("點擊按鈕");
            }
        });

        // 採用了流式佈局
        f.setLayout(new FlowLayout());

        // 把按鈕添加到窗體上
        f.add(b);

        // 設置窗體可見
        f.setVisible(true);
    }

}

result

點擊按鈕
點擊按鈕
點擊按鈕

sourceCode

/**
     * Adds the specified action listener to receive action events from
     * this button. Action events occur when a user presses or releases
     * the mouse over this button.
     * If l is null, no exception is thrown and no action is performed.
     * <p>Refer to <a href="doc-files/AWTThreadIssues.html#ListenersThreads"
     * >AWT Threading Issues</a> for details on AWT's threading model.
     *
     * @param         l the action listener
     * @see           #removeActionListener
     * @see           #getActionListeners
     * @see           java.awt.event.ActionListener
     * @since         1.1
     */
    public synchronized void addActionListener(ActionListener l) {
        if (l == null) {
            return;
        }
        actionListener = AWTEventMulticaster.add(actionListener, l);
        newEventsOnly = true;
    }

resource

  • [ JDK ] openjdk.java.net
  • [ doc - 參考 ] docs.oracle.com/en/java/javase/11
  • [ 規範 - 推薦 ] yq.aliyun.com/articles/69327
  • [ 規範 - 推薦 ] google.github.io/styleguide
  • [ 源碼 ] hg.openjdk.java.net
  • [ OS ] www.centos.org
  • [ IDE ] www.eclipse.org/downloads/packages
  • [ 平臺 ] www.cnblogs.com


感謝幫助過 給最苦 的人們。
Java、Groovy和Scala等基於JVM的語言,優秀,值得學習。
規範的命名和代碼格式等,有助於溝通和理解。
JVM的配置、監控與優化,比較實用,值得學習。html

相關文章
相關標籤/搜索