Java基础 awt Button 点击按钮后在控制台输出文字
生活随笔
收集整理的這篇文章主要介紹了
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的配置、監控與優化,比較實用,值得學習。
總結
以上是生活随笔為你收集整理的Java基础 awt Button 点击按钮后在控制台输出文字的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python图片转文字_【收藏】图片转成
- 下一篇: python 利用cip.cc查询IP归