修飾子與參數
public String getActionCommand()
getActionCommand() 的回傳值 (return value) 為 String 型態 (type) 的物件 (object),不需要參數 (paramenter) 。
舉例如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class getActionCommandDemo implements ActionListener {
JButton demo;
public static void main(String[] args) {
new getActionCommandDemo();
}
public getActionCommandDemo() {
JFrame frame = new JFrame("SwingDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo = new JButton("demo");
demo.addActionListener(this);
demo.setActionCommand("demo");
frame.setLayout(new FlowLayout());
frame.add(demo);
frame.pack();
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
System.out.println(demo.getActionCommand());
}
}
/* 《程式語言教學誌》的範例程式
http://pydoing.blogspot.com/
檔名:getActionCommandDemo.java
功能:示範 Java 程式
作者:張凱慶
時間:西元 2012 年 9 月 */此例的 getActionCommandDemo 直接實作 ActionListener 介面
public class getActionCommandDemo implements ActionListener {然後建立 JButton ,其為 AbstractButton 的子類別 (subclass) ,因此呼叫 addActionListener() 設定事件處理,並以 this 當參數,然後呼叫 setActionCommand() 將按鈕指令設定為 "demo"
demo = new JButton("demo");
demo.addActionListener(this);
demo.setActionCommand("demo");由於 getActionCommandDemo 實作 ActionListener 介面,因此要寫出 actionPerformed()
public void actionPerformed(ActionEvent e) {
System.out.println(demo.getActionCommand());
}如果使用者點擊 demo 按鈕,命令列就會印出 getActionCommand() 的回傳值。
編譯後執行,結果如下

| 中英文術語對照 | |
|---|---|
| 類別 | class |
| 方法 | method |
| 回傳值 | return value |
| 型態 | type |
| 物件 | object |
| 參數 | parameter |
| 子類別 | subclass |
您可以繼續參考
Swing 元件
- ButtonGroup
- JComponent
- AbstractButton
- JButton
- JToggleButton
- JCheckBox
- JRadioButton
- JMenuItem
- JCheckBoxMenuItem
- JMenu
- JRadioButtonMenuItem
- JColorChooser
- JComboBox
- JFileChooser
- JInternalFrame
- JLabel
- JLayeredPane
- JDesktopPane
- JList
- JMenuBar
- JOptionPane
- JPanel
- JPopupMenu
- JProgressBar
- JRootPane
- JScrollBar
- JScrollPane
- JSeparator
- JSlider
- JSpinner
- JSplitPane
- JTabbedPane
- JTable
- text.JTextComponent
- JEditorPane
- JTextPane
- JTextField
- JFormattedTextField
- JPasswordField
- JTextArea
- JToolBar
- JToolTip
- JTree
- JViewport
- JDialog
- JFrame
- JWindow
相關目錄
Java API 分類導覽
Java 教材
首頁
參考資料
http://docs.oracle.com/javase/6/docs/api/javax/swing/AbstractButton.html
沒有留言:
張貼留言