In this example we are going to see how to use Swing JButton In Java Example.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.AbstractButton; public class Main extends JFrame { JButton jButton; Main(String title) { super(title); setLayout(new FlowLayout()); jButton = new JButton("Click Me!"); add(jButton); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); String text = abstractButton.getText(); System.out.println(text); } }; jButton.addActionListener(actionListener); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { Main frm = new Main("Button Demo"); frm.setSize(150, 75); frm.setVisible(true); } } |
1 2 3 4 | Click Me! Click Me! |