import javax.swing.*; // for JFrame, JButton import java.awt.*; // for FlowLayout public class Ch12_1 // create a simple GUI of 6 JButtons, see page 476 of the textbook { // NOTE: the JButtons will not respond if clicked, we study how to do that in chapter 16 public static void main(String[] args) { JFrame frame=new JFrame("Exercise12_1"); // create JFrame and set its name frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // establish what to do when GUI is closed JButton b1=new JButton("Button 1"); // add the 6 JButtons JButton b2=new JButton("Button 2"); JButton b3=new JButton("Button 3"); JButton b4=new JButton("Button 4"); JButton b5=new JButton("Button 5"); JButton b6=new JButton("Button 6"); JPanel p1=new JPanel(); // create two JPanels to position 3 JButtons on each JPanel p2=new JPanel(); // this will allow us to have a slight space between JButton3 and 4 p1.add(b1); // add the first 3 JButtons to p1 p1.add(b2); p1.add(b3); p2.add(b4); // add the latter 3 JButtons to p2 p2.add(b5); p2.add(b6); frame.setSize(600,80); // set the JFrame's size, notice it is not very high frame.setLayout(new FlowLayout()); // use FlowLayout - all items placed on same row if possible frame.add(p1); // add the two JPanels, one after the other on the same row frame.add(p2); frame.setVisible(true); // make this JFrame visible } }