import java.awt.*; import javax.swing.*; public class SimpleButton extends JFrame { public SimpleButton() { setTitle("Simple Button"); JButton button = new JButton("Click me"); add(button); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { new SimpleButton(); } }
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ButtonCounter extends JFrame implements ActionListener { private int count = 0; private JLabel label; public ButtonCounter() { setTitle("Button Counter"); JButton button = new JButton("Click me"); button.addActionListener(this); add(button, BorderLayout.CENTER); label = new JLabel("Count: " + count, JLabel.CENTER); add(label, BorderLayout.SOUTH); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public void actionPerformed(ActionEvent e) { count++; label.setText("Count: " + count); } public static void main(String[] args) { new ButtonCounter(); } }Both examples use the Java package library java.awt, which includes components required to create graphical user interfaces in Java.