import javax.swing.*; import java.awt.*; class Ex5 { // Screen is divided into many pixels // Pixel is a unit of drawing final static int XPIXELS = 800; final static int YPIXELS = 600; static JLabel msg; static int numClicks = 0; public static void main(String[] args) { // JFrame is a predefined class JFrame jf = new JFrame("Example-5 in Java GUI"); // What to do when window is closed? jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the size of the window (in terms of pixels) jf.setSize(new Dimension(XPIXELS, YPIXELS)); // Create components Container con = jf.getContentPane(); // Create a panel JPanel p1 = new JPanel(); //p1.setBackground(Color.YELLOW); p1.setBackground(new Color((float)0.8, (float)1.0, (float)0.8)); p1.setBorder(BorderFactory.createLineBorder(Color.BLACK)); p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS)); p1.add(Box.createRigidArea(new Dimension(100, 50))); // Create a label JLabel label = new JLabel("Example-5"); p1.add(label); // Create a button JButton btn = new JButton("Click-me"); p1.add(btn); // Add the msg label msg = new JLabel(); p1.add(msg); msg.setText("Number of clicks is "+numClicks); btn.addActionListener(new ClickListener()); con.add(p1); // Show the frame jf.setVisible(true); // Note: main does not exit here } // End main() }