Advertisement
Guest User

GridPanelWindow

a guest
Jul 20th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. import javax.swing.*; // Needed for Swing classes
  2. import java.awt.*; // Needed for GridLayout class
  3.  
  4. /**
  5. This class demonstrates how panels may be added to
  6. the cells created by a GridLayout manager.
  7. */
  8.  
  9. public class GridPanelWindow extends JFrame
  10. {
  11. private final int WINDOW_WIDTH = 400; // Window width
  12. private final int WINDOW_HEIGHT = 200; // Window height
  13.  
  14. /**
  15. Constructor
  16. */
  17.  
  18. public GridPanelWindow()
  19. {
  20. // Set the title bar text.
  21. setTitle("Grid Layout");
  22.  
  23. // Set the size of the window.
  24. setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  25.  
  26. // Specify an action for the close button.
  27. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.  
  29. // Add a GridLayout manager to the content pane.
  30. setLayout(new GridLayout(2, 3));
  31.  
  32. // Create six buttons.
  33. JButton button1 = new JButton("Button 1");
  34. JButton button2 = new JButton("Button 2");
  35. JButton button3 = new JButton("Button 3");
  36. JButton button4 = new JButton("Button 4");
  37. JButton button5 = new JButton("Button 5");
  38. JButton button6 = new JButton("Button 6");
  39.  
  40. // Create six labels.
  41. JLabel label1 = new JLabel("This is cell 1.");
  42. JLabel label2 = new JLabel("This is cell 2.");
  43. JLabel label3 = new JLabel("This is cell 3.");
  44. JLabel label4 = new JLabel("This is cell 4.");
  45. JLabel label5 = new JLabel("This is cell 5.");
  46. JLabel label6 = new JLabel("This is cell 6.");
  47.  
  48. // Create six panels.
  49. JPanel panel1 = new JPanel();
  50. JPanel panel2 = new JPanel();
  51. JPanel panel3 = new JPanel();
  52. JPanel panel4 = new JPanel();
  53. JPanel panel5 = new JPanel();
  54. JPanel panel6 = new JPanel();
  55.  
  56. // Add the labels to the panels.
  57. panel1.add(label1);
  58. panel2.add(label2);
  59. panel3.add(label3);
  60. panel4.add(label4);
  61. panel5.add(label5);
  62. panel6.add(label6);
  63.  
  64. // Add the buttons to the panels.
  65. panel1.add(button1);
  66. panel2.add(button2);
  67. panel3.add(button3);
  68. panel4.add(button4);
  69. panel5.add(button5);
  70. panel6.add(button6);
  71.  
  72. // Add the panels to the content pane.
  73. add(panel1); // Goes into row 1, column 1
  74. add(panel2); // Goes into row 1, column 2
  75. add(panel3); // Goes into row 1, column 3
  76. add(panel4); // Goes into row 2, column 1
  77. add(panel5); // Goes into row 2, column 2
  78. add(panel6); // Goes into row 2, column 3
  79.  
  80. // Display the window.
  81. setVisible(true);
  82. }
  83.  
  84. /**
  85. The main method creates an instance of the
  86. GridPanelWindow class, displaying its window.
  87. */
  88.  
  89. public static void main(String[] args)
  90. {
  91. new GridPanelWindow();
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement