JButton
類是按鈕的實現。該組件具有標籤,並在按下時生成事件。它也可以有一個圖像。
類聲明
以下是javax.swing.JButton
類的聲明 -
public class JButton
extends AbstractButton
implements Accessible
類構造函數
編號 | 構造函數 | 描述 |
---|---|---|
1 | JButton() |
創建一個沒有設置文本或圖示的按鈕。 |
2 | JButton(Action a) |
創建一個按鈕,屬性取自提供的Action。 |
3 | JButton(Icon icon) |
創建一個帶圖示的按鈕。 |
4 | JButton(String text) |
創建一個包含文本的按鈕。 |
5 | JButton(String text, Icon icon) |
創建一個包含初始文本和圖示的按鈕。 |
類方法
編號 | 方法 | 描述 |
---|---|---|
1 | AccessibleContext getAccessibleContext() |
獲取與此JButton 關聯的AccessibleContext 。 |
2 | String getUIClassID() |
返回一個字串,該字串指定呈現此組件的L&F類的名稱。 |
3 | boolean isDefaultButton() |
獲取defaultButton 屬性的值,如果為true ,則表示此按鈕是JRootPane 的當前默認按鈕。 |
4 | boolean isDefaultCapable() |
獲取defaultCapable 屬性的值。 |
5 | protected String paramString() |
返回此JButton 的字串表示形式。 |
6 | void removeNotify() |
覆蓋JComponent.removeNotify 以檢查此按鈕當前是否設置為RootPane 上的默認按鈕。如果是,請將RootPane 的默認按鈕設置為null ,以確保RootPane 不會保留無效的按鈕引用。 |
7 | void setDefaultCapable(boolean defaultCapable) |
設置defaultCapable 屬性,該屬性確定是否可以將此按鈕設置為其根窗格的默認按鈕。 |
8 | void updateUI() |
將UI屬性重置為當前外觀的值。 |
方法繼承
該類繼承以下類中的方法 -
javax.swing.AbstractButton
javax.swing.JComponent
java.awt.Container
java.awt.Component
java.lang.Object
JButton示例
使用編輯器創建以下Java程式:SwingButton.java
// package com.zaixian.swing;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingButton {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
public SwingButton(){
prepareGUI();
}
public static void main(String[] args){
SwingButton swingControlDemo = new SwingButton();
swingControlDemo.showButtonDemo();
}
private void prepareGUI(){
mainFrame = new JFrame("Java Swing JButton示例");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
private static ImageIcon createImageIcon(String path, String description) {
java.net.URL imgURL = SwingButton.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
private void showButtonDemo(){
headerLabel.setText("Control in action: Button");
//resources folder should be inside SWING folder.
ImageIcon icon = createImageIcon("/resources/java_icon.jpg","Java");
JButton okButton = new JButton("好了");
JButton javaButton = new JButton("提交", icon);
JButton cancelButton = new JButton("取消", icon);
cancelButton.setHorizontalTextPosition(SwingConstants.LEFT);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusLabel.setText("'好了'按鈕提交");
}
});
javaButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusLabel.setText("'提交'按鈕提交");
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
statusLabel.setText("'取消'按鈕提交");
}
});
controlPanel.add(okButton);
controlPanel.add(javaButton);
controlPanel.add(cancelButton);
mainFrame.setVisible(true);
}
}
執行上面示例代碼,得到以下結果: