Oh, apologies. So an import can be static?! And what does it mean?
I have made the changes and I have less errors now (42) and the compiler seems to have a problem only with new GridBagLayout and with new Insets(). Now the constants are fine.
Violet_82 89 Posting Whiz in Training
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
import static
imports the names of the public static final
members of a class - ie the public constants
[update] ... that description was a little too simplified, although right in principle. Here's the definitive correct version from the JLS:
7.5.4 Static-Import-on-Demand Declarations
A static-import-on-demand declaration allows all accessible static members of
a named type to be imported as needed.
StaticImportOnDemandDeclaration:
import static TypeName . * ;
The TypeName must be the canonical name (ยง6.7) of a class type, interface type,
enum type, or annotation type.
Edited by JamesCherrill
scudzilla 32 Junior Poster
You get the GridBagLayout and Inset errors, because you have yet to import them.
import java.awt.GridBagConstraints; //you replaced this with the static one
import java.awt.Insets;
Furthermore, check your new GridBagConstraints initialisations: you have two arguments missing after the Insets.
Edited by scudzilla
Violet_82 89 Posting Whiz in Training
OK, thanks scudzilla, this thing is making me sweat!
Right, so the ipadx and ipady parameters were missing. I have added them in. ABout the import, just so I get it completely right: I now have
import static java.awt.GridBagConstraints.*;
import java.awt.Insets;
correct? (I have also tried this combination just in case
import static java.awt.GridBagConstraints;
import java.awt.Insets;
)
If I compile, I now only get 20 errors, still to do with new GridBagConstraints (that's where the caret is) saying:
Form.java:134: error: cannot find symbol
selectionPanel.add( selectionRadioButton, new GridBagConstraints( 3, 2, 1, 1, 0, 0, CENTER, NONE, new Insets( 0, 0, 0, 0), 0, 0 ) );//add component to jpanel and constraints
Code's here:
/*Form.java
ex 14.11 p 662
*/
import java.awt.GridBagLayout;//chosen JFrame layout
import static java.awt.GridBagConstraints.*;
import java.awt.Insets;
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 ), 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 ), 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 ), 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), 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), 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), 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), 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), 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), 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), 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), 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), 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 ), 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), 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), 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), 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 ), 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 ), 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 ), 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 ), 0, 0 ) );//add panel to JFrame + constraints
}//end of constructor
}//end of class
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
You're still missing
import java.awt.GridBagConstraints;
You need to import java.awt.GridBagConstraints so you can use the name GridBagConstraints without having to fully-qualify it all the time.
You need to import static GridBagConstraints.* so you can use the static constants within GridBagConstraints without having to fully-qualify their names all the time.
Violet_82 89 Posting Whiz in Training
sorry I don't mean to be thick, so they both need to be there then. Clearly I didn't understand that, sorry. OK it compiles now, but the layout reverted back to what I had a while ago, so it's ignoring the positioning.
I will go back and dig here and there trying to find the reason why.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.