1.常用方法
| 方法 | 描述 |
|---|---|
| JButton() | 创建一个没有文本或图标的按钮 |
| JButton(Icon icon) | 创建一个带图标的按钮 |
| JButton(String text) | 创建一个带文本的按钮 |
| JButton(Action a) | 创建一个按钮,其中的属性取自提供的 Action |
| JButton(String text, Icon icon) | 创建一个带有初始文本和图标的按钮 |
| updateUI() | 将UI属性重置为当前外观的值 |
| getUIClassID() | 返回一个字符串,它指定呈现此组件的L&F类的名称。 |
| isDefaultButton() | 获取defaultButton属性的值,如果true意味着此按钮是其JRootPane的当前默认按钮。 大多数外观和感觉呈现默认按钮不同,并可能提供绑定来访问默认按钮。 |
| isDefaultCapable() | 获取 defaultCapable属性的值 |
| setDefaultCapable(boolean defaultCapable) | 获取 defaultCapable属性的值 |
| removeNotify() | 覆盖 JComponent.removeNotify ,以检查此按钮当前被设置为在默认按钮 RootPane ,如果是的话,设置 RootPane的默认按钮 null ,以确保 RootPane不继续停留在无效的按钮引用。 |
| paramString() | 返回此JButton的字符串表示JButton 。 该方法仅用于调试目的,并且返回的字符串的内容和格式可能因实现而异。 返回的字符串可能为空,但可能不是null 。 |
| getAccessibleContext() | 获取AccessibleContext与此相关JButton 。 为JButton s时, AccessibleContext需要一个的形式AccessibleJButton 。 如果需要,将创建一个新的AccessibleJButton实例。 |
2.基本示例
public class TestJButton extends Jframe{
//面板
private JPanel jPanel;
//测试按钮
private JButton jButton;
public TestJButton(){
jPanel = new JPanel();
jButton = new JButton("测试按钮");
//面板添加按钮
jPanel.add(jButton);
//添加面板
add(jPanel);
}
public void showframe(){
setTitle("测试图形界面程序");
//设置窗口的大小
setSize(400, 400);
//设置距离屏幕左上角的距离
setLocation(800, 300);
//必须添加完对应的组件之后设置显示
setVisible(true);
setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
TestJButton testJButton = new TestJButton();
testJButton.showframe();
}
}
显示效果



