Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- //=======================JButton with Event Listner======================================
- 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);
- }
- }
- //=========================DisPLAY IMAGE ON BUTTON ========================
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement