Guest User

Untitled

a guest
Jan 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. GridLayout experimentLayout = new GridLayout(0,2);//create grid any amount of rows and 2 coloumns
  2.  
  3. ...
  4.  
  5. compsToExperiment.setLayout(experimentLayout);//add gridlayout to Component/JPanel
  6.  
  7. compsToExperiment.add(new JButton("Button 1"));
  8. compsToExperiment.add(new JButton("Button 2"));
  9. compsToExperiment.add(new JButton("Button 3"));
  10. compsToExperiment.add(new JButton("Long-Named Button 4"));
  11. compsToExperiment.add(new JButton("5"));
  12.  
  13. JButton button;
  14. pane.setLayout(new GridBagLayout());
  15. GridBagConstraints c = new GridBagConstraints();
  16. if (shouldFill) {
  17.                 //natural height, maximum width
  18.                 c.fill = GridBagConstraints.HORIZONTAL;
  19. }
  20.  
  21.  
  22.  
  23.  
  24. button = new JButton("Button 1");
  25. if (shouldWeightX) {
  26.                    c.weightx = 0.5;
  27. }
  28. c.fill = GridBagConstraints.HORIZONTAL;
  29. c.gridx = 0;
  30. c.gridy = 0;
  31. pane.add(button, c);
  32.  
  33.  
  34.  
  35.  
  36. button = new JButton("Button 2");
  37. c.fill = GridBagConstraints.HORIZONTAL;
  38. c.weightx = 0.5;
  39. c.gridx = 1;
  40. c.gridy = 0;
  41. pane.add(button, c);
  42.  
  43.  
  44.  
  45.  
  46. button = new JButton("Button 3");
  47. c.fill = GridBagConstraints.HORIZONTAL;
  48. c.weightx = 0.5;
  49. c.gridx = 2;
  50. c.gridy = 0;
  51. pane.add(button, c);
  52.  
  53.  
  54.  
  55.  
  56. button = new JButton("Long-Named Button 4");
  57. c.fill = GridBagConstraints.HORIZONTAL;
  58. c.ipady = 40;      //make this component tall
  59. c.weightx = 0.0;
  60. c.gridwidth = 3;
  61. c.gridx = 0;
  62. c.gridy = 1;
  63. pane.add(button, c);
  64.  
  65.  
  66.  
  67.  
  68. button = new JButton("5");
  69. c.fill = GridBagConstraints.HORIZONTAL;
  70. c.ipady = 0;       //reset to default
  71. c.weighty = 1.0;   //request any extra vertical space
  72. c.anchor = GridBagConstraints.PAGE_END; //bottom of space
  73. c.insets = new Insets(10,0,0,0);  //top padding
  74. c.gridx = 1;       //aligned with button 2
  75. c.gridwidth = 2;   //2 columns wide
  76. c.gridy = 2;       //third row
  77. pane.add(button, c);
Add Comment
Please, Sign In to add comment