/* This applet demonstrates how to do your own layout. The layout manager for the content pane is set to null. The setBounds() method of each component is called to set the size and position of the component. It is assumed that this applet is 350 pixels wide and 240 pixels high! If you want to deal with other sizes, you should implement the ComponentEvent interface, and compute the sizes and positions of the components in terms of the actual width and height of the applet. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class NullLayoutDemo extends JApplet implements ActionListener { Checkerboard board; // A checkerboard. The Checkerboard class is // nested inside this NullLayoutDemo class. JButton resignButton; // Two buttons. JButton newGameButton; JLabel message; // A label for displaying messages to the user. public void init() { getContentPane().setLayout(null); // I will do the layout myself. getContentPane().setBackground(new Color(0,150,0)); // Set a dark green background. /* Create the components and add them to the content pane. If you don't add them to the a container, they won't appear, even if you set their bounds! */ board = new Checkerboard(); // (Checkerboard is defined later in this class.) getContentPane().add(board); newGameButton = new JButton("New Game"); newGameButton.addActionListener(this); getContentPane().add(newGameButton); resignButton = new JButton("Resign"); resignButton.addActionListener(this); getContentPane().add(resignButton); message = new JLabel("Click \"New Game\" to begin a game.", JLabel.CENTER); message.setForeground( new Color(100,255,100) ); message.setFont(new Font("Serif", Font.BOLD, 14)); getContentPane().add(message); /* Set the position and size of each component by calling its setBounds() method. */ board.setBounds(20,20,164,164); newGameButton.setBounds(210, 60, 120, 30); resignButton.setBounds(210, 120, 120, 30); message.setBounds(0, 200, 330, 30); /* Add a border to the content pane. Since the return type of getContentPane() is Container, not JComponent, getContentPane() must be type-cast to a JComponent in order to call the setBorder() method. Although I think the content pane is always, in fact, a JPanel, to be safe I test that the return value really is a JComponent. */ if (getContentPane() instanceof JComponent) { ((JComponent)getContentPane()).setBorder( BorderFactory.createEtchedBorder()); } } public void actionPerformed(ActionEvent evt) { // In this demo applet, clicking on a button just // changes the displayed message. message.setText(evt.getActionCommand() + " button was pressed."); } class Checkerboard extends JPanel { // This canvas displays a 160-by-160 checkerboard pattern with // a 2-pixel black border. It is assumed that the size of the // board is set to exactly 164-by-164 pixels. This size is // set as the preferred size of the board. public Checkerboard() { setBackground(Color.black); setPreferredSize( new Dimension(164, 164) ); } public void paintComponent(Graphics g) { // Draw a 2-pixel black border around the edges of the board. // (There is no need to call super.paintComponent() since // this method paints the entire survace of the component.) g.setColor(Color.black); g.drawRect(0, 0, getSize().width - 1, getSize().height - 1); g.drawRect(1, 1, getSize().width - 3, getSize().height - 3); // Draw checkerboard pattern in gray and lightGray. for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { if ( row % 2 == col % 2 ) g.setColor(Color.lightGray); else g.setColor(Color.gray); g.fillRect(2 + col*20, 2 + row*20, 20, 20); } } } } // end class Checkerboard } // end class NullLayoutDemo