Labels would not show up in the output window. The red highlighted code is where said labels get added to the window. I can't tell what's wrong with the code and I've looked at it over and over.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Preferences extends JFrame {
// Declare variables:
// Panels
private JPanel shapeType;
private JPanel fillState;
private JPanel gradientState;
private JPanel lines;
private JPanel innerPanel;
// Booleans
private boolean hasLayout;
// Labels
private JLabel label_shapeType;
private JLabel label_fillState;
private JLabel label_gradientState;
private JLabel label_gradientCols;
private JLabel label_lineWidth;
private JLabel label_dashState;
private JLabel label_dashLength;
// Buttons
private JButton but_commit;
// Constructor
public Preferences() {
// Window properties
super( "Preferences" ); // Title
setLayout( new GridLayout(5,1) ); // Grid layout with five columns
// Initialize panels into grid
newPanel( shapeType );
newPanel( fillState );
newPanel( gradientState );
newPanel( lines );
innerPanel = new JPanel();
// Initialize labels into the panels
newLabel( label_shapeType, "Shape Type", shapeType, 1 );
newLabel( label_fillState, "Fill State", fillState, 1 );
newLabel( label_gradientState, "Gradient State", gradientState, 2 );
newLabel( label_gradientCols, "Gradient Colours", gradientState, 2 );
hasLayout = false;
newLabel( label_lineWidth, "Line Width", lines, 3 );
newLabel( label_dashState, "Dash State", lines, 3 );
newLabel( label_dashLength, "Dash Length", lines, 3 );
// Initialize buttons into grid/panels
but_commit = new JButton( "Commit Changes" );
add( but_commit );
}
private JPanel newPanel( JPanel panel ) {
// Initialize panel and set its layout
panel = new JPanel();
panel.setLayout( new BorderLayout() );
// Add panel into JFrame
add( panel );
// Return the new panel
return panel;
}
private JLabel newLabel( JLabel label, String text, JPanel panel, int numLabels ) {
// Initialize label
label = new JLabel( text );
// Initialize panel
panel = new JPanel();
// If there is more than one label to be added
if( numLabels > 1 ) {
// Give innerPanel rows depending on the number of labels
// This will happen only if the layout has not already been set
if ( hasLayout == false ) {
innerPanel.setLayout( new GridLayout(1,numLabels) );
hasLayout = true;
// Add the panel into BorderLayout's north region
panel.add( innerPanel, BorderLayout.NORTH );
}
// Add the label to innerPanel (grid within BorderLayout)
innerPanel.add( label );
} else {
// Add label to BorderLayout's north region
panel.add( label, BorderLayout.NORTH );
}
// Return the new Label
return label;
}
}
Test class if it's necessary:
import javax.swing.JFrame;
public class Preferences_TEST {
public static void main( String args[] )
{
Preferences app = new Preferences();
app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
app.setSize( 450, 300 );
app.setVisible( true );
}
}