Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Form.java
- ex 14.11 p 662
- */
- import java.awt.GridBagLayout;//chosen JFrame layout
- import java.awt.GridBagConstraints.*;
- import javax.swing.JFrame;
- import javax.swing.JComboBox;
- import javax.swing.JButton;
- import javax.swing.JTextArea;
- import javax.swing.JCheckBox;
- import javax.swing.JRadioButton;
- import javax.swing.JLabel;
- import javax.swing.ButtonGroup;//for radio buttons
- import java.awt.GridLayout;//chosen layout for panel selectionPanel and buttonsPanel
- import java.awt.FlowLayout;//chosen layout for panel printQualityPanel
- import javax.swing.JPanel;
- public class Form extends JFrame{
- //label
- private JLabel printerLabel;
- private JLabel printQualityLabel;
- //txt areas
- private JTextArea textArea1;
- private JTextArea textArea2;
- private JTextArea textArea3;
- //checkboxes
- private JCheckBox imageCheckBox;
- private JCheckBox textCheckBox;
- private JCheckBox codeCheckBox;
- private JCheckBox printCheckBox;
- //radio buttons
- private JRadioButton selectionRadioButton;
- private JRadioButton allRadioButton;
- private JRadioButton appletRadioButton;
- private ButtonGroup radioGroup;//to create logical connection between radio buttons
- //jcombobox
- private JComboBox<String> qualityCombo;
- private String[] values;
- //buttons
- private JButton okButton;
- private JButton cancelButton;
- private JButton setupButton;
- private JButton helpButton;
- //panels
- private JPanel selectionPanel;//contains text areas, radio buttons and chekboxes
- private JPanel printQualityPanel;//contains print quality combo box
- private JPanel buttonsPanel;//contains buttons
- //layout
- private GridBagLayout jPanel1Layout;
- private GridBagLayout jPanel2Layout;
- private GridBagLayout jPanel3Layout;
- private GridBagLayout labelLayout;
- private GridBagLayout layout;//layout of JFrame
- //constraints
- private GridBagConstraints constraints;
- //constructor
- public Form(){
- super("Form GUI");
- layout = new GridBagLayout();
- setLayout( layout );//set layout of JFrame
- //create panels
- selectionPanel = new JPanel();
- printQualityPanel = new JPanel();
- buttonsPanel = new JPanel();
- //create flow layout for aligning panels one next to the other
- jPanel1Layout = new GridBagLayout();
- jPanel2Layout = new GridBagLayout();
- jPanel3Layout = new GridBagLayout();
- labelLayout = new GridBagLayout();
- constraints = new GridBagConstraints();
- //create controls
- //labels
- printerLabel = new JLabel("Printer: My printer");
- printQualityLabel = new JLabel("Print Quality:");
- //txt areas
- textArea1 = new JTextArea( 5, 5 );
- textArea2 = new JTextArea( 5, 5 );
- textArea3 = new JTextArea( 5, 5 );
- //checkboxes
- imageCheckBox = new JCheckBox("Image");
- textCheckBox = new JCheckBox("Image");
- codeCheckBox = new JCheckBox("Image");
- printCheckBox = new JCheckBox("Print to file");
- //radio buttons
- selectionRadioButton = new JRadioButton("Selection", false);
- allRadioButton = new JRadioButton("All", true);
- appletRadioButton = new JRadioButton("Applet", false);
- //add radio buttons to the group
- radioGroup = new ButtonGroup();
- radioGroup.add( selectionRadioButton );
- radioGroup.add( allRadioButton );
- radioGroup.add( appletRadioButton );
- //combo box
- String[] values = { "Value1", "Value2", "Value3", "Value4" };
- qualityCombo = new JComboBox<>( values );
- //buttons
- okButton = new JButton("OK");
- cancelButton = new JButton("Cancel");
- setupButton = new JButton("Setup");
- helpButton = new JButton("Help");
- okButton = new JButton("OK");
- //BASED ON A 6X7 GRID!!
- //first text area
- 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
- //second txt area
- 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
- //third text area
- 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
- //checkboxes
- 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
- 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
- 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
- //radio buttons
- 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
- 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
- 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
- //END OF FIRST PANEL
- //SECOND PANEL containing combo box and print to file
- //label
- 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
- //jcombo
- 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
- //bottom checkbox
- 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
- //END OF SECOND PANEL
- //THIRD PANEL
- //buttons
- 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
- 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
- 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
- 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
- //END OF THIRD PANEL
- //set layout of JPanels
- selectionPanel.setLayout( jPanel1Layout ); //
- printQualityPanel.setLayout( jPanel2Layout );
- buttonsPanel.setLayout( jPanel3Layout );
- //set up the grid for the JFrame: add JPanels to JFrame
- 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
- add( selectionPanel, new GridBagConstraints( 0, 1, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add panel to JFrame + constraints
- add( printQualityPanel, new GridBagConstraints( 0, 2, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add panel to JFrame + constraints
- add( buttonsPanel, new GridBagConstraints( 1, 1, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0 ) ) );//add panel to JFrame + constraints
- }//end of constructor
- }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement