📅  最后修改于: 2020-09-29 09:58:47             🧑  作者: Mango
JButton类用于创建具有平台独立实现的带标签的按钮。按下按钮后,应用程序将执行某些操作。它继承了AbstractButton类。
我们来看一下javax.swing.JButton类的声明。
public class JButton extends AbstractButton implements Accessible
Constructor | Description |
---|---|
JButton() | It creates a button with no text and icon. |
JButton(String s) | It creates a button with the specified text. |
JButton(Icon i) | It creates a button with the specified icon object. |
Methods | Description |
---|---|
void setText(String s) | It is used to set specified text on button |
String getText() | It is used to return the text of the button. |
void setEnabled(boolean b) | It is used to enable or disable the button. |
void setIcon(Icon b) | It is used to set the specified Icon on the button. |
Icon getIcon() | It is used to get the Icon of the button. |
void setMnemonic(int a) | It is used to set the mnemonic on the button. |
void addActionListener(ActionListener a) | It is used to add the action listener to this object. |
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
f.add(b);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
输出:
import java.awt.event.*;
import javax.swing.*;
public class ButtonExample {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
final JTextField tf=new JTextField();
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
}
输出:
import javax.swing.*;
public class ButtonExample{
ButtonExample(){
JFrame f=new JFrame("Button Example");
JButton b=new JButton(new ImageIcon("D:\\icon.png"));
b.setBounds(100,100,100, 40);
f.add(b);
f.setSize(300,400);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ButtonExample();
}
}
输出: