Why does this JPanel fill up the TabbedPane. While the JPanel Below this one doesn't. Can someone help me with this.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.awt.Color.*;
public class TraineeInfo extends JPanel implements ActionListener
{ // Opens class
// Text fields for user input
private JTextField name;
private JTextField street;
private JTextField city;
private JTextField statezip;
// Labels to go with each text field
private JLabel lname;
private JLabel lstreet;
private JLabel lcity;
private JLabel lstatezip;
// Check boxes for types of training
private JCheckBox ow;
private JCheckBox a;
private JCheckBox res;
private JCheckBox un;
private JCheckBox w;
// Textfields for Emergency box
private JTextField nname;
private JTextField phone;
private JTextField rel;
// Textfields for Emergency Contact
private JLabel lnname;
private JLabel lphone;
private JLabel lrel;
// Buttons and image
private JButton enter;
private JButton edit;
private JLabel seahorse;
// Panels to be built and added
// to the border layout of this
// panel.
private JPanel images;
private JPanel jaddress;
private JPanel emerg;
private JPanel training;
// Class to handle functionality of checkboxes
ItemListener handler = new CheckBoxHandler();
// Class constructor that builds the necessary
// labels and text fields images, buttons,
// and panels.
public TraineeInfo()
{ // Opens Constructor
// Sets layout for Trainee panel
setLayout(new BorderLayout());
// Sets background color for
// Diver panel
setBackground(Color.white)
;
// Initializes textfields
name = new JTextField("Enter Your Name");
street = new JTextField();
city = new JTextField ();
statezip = new JTextField ();
// Initializes labels for textfields
lname = new JLabel("Name: ");
lstreet = new JLabel("Street: ");
lcity = new JLabel ("City: ");
lstatezip = new JLabel("State & Zip Code: ");
// Initializes checkboxes with titles
ow = new JCheckBox("Beginner", true);
a = new JCheckBox("Advanced");
res = new JCheckBox("Accomplished");
un = new JCheckBox("Teacher Trainer");
w = new JCheckBox("Teacher");
// Initializes textfields for emergency panel
nname = new JTextField();
phone = new JTextField();
rel = new JTextField ();
// Initializes labels for textfields
lnname = new JLabel("Name: ");
lphone = new JLabel("Phone: ");
lrel = new JLabel ("Relationship: ");
// Initialize objects
enter = new JButton("Enter Diver Data");
edit = new JButton("Edit Diver Data");
seahorse = new JLabel("",
new ImageIcon("images/2seahors
es.jpg"),
JLabel.CENTER);
// Calls method to buid image panel, which
// is defined outside of the constructor
buildImagePanel();
// Calls method to buid address panel, which
// is defined outside of the constructor
buildAddressPanel();
// Calls method to buid emerg panel, which
// is defined outside of the constructor
buildEmergencyPanel();
// Calls method to buid training panel, which
// is defined outside of the constructor
buildTrainingPanel();
//The methods called above build the panels, then this
// call to add adds each panel to the main panel's
// border layout manager.
add(jaddress, BorderLayout.NORTH);
add(images, BorderLayout.CENTER);
add(training, BorderLayout.EAST);
add(emerg, BorderLayout.SOUTH);
} // Ends constructor
// This method creates a panel called images
private void buildImagePanel()
{ // Opens method
images = new JPanel();
// Sets the color, layout, and adds the
// seahorse object
images.setBackground(Color
.white);
images.setLayout(new FlowLayout() );
images.add(seahorse);
} // Closes method
private void buildAddressPanel ()
{ // Opens method
jaddress = new JPanel();
// Sets color and layout.
// Adds the textfields and labels for
// diver input.
jaddress.setBackground(Col
or.white);
jaddress.setLayout( new GridLayout(2, 4, 20, 20) );
//Adds each component to the panel
jaddress.add(lname);
jaddress.add(name);
jaddress.add(lstreet);
jaddress.add(street);
jaddress.add(lcity);
jaddress.add(city);
jaddress.add(lstatezip);
jaddress.add(statezip);
// Sets a border around the panel, including
// a title
jaddress.setBorder(BorderF
actory.cre
ateTitledB
order(
"Diver Personal Information"));
//Listeners for each text field in the
name.addActionListener( this );
street.addActionListener( this );
city.addActionListener( this );
statezip.addActionListener
( this );
} // Closes method
private void buildEmergencyPanel()
{ // Opens method
//Create another panel for emergency input fields
emerg = new JPanel();
emerg.setLayout( new GridLayout(2, 4, 20, 0) );
emerg.setBackground(Color.
white);
emerg.add( lnname);
emerg.add( nname);
emerg.add( lphone);
emerg.add( phone);
emerg.add( lrel);
emerg.add( rel);
emerg.add(enter);
emerg.add(edit);
//Creates titled border around emerg panel
emerg.setBorder(BorderFact
ory.create
TitledBord
er("Emerge
ncy"));
//Adds an actionListener for Emergency Info
nname.addActionListener( this );
phone.addActionListener( this );
rel.addActionListener( this );
//Creates borders around the Edit and
// Enter Diver Data buttons
enter.setBorder(BorderFact
ory.create
RaisedBeve
lBorder())
;
edit.setBorder(BorderFacto
ry.createR
aisedBevel
Border());
//Listeners for the buttons
enter.addActionListener( this );
edit.addActionListener( this );
} //Closes method
private void buildTrainingPanel()
{ //Opens method
// Creates a panel for training courses
training = new JPanel();
training.setBackground(Col
or.white);
training.setLayout(new GridLayout(5, 1, 10, 20 ) );
//sets backgrounds of components to white
training.add(ow).setBackgr
ound(Color
.white);
training.add(a).setBackgro
und(Color.
white);
training.add(un).setBackgr
ound(Color
.white);
training.add(res).setBackg
round(Colo
r.white);
training.add(w).setBackgro
und(Color.
white);
//Sets a titled border around training panel
training.setBorder(BorderF
actory.cre
ateTitledB
order("Tra
ining"));
//Adds listeners to checkbox items
ow.addItemListener(handler
);
a.addItemListener(handler)
;
un.addItemListener(handler
);
res.addItemListener(handle
r);
w.addItemListener(handler)
;
} //Closes method
// Prints the input into JLabels and hides text fields, or
// returns the text fields so input may be changed
public void actionPerformed(ActionEven
t evt)
{ // Opens method
// Checks if the button clicked was the
// Enter Diver Data button.
// If not, moves on to next if statment.
// Otherwise it enters this if statement
if ((evt.getSource() == name) || (evt.getSource() == enter))
{ // Opens if statement
//Retrives the text from the name text field and
//assigns it to the variable nameText of
//type String
String nameText = name.getText();
lname.setText("Name: " + nameText);
//After printing text to JLabel, hides the textfield
name.setVisible(false);
}// ends if statement
if ((evt.getSource() == street) || (evt.getSource() == enter))
{ // Opens if statement
String streetText = street.getText();
lstreet.setText("Street: " + streetText);
street.setVisible(false);
}// ends if statement
if ((evt.getSource() == city) || (evt.getSource() == enter))
{ // Opens if statement
String cityText = city.getText();
lcity.setText("City: " + cityText);
city.setVisible(false);
}// ends if statement
if ((evt.getSource() == statezip) || (evt.getSource() == enter))
{ // Opens if statement
String statezipText = statezip.getText();
lstatezip.setText("State & Zip: " + statezipText);
statezip.setVisible(false)
;
}// ends if statement
if ((evt.getSource() == nname) || (evt.getSource() == enter))
{ // Opens if statement
String relname = nname.getText();
lnname.setText("Name: " + relname);
nname.setVisible(false);
}// ends if statement
if ((evt.getSource() == phone) || (evt.getSource() == enter))
{ // Opens if statement
String relphone = phone.getText();
lphone.setText("Phone: " + relphone);
lphone.setForeground(Color
.red);
phone.setVisible(false);
}
if ((evt.getSource() == rel) || (evt.getSource() == enter))
{
String relString = rel.getText();
lrel.setText("Relationship
: " + relString);
rel.setVisible(false);
// If Edit button was clicked, set textfields to
// visible for user to reenter information and
// it sets the text that had been entered to
// to an empty String, giving the appearance
// that the text has been removed.
} // Closes if statement
if (evt.getSource() == edit)
{ // Opens else if statement
// If edit button has been pressed
// the following is set to visible
name.setVisible(true);
street.setVisible(true);
city.setVisible(true);
statezip.setVisible(true);
// Relative's info
nname.setVisible(true);
phone.setVisible(true);
rel.setVisible(true);
lnname.setText("Name: ");
lnname.setForeground(Color
.black);
lphone.setText("Phone: ");
lphone.setForeground(Color
.black);
lrel.setText("Relationship
: ");
lrel.setForeground(Color.b
lack);
} // Ends if statement
} // Ends actionPerformed method
// Inner class that handles functionality for the
// checkboxes.
private class CheckBoxHandler implements ItemListener
{ // Opens inner class
public void itemStateChanged (ItemEvent e)
{ // Opens method
// Retrieves object that fires an
// event, casts it as a JCheckBox
// object and assigns it to source.
JCheckBox source = (JCheckBox) e.getSource();
// If the object is selected . . .
if ( e.getStateChange() == ItemEvent.SELECTED )
// change the foreground to blue
source.setForeground(Color
.blue);
// otherwise set the foreground to black.
else
source.setForeground(Color
.black);
} // Closes ItemStateChanged method
} // Closes class CheckBoxHandler
} // Closes Class Trainee
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Kempos extends JPanel
{ // Opens class
public Kempos()
{
super();
Font f1 = new Font("Courier New", Font.BOLD, 15);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
JPanel p4 = new JPanel();
JPanel p5 = new JPanel();
final JButton b1 = new JButton("G1");
final JButton b2 = new JButton("G2");
final JButton b3 = new JButton("G3");
final JButton b4 = new JButton("G4");
final JButton b5 = new JButton("G5");
final JButton b6 = new JButton("G6");
final JButton b7 = new JButton("G7");
final JButton b8 = new JButton("G8");
final JLabel l1 = new JLabel();
l1.setBorder(BorderFactory
.createLin
eBorder(Co
lor.yellow
));
p1.setBorder(BorderFactory
.createLow
eredBevelB
order());
p4.setBorder(BorderFactory
.createLow
eredBevelB
order());
p4.setBackground(Color.blu
e);
p1.setLayout(new GridLayout(1,2));
p1.add(p2); p1.add(p3);
p2.setLayout(new GridLayout(1,1));
p2.add(p4);
p4.setLayout(new GridLayout(3,3));
p4.add(b1); p4.add(b2); p4.add(b3);
p4.add(b4); p4.add(l1); p4.add(b5);
p4.add(b6); p4.add(b7); p4.add(b8);
p5.setLayout(new GridLayout(5,1));
p5.add(new JLabel("Name 1\n"));
p5.add(new JLabel("Name 2"));
p5.add(new JLabel("Name 3"));
p5.add(new JLabel("Name 4"));
p5.add(new JLabel("Name 5"));
p3.setLayout(new GridLayout(1,3));
p3.add(new JPanel()); p3.add(p5);
p3.add(new JPanel());
add(p1, BorderLayout.CENTER);
// Add your action listener here . . .
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
t h)
{
l1.setText(" G1 ");
b1.setText("");
b1.setEnabled(false);
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
t h)
{
l1.setText(" G2 ");
b2.setText("");
b2.setEnabled(false);
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
t h)
{
l1.setText(" G3 ");
b3.setText("");
b3.setEnabled(false);
}
});
b4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
t h)
{
l1.setText(" G4 ");
b4.setText("");
b4.setEnabled(false);
}
});
b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
t h)
{
l1.setText(" G5 ");
b5.setText("");
b5.setEnabled(false);
}
});
b6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
t h)
{
l1.setText(" G6 ");
b6.setText("");
b6.setEnabled(false);
}
});
b7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
t h)
{
l1.setText(" G7 ");
b7.setText("");
b7.setEnabled(false);
}
});
b8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEven
t h)
{
l1.setText(" G8 ");
b8.setText("");
b8.setEnabled(false);
}
});
}
}