Advertisement
Guest User

Untitled

a guest
Feb 10th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.23 KB | None | 0 0
  1. /*Form.java
  2. ex 14.11 p 662
  3. */
  4. import java.awt.GridBagLayout;//chosen JFrame layout
  5. import java.awt.GridBagConstraints.*;
  6. import javax.swing.JFrame;
  7. import javax.swing.JComboBox;
  8. import javax.swing.JButton;
  9. import javax.swing.JTextArea;
  10. import javax.swing.JCheckBox;
  11. import javax.swing.JRadioButton;
  12. import javax.swing.JLabel;
  13. import javax.swing.ButtonGroup;//for radio buttons
  14. import java.awt.GridLayout;//chosen layout for panel selectionPanel and buttonsPanel
  15. import java.awt.FlowLayout;//chosen layout for panel printQualityPanel
  16. import javax.swing.JPanel;
  17.  
  18. public class Form extends JFrame{
  19. //label
  20. private JLabel printerLabel;
  21. private JLabel printQualityLabel;
  22.  
  23. //txt areas
  24. private JTextArea textArea1;
  25. private JTextArea textArea2;
  26. private JTextArea textArea3;
  27.  
  28. //checkboxes
  29. private JCheckBox imageCheckBox;
  30. private JCheckBox textCheckBox;
  31. private JCheckBox codeCheckBox;
  32. private JCheckBox printCheckBox;
  33.  
  34. //radio buttons
  35. private JRadioButton selectionRadioButton;
  36. private JRadioButton allRadioButton;
  37. private JRadioButton appletRadioButton;
  38. private ButtonGroup radioGroup;//to create logical connection between radio buttons
  39.  
  40. //jcombobox
  41. private JComboBox<String> qualityCombo;
  42. private String[] values;
  43.  
  44. //buttons
  45. private JButton okButton;
  46. private JButton cancelButton;
  47. private JButton setupButton;
  48. private JButton helpButton;
  49.  
  50. //panels
  51. private JPanel selectionPanel;//contains text areas, radio buttons and chekboxes
  52. private JPanel printQualityPanel;//contains print quality combo box
  53. private JPanel buttonsPanel;//contains buttons
  54.  
  55. //layout
  56. private GridBagLayout jPanel1Layout;
  57. private GridBagLayout jPanel2Layout;
  58. private GridBagLayout jPanel3Layout;
  59. private GridBagLayout labelLayout;
  60. private GridBagLayout layout;//layout of JFrame
  61.  
  62. //constraints
  63. private GridBagConstraints constraints;
  64.  
  65. //constructor
  66. public Form(){
  67. super("Form GUI");
  68. layout = new GridBagLayout();
  69. setLayout( layout );//set layout of JFrame
  70.  
  71. //create panels
  72. selectionPanel = new JPanel();
  73. printQualityPanel = new JPanel();
  74. buttonsPanel = new JPanel();
  75.  
  76. //create flow layout for aligning panels one next to the other
  77. jPanel1Layout = new GridBagLayout();
  78. jPanel2Layout = new GridBagLayout();
  79. jPanel3Layout = new GridBagLayout();
  80. labelLayout = new GridBagLayout();
  81. constraints = new GridBagConstraints();
  82.  
  83. //create controls
  84. //labels
  85. printerLabel = new JLabel("Printer: My printer");
  86. printQualityLabel = new JLabel("Print Quality:");
  87. //txt areas
  88. textArea1 = new JTextArea( 5, 5 );
  89. textArea2 = new JTextArea( 5, 5 );
  90. textArea3 = new JTextArea( 5, 5 );
  91. //checkboxes
  92. imageCheckBox = new JCheckBox("Image");
  93. textCheckBox = new JCheckBox("Image");
  94. codeCheckBox = new JCheckBox("Image");
  95. printCheckBox = new JCheckBox("Print to file");
  96. //radio buttons
  97. selectionRadioButton = new JRadioButton("Selection", false);
  98. allRadioButton = new JRadioButton("All", true);
  99. appletRadioButton = new JRadioButton("Applet", false);
  100. //add radio buttons to the group
  101. radioGroup = new ButtonGroup();
  102. radioGroup.add( selectionRadioButton );
  103. radioGroup.add( allRadioButton );
  104. radioGroup.add( appletRadioButton );
  105. //combo box
  106. String[] values = { "Value1", "Value2", "Value3", "Value4" };
  107. qualityCombo = new JComboBox<>( values );
  108. //buttons
  109. okButton = new JButton("OK");
  110. cancelButton = new JButton("Cancel");
  111. setupButton = new JButton("Setup");
  112. helpButton = new JButton("Help");
  113. okButton = new JButton("OK");
  114.  
  115. //BASED ON A 6X7 GRID!!
  116.  
  117. //first text area
  118. selectionPanel.add( textArea1, new GridBagConstraints( 0, 2, 1, 3, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//set constraints on the element and add to jpanel
  119.  
  120. //second txt area
  121. selectionPanel.add( textArea2, new GridBagConstraints( 2, 2, 1, 3, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add component to jpanel and set constraints
  122.  
  123. //third text area
  124. selectionPanel.add( textArea3, new GridBagConstraints( 4, 2, 1, 3, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add component to jpanel and add constraints
  125.  
  126.  
  127. //checkboxes
  128. selectionPanel.add( imageCheckBox, new GridBagConstraints( 1, 2, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and add constraints
  129. selectionPanel.add( textCheckBox, new GridBagConstraints( 1, 3, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and add constraints
  130. selectionPanel.add( codeCheckBox, new GridBagConstraints( 1, 4, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and add constraints
  131.  
  132. //radio buttons
  133. selectionPanel.add( selectionRadioButton, new GridBagConstraints( 3, 2, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints
  134. selectionPanel.add( allRadioButton, new GridBagConstraints( 3, 3, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints
  135. selectionPanel.add( appletRadioButton, new GridBagConstraints( 3, 4, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints
  136. //END OF FIRST PANEL
  137.  
  138. //SECOND PANEL containing combo box and print to file
  139. //label
  140. printQualityPanel.add( printQualityLabel, new GridBagConstraints( 0, 5, 1, 2, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints
  141.  
  142. //jcombo
  143. printQualityPanel.add( qualityCombo, new GridBagConstraints( 3, 5, 2, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints
  144.  
  145. //bottom checkbox
  146. printQualityPanel.add( printCheckBox, new GridBagConstraints( 5, 5, 2, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints
  147. //END OF SECOND PANEL
  148.  
  149. //THIRD PANEL
  150. //buttons
  151. buttonsPanel.add( okButton, new GridBagConstraints( 6, 0, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add component to jpanel and constraints
  152. buttonsPanel.add( cancelButton, new GridBagConstraints( 6, 2, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints
  153. buttonsPanel.add( setupButton, new GridBagConstraints( 6, 4, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraint
  154. buttonsPanel.add( helpButton, new GridBagConstraints( 6, 6, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0) ) );//add component to jpanel and constraints
  155.  
  156. //END OF THIRD PANEL
  157.  
  158. //set layout of JPanels
  159. selectionPanel.setLayout( jPanel1Layout ); //
  160. printQualityPanel.setLayout( jPanel2Layout );
  161. buttonsPanel.setLayout( jPanel3Layout );
  162.  
  163. //set up the grid for the JFrame: add JPanels to JFrame
  164. add( printerLabel, new GridBagConstraints( 0, 0 , 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add print label directly to JFrame + constraints
  165. add( selectionPanel, new GridBagConstraints( 0, 1, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add panel to JFrame + constraints
  166. add( printQualityPanel, new GridBagConstraints( 0, 2, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add panel to JFrame + constraints
  167. add( buttonsPanel, new GridBagConstraints( 1, 1, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add panel to JFrame + constraints
  168.  
  169.  
  170.  
  171. }//end of constructor
  172.  
  173.  
  174. }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement