//TestGUISwing1.java //Just create and show a JFrame. //Size is the default obtained by calling pack(). import java.awt.*; import javax.swing.*; public class TestGUISwing1 { public static void main(String[] args) { JFrame frame = new JFrame("My JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.show(); } } //TestGUISwing2.java //Just create and show a JFrame. //Size is determined by setting a specific width and height. import java.awt.*; import javax.swing.*; public class TestGUISwing2 { public static void main(String[] args) { JFrame frame = new JFrame("My JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 300); frame.setVisible(true); //frame.show(); //Can use this line in place of one above. } } //TestGUISwing3.java //Add two buttons to a panel and add a panel to the frame. import java.awt.*; import javax.swing.*; public class TestGUISwing3 { public static void main(String[] args) { JFrame frame = new JFrame("My JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton b1 = new JButton("First"); JButton b2 = new JButton("Second"); JPanel panel = new JPanel(); panel.add(b1); panel.add(b2); frame.getContentPane().add(panel); frame.pack(); frame.show(); } } //TestGUISwing4.java //Add two buttons to a panel and add a panel to the frame. //Then add another button to the frame. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestGUISwing4 { public static void main(String[] args) { JFrame frame = new JFrame("My JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JButton b1 = new JButton("First"); JButton b2 = new JButton("Second"); panel.add(b1); panel.add(b2); frame.getContentPane().add(panel); JButton b3 = new JButton("Third"); frame.getContentPane().add(b3); frame.pack(); frame.show(); } } //TestGUISwing5.java //First, set the layout of the frame to FlowLayout. //Then add two buttons to a panel and add a panel to the frame. //Then add another button to the frame. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestGUISwing5 { public static void main(String[] args) { JFrame frame = new JFrame("My JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(new FlowLayout()); JPanel panel = new JPanel(); JButton b1 = new JButton("First"); JButton b2 = new JButton("Second"); panel.add(b1); panel.add(b2); frame.getContentPane().add(panel); JButton b3 = new JButton("Third"); frame.getContentPane().add(b3); frame.pack(); frame.show(); } } //TestGUISwing6.java //Add two buttons to a panel and add a panel to the frame. //Then add another button to the frame, but make use of the //fact that the default layout is BorderLayout to position //this third button in the WEST position of the border layout. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestGUISwing6 { public static void main(String[] args) { JFrame frame = new JFrame("My JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JButton b1 = new JButton("First"); JButton b2 = new JButton("Second"); panel.add(b1); panel.add(b2); frame.getContentPane().add(panel); JButton b3 = new JButton("Third"); frame.getContentPane().add(b3, BorderLayout.WEST); frame.pack(); frame.show(); } } //TestGUISwing7.java //Add two buttons to a panel and add a panel to the frame. //Then add another button to the frame, but make use of the //fact that the default layout is BorderLayout to position //this third button in the CENTER position of the border layout. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestGUISwing7 { public static void main(String[] args) { JFrame frame = new JFrame("My JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JButton b1 = new JButton("First"); JButton b2 = new JButton("Second"); panel.add(b1); panel.add(b2); frame.getContentPane().add(panel); JButton b3 = new JButton("Third"); frame.getContentPane().add(b3, BorderLayout.CENTER); frame.pack(); frame.show(); } } //TestGUISwing8.java //This time add a button first, then the panel with two buttons. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestGUISwing8 { public static void main(String[] args) { JFrame frame = new JFrame("My JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); JButton b1 = new JButton("First"); JButton b2 = new JButton("Second"); panel.add(b1); panel.add(b2); JButton b3 = new JButton("Third"); frame.getContentPane().add(b3); frame.getContentPane().add(panel); frame.pack(); frame.show(); } } //TestGUISwing9.java //Show some color and additional layout features. //Also note the use of setOpaque for the label. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TestGUISwing9 { public static void main(String[] args) { JFrame frame = new JFrame("My JFrame"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); panel.setBackground(Color.YELLOW); JButton b1 = new JButton("First"); JButton b2 = new JButton("Second"); panel.add(b1); panel.add(b2); panel.setBorder(BorderFactory.createEmptyBorder(30, //top 30, //left 10, //bottom 30) //right ); JButton b3 = new JButton("Third"); b3.setBackground(Color.WHITE); JLabel label = new JLabel("Center", SwingConstants.CENTER); label.setOpaque(true); label.setBackground(Color.CYAN); frame.getContentPane().add(panel, BorderLayout.NORTH); frame.getContentPane().add(b3, BorderLayout.SOUTH); frame.getContentPane().add(label, BorderLayout.CENTER); frame.pack(); frame.show(); } }