|
按钮是一个常用组件,按钮可以带标签或图象。
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.Container
|
+--javax.swing.JComponent
|
+--javax.swing.AbstractButton
|
+--javax.swing.JButton
常用的构造方法有:
JButton(Icon icon) //按钮上显示图标
JButton(String text) //按钮上显示字符
JButton(String text, Icon icon) //按钮上既显示图标又显示字符
例7.2
public class ButtonDemo extends Jpanel implements ActionListener{
JButton b1,b2,b3;
public ButtonDemo() {
super();
ImageIcon leftButtonIcon=new ImageIcon("images/right.gif);
//显示在左按钮上的图标
ImageIcon middleButtonIcon=new ImageIcon("images/middle.gif);
//显示在中间按钮上的图标
ImageIcon middleButtonIcon=new ImageIcon("images/left.gif);
//显示在右按钮上的图标
b1=new JButton("Disable middle button",leftButtonIcon);
//按钮b1上同时显示文字和图标
b1.setVerticalTextPosition(AbstractButton.CENTER);
//按钮b1上的文字在垂直方向上是居中对齐
b1.setHorizontalTextPosition(AbstractButton.LEFT);
//按钮b1上的文字在水平居方向上是居左对齐
b1.setMnemonic('d'); //设置按钮b1的替代的键盘按键是'd'
b1.setActionCommand("diaable");
……
}
}

|