Guest User

Untitled

a guest
Oct 21st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. public static void main(String[] args){
  2. SwingUtilities.invokeLater(new Runnable(){
  3. public void run(){
  4. LeftoverExample.createGUI();
  5. }
  6. });
  7. }
  8.  
  9. public static void createGUI(){
  10. JFrame jF = new JFrame();
  11. jF.setSize(new Dimension(500,500));
  12. jF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13.  
  14. //GBL Example
  15. JPanel gBLExamplePanel = new JPanel();
  16. GridBagLayout gBL = new GridBagLayout();
  17. gBL.columnWidths = new int[]{0};
  18. gBL.rowHeights = new int[]{50};
  19. gBLExamplePanel.setLayout(gBL);
  20.  
  21. //Initial Constraints
  22. GridBagConstraints gBC = new GridBagConstraints();
  23. gBC.fill = GridBagConstraints.BOTH;
  24. gBC.gridx = 0;
  25. gBC.gridy = 0;
  26. gBC.weightx = 1;
  27. gBC.weighty = 0;
  28. gBC.insets = new Insets(0, 0, 0, 0);
  29.  
  30. //Add to GBL Panel
  31. LeftoverExample.addButtons(gBLExamplePanel, gBC);
  32. LeftoverExample.addButtons(gBLExamplePanel, gBC);
  33. LeftoverExample.addButtons(gBLExamplePanel, gBC);
  34.  
  35. //BoxLayout Example
  36. JPanel boxLayoutExamplePanel = new JPanel();
  37. boxLayoutExamplePanel.setLayout(new BoxLayout(boxLayoutExamplePanel, BoxLayout.X_AXIS));
  38.  
  39. //Add to BoxLayout Panel
  40. LeftoverExample.addButtons(boxLayoutExamplePanel);
  41. LeftoverExample.addButtons(boxLayoutExamplePanel);
  42. LeftoverExample.addButtons(boxLayoutExamplePanel);
  43.  
  44. //Add Panels to ContentPane
  45. jF.getContentPane().add(gBLExamplePanel);
  46. jF.getContentPane().add(boxLayoutExamplePanel, BorderLayout.SOUTH);
  47.  
  48. //Final
  49. jF.setVisible(true);
  50. }
  51.  
  52. private static JButton createButton(Color c){
  53. JButton jB = new JButton();
  54. jB.setBackground(c);
  55. jB.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
  56. return jB;
  57. }
  58.  
  59. private static void addButtons(JComponent jC, GridBagConstraints gBC){
  60. //Create Buttons
  61. Color[] colorA = {Color.RED, Color.BLUE, Color.BLACK, Color.GREEN};
  62. for(Color c : colorA){
  63. jC.add(LeftoverExample.createButton(c), gBC);
  64. gBC.gridx++;
  65. }
  66. }
  67.  
  68. private static void addButtons(JComponent jC){
  69. //Create Buttons
  70. Color[] colorA = {Color.BLUE, Color.BLACK, Color.GREEN, Color.RED};
  71. for(Color c : colorA){
  72. jC.add(LeftoverExample.createButton(c));
  73. }
  74. }
Add Comment
Please, Sign In to add comment